aboutsummaryrefslogtreecommitdiff
path: root/plugin/src
diff options
context:
space:
mode:
author_ov4 <ov4@ov4.org>2026-10-13 06:02:56 +0000
committer_ov4 <ov4@ov4.org>2026-10-13 06:11:36 +0000
commit9ec4fbddd559d93b3d6b072dc8f56d2a572585bd (patch)
treeac576c0da3b936ca6198b7f84944c76727a89c6b /plugin/src
parente52266d8ce8823866b2dad3e04ffc78438e08397 (diff)
feat(plugin): color bracket pairs and mark unmatched brackets red
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Diffstat (limited to 'plugin/src')
-rw-r--r--plugin/src/brackets.js25
-rw-r--r--plugin/src/extension.js26
-rw-r--r--plugin/src/lexical.js4
3 files changed, 55 insertions, 0 deletions
diff --git a/plugin/src/brackets.js b/plugin/src/brackets.js
new file mode 100644
index 0000000..ad79ea5
--- /dev/null
+++ b/plugin/src/brackets.js
@@ -0,0 +1,25 @@
+'use strict';
+
+const { codeOnly } = require('./lexical');
+const pairs = { ')': '(', ']': '[' };
+
+function unmatchedBrackets(text) {
+ const code = codeOnly(text);
+ const stack = [];
+ const unmatched = [];
+ for (let offset = 0; offset < code.length; offset++) {
+ const char = code[offset];
+ if (char === '(' || char === '[') {
+ stack.push({ char, offset });
+ } else if (pairs[char]) {
+ const opening = stack.pop();
+ if (!opening || opening.char !== pairs[char]) {
+ if (opening) unmatched.push(opening.offset);
+ unmatched.push(offset);
+ }
+ }
+ }
+ return unmatched.concat(stack.map(opening => opening.offset)).sort((a, b) => a - b);
+}
+
+module.exports = { unmatchedBrackets };
diff --git a/plugin/src/extension.js b/plugin/src/extension.js
index a7b4834..bfc51a1 100644
--- a/plugin/src/extension.js
+++ b/plugin/src/extension.js
@@ -3,6 +3,7 @@
const vscode = require('vscode');
const { indentationEdits } = require('./indentation');
const { codeOnly } = require('./lexical');
+const { unmatchedBrackets } = require('./brackets');
const languageId = 'cie-pseudocode';
function formatLines(document, startLine, endLine, keepBlankIndent = false) {
@@ -12,7 +13,31 @@ function formatLines(document, startLine, endLine, keepBlankIndent = false) {
}
function activate(context) {
+ const invalidBracket = vscode.window.createTextEditorDecorationType({
+ dark: { color: '#F44747' }
+ });
+ function refreshBrackets(document) {
+ const rangesByDocument = new Map();
+ for (const editor of vscode.window.visibleTextEditors) {
+ if (document && editor.document !== document) continue;
+ if (editor.document.languageId !== languageId) {
+ editor.setDecorations(invalidBracket, []);
+ continue;
+ }
+ if (!rangesByDocument.has(editor.document)) {
+ const ranges = unmatchedBrackets(editor.document.getText()).map(offset =>
+ new vscode.Range(editor.document.positionAt(offset), editor.document.positionAt(offset + 1)));
+ rangesByDocument.set(editor.document, ranges);
+ }
+ editor.setDecorations(invalidBracket, rangesByDocument.get(editor.document));
+ }
+ }
context.subscriptions.push(
+ invalidBracket,
+ vscode.workspace.onDidChangeTextDocument(event => refreshBrackets(event.document)),
+ // Changing a document's language closes/reopens it without a text edit.
+ vscode.workspace.onDidOpenTextDocument(() => refreshBrackets()),
+ vscode.window.onDidChangeVisibleTextEditors(() => refreshBrackets()),
vscode.languages.registerDocumentRangeFormattingEditProvider(languageId, {
provideDocumentRangeFormattingEdits(document, range) {
const endLine = range.end.character === 0 && range.end.line > range.start.line
@@ -28,6 +53,7 @@ function activate(context) {
}
}, '\n', 'E', 'F', 'L', 'T', 'N')
);
+ refreshBrackets();
}
module.exports = { activate };
diff --git a/plugin/src/lexical.js b/plugin/src/lexical.js
index 3fc856a..ff01166 100644
--- a/plugin/src/lexical.js
+++ b/plugin/src/lexical.js
@@ -17,6 +17,10 @@ function codeOnly(text) {
} else if (quote) {
if (char === quote) quote = null;
result.push(' ');
+ } else if (text.slice(i, i + 3) === "'''") {
+ // The compiler accepts a quote character as a three-quote CHAR literal.
+ result.push(' ', ' ', ' ');
+ i += 2;
} else if (char === '"' || char === "'") {
quote = char;
result.push(' ');