fixed syntax highlighting (don't highlight partial tokens/keywords)

This commit is contained in:
Christopher 2022-01-07 19:19:03 -06:00
parent d2193e017d
commit ad0b73aaee

@ -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, "");