Merge pull request #2451 from neuralsim/fixSyntaxHighlighting

fixed syntax highlighting (don't highlight partial tokens/keywords)
This commit is contained in:
hydroflame 2022-01-08 13:30:35 -05:00 committed by GitHub
commit ff15d2d730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -288,13 +288,15 @@ export function Root(props: IProps): React.ReactElement {
.getLanguages() .getLanguages()
.find((l: any) => l.id === "javascript") .find((l: any) => l.id === "javascript")
.loader(); .loader();
l.language.tokenizer.root.unshift(["ns", { token: "ns" }]); // replaced the bare tokens with regexes surrounded by \b, e.g. \b{token}\b which matches a word-break on either side
for (const symbol of symbols) l.language.tokenizer.root.unshift([symbol, { token: "netscriptfunction" }]); // this prevents the highlighter from highlighting pieces of variables that start with a reserved token name
l.language.tokenizer.root.unshift([new RegExp('\\bns\\b'), { token: "ns" }]);
for (const symbol of symbols) l.language.tokenizer.root.unshift([new RegExp(`\\b${symbol}\\b`), { token: "netscriptfunction" }]);
const otherKeywords = ["let", "const", "var", "function"]; const otherKeywords = ["let", "const", "var", "function"];
const otherKeyvars = ["true", "false", "null", "undefined"]; const otherKeyvars = ["true", "false", "null", "undefined"];
otherKeywords.forEach((k) => l.language.tokenizer.root.unshift([k, { token: "otherkeywords" }])); otherKeywords.forEach((k) => l.language.tokenizer.root.unshift([new RegExp(`\\b${k}\\b`), { token: "otherkeywords" }]));
otherKeyvars.forEach((k) => l.language.tokenizer.root.unshift([k, { token: "otherkeyvars" }])); otherKeyvars.forEach((k) => l.language.tokenizer.root.unshift([new RegExp(`\\b${k}\\b`), { token: "otherkeyvars" }]));
l.language.tokenizer.root.unshift(["this", { token: "this" }]); l.language.tokenizer.root.unshift([new RegExp('\\bthis\\b'), { token: "this" }]);
})(); })();
const source = (libSource + "").replace(/export /g, ""); const source = (libSource + "").replace(/export /g, "");