Commit 0fce839d authored by Phil Jones's avatar Phil Jones

Add finesse2 syntax highlighting.

parent 29634d18
......@@ -8,18 +8,199 @@ import 'codemirror/mode/python/python';
import 'codemirror/addon/mode/multiplex';
declare module 'codemirror' {
function multiplexingMode(x: CodeMirror.Mode<any>, ...others: object[]): CodeMirror.Mode<any>;
var modeInfo: any[];
// There should be a better way to do this to stop typescript
// complaining....
function multiplexingMode(outerMode: CodeMirror.Mode<any>, ...others: object[]): CodeMirror.Mode<any>;
}
const components = [
'bs[12]\?',
'dbs',
'gr\d*',
'isol',
'l',
'lens',
'm[12]\?',
'mod',
's',
'sq',
]
const detectors = [
'ad',
'beam',
'bp',
'cp',
'guoy',
'hd',
'pd[SN]\?\d*',
'pgaind',
'qd',
'q\d*hd\?',
'qhd[SN]\?',
'qnoised[SN]\?',
'qshot',
'qshot[SN]\?',
'sd',
'xd',
]
const commands = [
'attr',
'cav',
'conf',
'fadd',
'fsig',
'gauss',
'knm',
'lambda',
'map',
'mask',
'maxtem',
'pdtype',
'phase',
'retrace',
'smotion',
'startnode',
'tem',
'tf2\?',
'vacuum',
// Plotting:
'const',
'deriv_h',
'diff',
'func',
'lock',
'noplot',
'noxaxis',
'put',
'scale',
'set',
'trace',
'var',
'x2\?axis',
'yaxis',
// Auxiliary
'gnuterm',
'multi',
'pause',
'pyterm',
]
function wordListRegexp(words: string[]): RegExp {
return new RegExp("^((" + words.join(")|(") + "))\\b");
}
const regex = {
"command": wordListRegexp(commands),
"component": wordListRegexp(components),
"detector": wordListRegexp(detectors),
"name": /[a-zA-Z_][a-zA-Z0-9_-]/,
"number": /^(([+-]?inf)|([+-]?(\d+\.\d*|\d*\.\d+|\d+)([eE]-?\d*\.?\d*)?j?([pnumkMGT])?))$/,
"variable": /\$\w+/
}
/*
* Available tokens:
*
* keyword
* atom
* number
* def
* variable
* variable-2
* variable-3
* property
* operator
* comment
* string
* string-2
* meta
* qualifier
* builtin
* bracket
* tag
* attribute
* header
* quote
* hr
* link
*/
CodeMirror.defineMode("finesse2", () => {
function tokenBase(stream: CodeMirror.StringStream, state: any) {
if (stream.sol()) {
state.firstOnLine = true;
}
if (stream.eatSpace()) {
return null;
}
if (/[#%]/.test(stream.peek())) {
stream.skipToEnd();
return "comment";
}
if (stream.peek() == '$') {
stream.next();
}
stream.eatWhile(/\w/);
let style = null;
let cur = stream.current();
//console.log(cur, state.prevToken);
if (state.firstOnLine) {
if (regex["component"].test(cur) || regex["detector"].test(cur)) {
// Component definition
style = "keyword";
} else if (regex["command"].test(cur)) {
// Command
style = "builtin";
}
} else if (state.prevToken == "keyword" && regex["name"].test(cur)) {
// Component name
style = "def";
} else if (regex["number"].test(cur)) {
// Number
style = "number";
} else if (regex["variable"].test(cur)) {
style = "variable-2";
}
if (style != null) {
state.firstOnLine = false;
state.prevToken = style;
return style;
}
stream.next();
}
return <CodeMirror.Mode<any>> {
startState: function() {
return {
tokenize: tokenBase,
firstOnLine: true,
prevToken: <string> null
};
},
token: function(stream, state) {
return state.tokenize(stream, state)
}
};
});
CodeMirror.defineMIME("text/x-finesse2", "finesse2");
CodeMirror.defineMode("finesse-python", (config) => {
let pmode = CodeMirror.getMode(config, "python");
return CodeMirror.multiplexingMode(
pmode,
{
open: /#katcode/,
open: /(?=#kat2code)/,
close: /(?=""")/, // Match string end without consuming it
mode: CodeMirror.getMode(config, "text/html"),
mode: CodeMirror.getMode(config, "text/x-finesse2"),
delimStyle: "delim"
}
)
......
......@@ -44,7 +44,7 @@ function activate_finesse(sender: INotebookTracker, panel: NotebookPanel): void
// I ain't proud of this, but it's the sanest way I could find to get
// the syntax highlighting to change on startup without selecting a
// different cell
setTimeout(() => {check_all(panel.content)}, 1000);
setTimeout(() => {check_all(panel.content)}, 500);
};
export default plugin;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment