diff --git a/package-lock.json b/package-lock.json index 181bf97b..95db6bad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,7 +37,7 @@ "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^12.1.1", "fs-extra": "^11.2.0", - "monaco-editor": "^0.34.1", + "monaco-editor": "^0.52.0", "nunjucks": "^3.2.4", "postcss": "^8.4.49", "postcss-url": "^10.1.3", @@ -2437,9 +2437,9 @@ } }, "node_modules/monaco-editor": { - "version": "0.34.1", - "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.34.1.tgz", - "integrity": "sha512-FKc80TyiMaruhJKKPz5SpJPIjL+dflGvz4CpuThaPMc94AyN7SeC9HQ8hrvaxX7EyHdJcUY5i4D0gNyJj1vSZQ==", + "version": "0.52.0", + "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.52.0.tgz", + "integrity": "sha512-OeWhNpABLCeTqubfqLMXGsqf6OmPU6pHM85kF3dhy6kq5hnhuVS1p3VrEW/XhWHc71P2tHyS5JFySD8mgs1crw==", "dev": true }, "node_modules/nanoid": { @@ -5742,9 +5742,9 @@ "dev": true }, "monaco-editor": { - "version": "0.34.1", - "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.34.1.tgz", - "integrity": "sha512-FKc80TyiMaruhJKKPz5SpJPIjL+dflGvz4CpuThaPMc94AyN7SeC9HQ8hrvaxX7EyHdJcUY5i4D0gNyJj1vSZQ==", + "version": "0.52.0", + "resolved": "https://registry.npmjs.org/monaco-editor/-/monaco-editor-0.52.0.tgz", + "integrity": "sha512-OeWhNpABLCeTqubfqLMXGsqf6OmPU6pHM85kF3dhy6kq5hnhuVS1p3VrEW/XhWHc71P2tHyS5JFySD8mgs1crw==", "dev": true }, "nanoid": { diff --git a/package.json b/package.json index 87c3c6f9..e0f3df22 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "@rollup/plugin-terser": "^0.4.4", "@rollup/plugin-typescript": "^12.1.1", "fs-extra": "^11.2.0", - "monaco-editor": "^0.34.1", + "monaco-editor": "^0.52.0", "nunjucks": "^3.2.4", "postcss": "^8.4.49", "postcss-url": "^10.1.3", diff --git a/src/editor/monaco-provider.ts b/src/editor/monaco-provider.ts index fef103e5..74419745 100644 --- a/src/editor/monaco-provider.ts +++ b/src/editor/monaco-provider.ts @@ -10,6 +10,7 @@ const documents = new ESPHomeDocuments(); // Register the custom language monaco.languages.register({ id: "esphome" }); +monaco.languages.register({ id: "cpp" }); const hoverHandler = new HoverHandler(documents); monaco.languages.registerHoverProvider("esphome", { @@ -321,8 +322,440 @@ monaco.languages.setMonarchTokensProvider("esphome", { [/@numberDate(?=[ \t]*[,\]\}])/, "number.date"], ], - tagHandle: [[/\![^ ]*/, "tag"]], + tagHandle: [ + [/\!lambda /, "tag", "@lambda"], + [/\![^ ]*/, "tag"], + ], anchor: [[/[&*][^ ]+/, "namespace"]], + + lambda: [ + [ + /[ \t]*[>|][0-9]*[+-]?[ \t]*$/, + { token: "operators", switchTo: "@lambdaBlock" }, + ], + [ + /[ \t]*(['"])/, + { token: "string", switchTo: "@lambdaString.$1", nextEmbedded: "cpp" }, + ], + [ + /.+$/, + { token: "@rematch", switchTo: "@lambdaLine", nextEmbedded: "cpp" }, + ], + ], + + lambdaBlock: [ + [ + /^([ \t]+).+$/, + { + token: "@rematch", + switchTo: "@lambdaBlockContinued.$1", + nextEmbedded: "cpp", + }, + ], + ], + + lambdaBlockContinued: [ + [/^($S2).+$/, { token: "" }], + [ + /^(?!$S2).+$/, + { token: "@rematch", next: "@pop", nextEmbedded: "@pop" }, + ], + ], + + lambdaString: [ + [/$S2/, { token: "string", next: "@pop", nextEmbedded: "@pop" }], + [/./, { token: "" }], + ], + + lambdaLine: [ + [/$/, { token: "@rematch", next: "@pop", nextEmbedded: "@pop" }], + [/./, { token: "" }], + ], + }, +}); + +monaco.languages.setMonarchTokensProvider("cpp", { + defaultToken: "", + tokenPostfix: ".cpp", + + brackets: [ + { token: "delimiter.curly", open: "{", close: "}" }, + { token: "delimiter.parenthesis", open: "(", close: ")" }, + { token: "delimiter.square", open: "[", close: "]" }, + { token: "delimiter.angle", open: "<", close: ">" }, + ], + + keywords: [ + "abstract", + "amp", + "array", + "auto", + "bool", + "break", + "case", + "catch", + "char", + "class", + "const", + "constexpr", + "const_cast", + "continue", + "cpu", + "decltype", + "default", + "delegate", + "delete", + "do", + "double", + "dynamic_cast", + "each", + "else", + "enum", + "event", + "explicit", + "export", + "extern", + "false", + "final", + "finally", + "float", + "for", + "friend", + "gcnew", + "generic", + "goto", + "if", + "in", + "initonly", + "inline", + "int", + "interface", + "interior_ptr", + "internal", + "literal", + "long", + "mutable", + "namespace", + "new", + "noexcept", + "nullptr", + "__nullptr", + "operator", + "override", + "partial", + "pascal", + "pin_ptr", + "private", + "property", + "protected", + "public", + "ref", + "register", + "reinterpret_cast", + "restrict", + "return", + "safe_cast", + "sealed", + "short", + "signed", + "sizeof", + "static", + "static_assert", + "static_cast", + "struct", + "switch", + "template", + "this", + "thread_local", + "throw", + "tile_static", + "true", + "try", + "typedef", + "typeid", + "typename", + "union", + "unsigned", + "using", + "virtual", + "void", + "volatile", + "wchar_t", + "where", + "while", + + "_asm", // reserved word with one underscores + "_based", + "_cdecl", + "_declspec", + "_fastcall", + "_if_exists", + "_if_not_exists", + "_inline", + "_multiple_inheritance", + "_pascal", + "_single_inheritance", + "_stdcall", + "_virtual_inheritance", + "_w64", + + "__abstract", // reserved word with two underscores + "__alignof", + "__asm", + "__assume", + "__based", + "__box", + "__builtin_alignof", + "__cdecl", + "__clrcall", + "__declspec", + "__delegate", + "__event", + "__except", + "__fastcall", + "__finally", + "__forceinline", + "__gc", + "__hook", + "__identifier", + "__if_exists", + "__if_not_exists", + "__inline", + "__int128", + "__int16", + "__int32", + "__int64", + "__int8", + "__interface", + "__leave", + "__m128", + "__m128d", + "__m128i", + "__m256", + "__m256d", + "__m256i", + "__m512", + "__m512d", + "__m512i", + "__m64", + "__multiple_inheritance", + "__newslot", + "__nogc", + "__noop", + "__nounwind", + "__novtordisp", + "__pascal", + "__pin", + "__pragma", + "__property", + "__ptr32", + "__ptr64", + "__raise", + "__restrict", + "__resume", + "__sealed", + "__single_inheritance", + "__stdcall", + "__super", + "__thiscall", + "__try", + "__try_cast", + "__typeof", + "__unaligned", + "__unhook", + "__uuidof", + "__value", + "__virtual_inheritance", + "__w64", + "__wchar_t", + ], + + operators: [ + "=", + ">", + "<", + "!", + "~", + "?", + ":", + "==", + "<=", + ">=", + "!=", + "&&", + "||", + "++", + "--", + "+", + "-", + "*", + "/", + "&", + "|", + "^", + "%", + "<<", + ">>", + "+=", + "-=", + "*=", + "/=", + "&=", + "|=", + "^=", + "%=", + "<<=", + ">>=", + ], + + // we include these common regular expressions + symbols: /[=>\[\]]/, "@brackets"], + [ + /@symbols/, + { + cases: { + "@operators": "delimiter", + "@default": "", + }, + }, + ], + + // numbers + [/\d*\d+[eE]([\-+]?\d+)?(@floatsuffix)/, "number.float"], + [/\d*\.\d+([eE][\-+]?\d+)?(@floatsuffix)/, "number.float"], + [/0[xX][0-9a-fA-F']*[0-9a-fA-F](@integersuffix)/, "number.hex"], + [/0[0-7']*[0-7](@integersuffix)/, "number.octal"], + [/0[bB][0-1']*[0-1](@integersuffix)/, "number.binary"], + [/\d[\d']*\d(@integersuffix)/, "number"], + [/\d(@integersuffix)/, "number"], + + // delimiter: after number because of .\d floats + [/[;,.]/, "delimiter"], + + // strings + [/"([^"\\]|\\.)*$/, "string.invalid"], // non-teminated string + [/"/, "string", "@string"], + + // characters + [/'[^\\']'/, "string"], + [/(')(@escapes)(')/, ["string", "string.escape", "string"]], + [/'/, "string.invalid"], + ], + + whitespace: [ + [/[ \t\r\n]+/, ""], + [/\/\*\*(?!\/)/, "comment.doc", "@doccomment"], + [/\/\*/, "comment", "@comment"], + [/\/\/.*\\$/, "comment", "@linecomment"], + [/\/\/.*$/, "comment"], + ], + + comment: [ + [/[^\/*]+/, "comment"], + [/\*\//, "comment", "@pop"], + [/[\/*]/, "comment"], + ], + + //For use with continuous line comments + linecomment: [ + [/.*[^\\]$/, "comment", "@pop"], + [/[^]+/, "comment"], + ], + + //Identical copy of comment above, except for the addition of .doc + doccomment: [ + [/[^\/*]+/, "comment.doc"], + [/\*\//, "comment.doc", "@pop"], + [/[\/*]/, "comment.doc"], + ], + + string: [ + [/[^\\"]+/, "string"], + [/@escapes/, "string.escape"], + [/\\./, "string.escape.invalid"], + [/"/, "string", "@pop"], + ], + + raw: [ + [/[^)]+/, "string.raw"], + [/\)$S2\"/, { token: "string.raw.end", next: "@pop" }], + [/\)/, "string.raw"], + ], + + annotation: [ + { include: "@whitespace" }, + [/using|alignas/, "keyword"], + [/[a-zA-Z0-9_]+/, "annotation"], + [/[,:]/, "delimiter"], + [/[()]/, "@brackets"], + [/\]\s*\]/, { token: "annotation", next: "@pop" }], + ], + + include: [ + [ + /(\s*)(<)([^<>]*)(>)/, + [ + "", + "keyword.directive.include.begin", + "string.include.identifier", + { token: "keyword.directive.include.end", next: "@pop" }, + ], + ], + [ + /(\s*)(")([^"]*)(")/, + [ + "", + "keyword.directive.include.begin", + "string.include.identifier", + { token: "keyword.directive.include.end", next: "@pop" }, + ], + ], + ], }, });