refactor: improve syntax highlighting and change theme in editor (#1200)

Co-authored-by: truckershitch <truckershitch@hambone.e4ward.com>
Co-authored-by: Stefan Dej <meteyou@gmail.com>
This commit is contained in:
authored and GitHub committed 2023-05-18 16:40:57 +02:00
1 parent 140d796f51
commit 080a713f4c
9 files changed
+341 -465

No files matched your search

+279 -41
View File
@@ -3,6 +3,212 @@ import { gcode } from '@/plugins/StreamParserGcode'
export const klipper_config: StreamParser<any> = {
token: function (stream: StringStream, state: StreamParserKlipperConfigState): string | null {
/* see https://tedboy.github.io/jinja2/off_doc.templates.html */
const operators = [
'\\+',
'-',
'\\/\\/',
'\\/',
'%',
'\\*\\*',
'\\*',
'\\(',
'\\)',
'==',
'!=',
'>=',
'>',
'<=',
'<',
'=',
'\\|',
'~',
',',
]
const reOperator = new RegExp('^' + operators.join('|'))
const keywords = [
'elif',
'else',
'endif',
'if',
'endfor',
'for',
'loop\\.index',
'loop\\.revindex',
'loop\\.first',
'loop\\.last',
'loop\\.length',
'loop\\.cycle',
'loop\\.depth',
'and',
'or',
'not',
'in',
'is',
'endmacro',
'macro',
'endcall',
'call',
'endfilter',
'filter',
'endset',
'set',
'extends',
'block',
'endblock',
'include',
'import',
'do',
]
const filters = [
'abs',
'attr',
'batch',
'capitalize',
'center',
'default',
'dictsort',
'escape',
'filesizeformat',
'first',
'float',
'forceescape',
'format',
'groupby',
'indent',
'int',
'join',
'last',
'length',
'list',
'lower',
'map',
'max',
'min',
'pprint',
'random',
'reject',
'rejectattr',
'replace',
'reverse',
'round',
'tojson',
'safe',
'select',
'selectattr',
'slice',
'sort',
'string',
'striptags',
'sum',
'title',
'trim',
'truncate',
'unique',
'upper',
'urlencode',
'urlize',
'wordcount',
'wordwrap',
'xmlattr',
]
const tests = [
'callable',
'defined',
'divisibleby',
'equalto',
'escaped',
'even',
'iterable',
'lower',
'mapping',
'none',
'number',
'odd',
'sameas',
'sequence',
'string',
'undefined',
'upper',
]
const globalFns = ['range', 'lipsum', 'dict', 'cycler', 'joiner']
const cyclerMethods = ['\\.reset\\(\\)', '\\.next\\(\\)']
const reKeyword = new RegExp('^' + keywords.join('\\s+|') + '|' + cyclerMethods.join('|') + '\\s+')
const reUpdateOps = new RegExp(
'^' + filters.join('|') + '|' + tests.join('|') + '|' + globalFns.join('|') + '\\s+'
)
function jinja2Element(stream: StringStream): string {
const pctMatch: any = stream.match(/^%}/)
const braceMatch: any = stream.match(/^}/)
function notJinja(): boolean {
return state.klipperMacroJinjaBraceStack.length === 0 && state.klipperMacroJinjaPctStack.length === 0
}
if (pctMatch || braceMatch) {
if (pctMatch) {
state.klipperMacroJinjaPctStack.pop()
if (notJinja()) {
state.klipperMacroJinja = false
}
} else {
// brace match
state.klipperMacroJinjaBraceStack.pop()
if (notJinja()) {
state.klipperMacroJinja = false
}
}
stream.eatSpace()
state.gcodeZeroPos = stream.pos
return 'tag'
}
// https://www.metaltoad.com/blog/regex-quoted-string-escapable-quotes
if (stream.match(/^((?<![\\])['"])((?:.(?!(?<![\\])\1))*.?)\1/)) {
state.klipperMacroJinjaHighlightNext = true
return 'string'
}
if (stream.eol() && stream.match(/^"/)) {
state.klipperMacroJinjaHighlightNext = false
return 'string'
}
if (state.klipperMacroJinjaHighlightNext) {
if (stream.match(reKeyword)) {
state.klipperMacroJinjaHighlightNext = false
return 'keyword'
}
}
if (state.klipperMacroJinjaHighlightNext) {
if (stream.match(reUpdateOps)) {
// check character after "updateOp" element -- might not be a "("
if (['(', '}', ',', '|', ' '].includes(stream.peek() ?? '')) {
state.klipperMacroJinjaHighlightNext = false
return 'updateOperator'
}
}
}
if (stream.match(reOperator)) {
state.klipperMacroJinjaHighlightNext = true
return 'number'
}
if (stream.match(/^true\s|false\s/i)) {
state.klipperMacroJinjaHighlightNext = false
return 'atom'
}
if (stream.match(/^[-+]?[0-9]*\.?[0-9]+/)) {
state.klipperMacroJinjaHighlightNext = false
return 'number'
}
if (stream.eatSpace()) {
state.klipperMacroJinjaHighlightNext = true
return 'propertyName'
}
stream.next()
state.klipperMacroJinjaHighlightNext = false
return 'propertyName'
}
const ch = stream.peek()
/* comments */
if (
@@ -50,57 +256,57 @@ export const klipper_config: StreamParser<any> = {
stream.eatSpace()
state.gcodeZeroPos = stream.pos
}
if (state.klipperMacroJinja) {
if (
(state.klipperMacroJinjaPercent && stream.match(/^%}/)) ||
(!state.klipperMacroJinjaPercent && stream.match(/^}/))
) {
state.klipperMacroJinja = false
state.klipperMacroJinjaPercent = false
stream.eatSpace()
state.gcodeZeroPos = stream.pos
return null
}
stream.next()
return 'string'
}
if (stream.match(/^\s*{[%{]?/)) {
state.klipperMacroJinjaPercent = stream.string.includes('{%')
if (stream.match(/^\s*{[%#]?/)) {
state.klipperMacroJinja = true
return null
if (stream.string.includes('{%')) {
state.klipperMacroJinjaPctStack.push('{%')
} else {
state.klipperMacroJinjaBraceStack.push('{')
}
return 'tag'
}
if (state.klipperMacroJinja) {
return jinja2Element(stream)
}
return gcode.token(stream, state, state.gcodeZeroPos ?? 0)
}
} else {
// stream.indentation > 0
state.was = true
if (state.gcode) {
if (stream.sol()) {
stream.eatSpace()
state.gcodeZeroPos = stream.pos
}
if (state.klipperMacroJinja) {
if (
(state.klipperMacroJinjaPercent && stream.match(/^%}/)) ||
(!state.klipperMacroJinjaPercent && stream.match(/^}/))
) {
state.klipperMacroJinja = false
state.klipperMacroJinjaPercent = false
stream.eatSpace()
state.gcodeZeroPos = stream.pos
return null
}
stream.next()
return 'string'
}
if (stream.match(/^\s*{[%{]?/)) {
state.klipperMacroJinjaPercent = stream.string.includes('{%')
if (stream.match(/^\s*{[%#]?/)) {
state.klipperMacroJinja = true
return null
if (stream.string.includes('{%')) {
state.klipperMacroJinjaPctStack.push('{%')
} else {
state.klipperMacroJinjaBraceStack.push('{')
}
return 'tag'
}
if (state.klipperMacroJinja) {
return jinja2Element(stream)
}
return gcode.token(stream, state, state.gcodeZeroPos ?? stream.pos)
} else if (state.variable) {
if (stream.sol()) {
stream.eatSpace()
}
if (stream.match(/^\s*{[%#]?/)) {
state.klipperMacroJinja = true
if (stream.string.includes('{%')) {
state.klipperMacroJinjaPctStack.push('{%')
} else {
state.klipperMacroJinjaBraceStack.push('{')
}
return 'tag'
}
if (state.klipperMacroJinja) {
return jinja2Element(stream)
}
} else if (state.pair) {
stream.eatSpace()
if (ch !== ',') {
@@ -118,12 +324,15 @@ export const klipper_config: StreamParser<any> = {
if (state.was && stream.indentation() === 0) {
state.pair = false
state.gcode = false
state.variable = false
state.was = false
}
if (!state.pair && !state.gcode && stream.sol()) {
if (!state.pair && !state.gcode && !state.variable && stream.sol()) {
if (stream.match(/^(?:[A-Za-z]*_?gcode|enable):/)) {
state.gcode = true
} else if (stream.match(/^variable_[a-zA-Z]+:/)) {
state.variable = true
} else {
stream.match(/^.+?:\s*/)
state.pair = !stream.eol()
@@ -132,6 +341,30 @@ export const klipper_config: StreamParser<any> = {
return 'atom'
}
if (state.variable) {
if (stream.sol() || stream.eol()) {
state.variable = false
return null
}
if (stream.match(/^\s*{[%#]?/)) {
state.klipperMacroJinja = true
if (stream.string.includes('{%')) {
state.klipperMacroJinjaPctStack.push('{%')
} else {
state.klipperMacroJinjaBraceStack.push('{')
}
return 'tag'
} else {
// no Jinja in variable
state.pair = true
// state.variable = false
}
if (state.klipperMacroJinja) {
stream.eatSpace()
return jinja2Element(stream)
}
}
if (state.pair) {
if (ch === ':') {
stream.next()
@@ -143,7 +376,6 @@ export const klipper_config: StreamParser<any> = {
state.pair = false
return null
}
if (stream.match(/^(-?\d*\.?(?:\d+)?(,|$|\s))+/)) {
state.pair = false
return 'number'
@@ -163,10 +395,13 @@ export const klipper_config: StreamParser<any> = {
pair: false,
was: false,
gcode: false,
variable: false,
klipperMacro: false,
gcodeZeroPos: null,
klipperMacroJinja: false,
klipperMacroJinjaPercent: false,
klipperMacroJinjaHighlightNext: false,
klipperMacroJinjaBraceStack: [],
klipperMacroJinjaPctStack: [],
}
},
languageData: {
@@ -179,8 +414,11 @@ interface StreamParserKlipperConfigState {
pair: boolean
was: boolean
gcode: boolean
variable: boolean
gcodeZeroPos: number | null
klipperMacro: boolean
klipperMacroJinja: boolean
klipperMacroJinjaPercent: boolean
klipperMacroJinjaHighlightNext: boolean // Highlight next element if no space follows curent keyword
klipperMacroJinjaBraceStack: string[] // Should these two stacks be combined for overhead / aesthetics?
klipperMacroJinjaPctStack: string[]
}