From 337475f058be67a3583c9d56df86ff5611151517 Mon Sep 17 00:00:00 2001 From: Luca Di Stefano Date: Mon, 23 Feb 2026 12:28:13 +0100 Subject: [PATCH] Improve syntax highlighting We add a step to the build process that takes the syntax highlighting file generated by :angium (syntaxes/r-check.tmLanguage.json) and inject better, hand-made patterns. It also prints a warning if keywords are not found in the new syntax highlighting (although it is a simple substring search, thus may give false positives). Fixes #3. --- Makefile | 3 +- fix-syntax.cjs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 fix-syntax.cjs diff --git a/Makefile b/Makefile index fac80bc..8d5fdc2 100644 --- a/Makefile +++ b/Makefile @@ -17,9 +17,10 @@ version = $(strip $(shell grep version package.json | tr -s ' ' | cut -d' ' -f3 build: out/extension/main.js -out/extension/main.js: $(src) $(bin) $(grammar) package.json +out/extension/main.js: $(src) $(bin) $(grammar) package.json fix-syntax.cjs npm install npm run langium:generate + npm run fix-syntax npm run build update_submodules: diff --git a/fix-syntax.cjs b/fix-syntax.cjs new file mode 100644 index 0000000..f6eccfb --- /dev/null +++ b/fix-syntax.cjs @@ -0,0 +1,81 @@ +console.log("Fixing syntax hihglighting...") + +const fs = require('fs'); +const path = require('path'); + +const jsonpath = path.join(__dirname, "syntaxes", "r-check.tmLanguage.json"); + +fs.readFile(jsonpath, 'utf8', (err, data) => { + if (err) { + console.error('Error reading file:', err); + return; + } + const jsonData = JSON.parse(data); + let oldPatterns = []; + jsonData.patterns.forEach(element => { + const match = (element.match ?? "") + .split("|") + .map(s => s.trim() + .replace(/\\b/g, "").replace(/\\B/g, "") + .replace("(", "").replace(")", "")) + .filter(s => s.length > 0); + oldPatterns.push(...match); + }); + // const oldPatterns = jsonData.patterns; + console.log("Old patterns:", oldPatterns); + + + jsonData.patterns = [ + { + include: "#comments" + }, + { + name: "keyword.control.r-check", + match: "\\b(SPEC|enum|channels|property-variables|exists|false|forall|guard|init|local|message-structure|receive-guard|relabel|rep|repeat|system)\\b|\\B(-automaton-state)\\b|\\b(GET@|SUPPLY@)\\B" + }, + { + match: "\\b(agent)\\b\\s+([^\\s]+)", + captures: { + 1: { name: "keyword.other.agent.r-check" }, + 2: { name: "entity.name.agent.r-check" } + } + }, + { + name: "keyword.operator.r-check", + match: "\\b(F|G|R|U|W|X|exists|forall)\\b" + }, + { + name: "constant.language.r-check", + match: "\\b(false|true|any)\\b" + }, + { + name: "constant.numeric.r-check", + match: "\\b([0-9]+)\\b" + }, + { + name: "variable.language.r-check", + match: "\\b(myself|chan|sender|getter|supplier|p2p)\\b" + }, + { + name: "storage.type.r-check", + match: "\\b(bool|channel|int|Agent|location)\\b" + } + ]; + + // Careful, this can still have false positives + oldPatterns.forEach(pattern => { + if (!jsonData.patterns.some(p => p.match !== undefined && p.match.includes(pattern))) { + console.warn(`Pattern "${pattern}" is missing in the new patterns.`); + } + }); + + + fs.writeFile(jsonpath, JSON.stringify(jsonData, null, 2), 'utf8', (err) => { + if (err) { + console.error('Error writing file:', err); + return; + } + console.log('Syntax file updated successfully'); + }); +}); + diff --git a/package.json b/package.json index 38524d6..1575f99 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "bundle:serve": "http-server ./dist --port 5175", "dev": "vite", "dev:debug": "vite --debug --force", + "fix-syntax": "node fix-syntax.cjs", "serve": "npm run dev", "test": "vitest run" },