From c17775323baf93e0d9bd44a6f83cd32a986b6fcb Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 21 Oct 2026 10:50:48 +0000 Subject: refactor(plugin): share formatting triggers and selection boundaries Co-Authored-By: Claude Opus 5.5 --- plugin/AGENTS/ARCH.md | 3 +++ plugin/src/extension.js | 11 ++++------- plugin/src/indentation.js | 11 ++++++++++- 3 files changed, 17 insertions(+), 8 deletions(-) (limited to 'plugin') diff --git a/plugin/AGENTS/ARCH.md b/plugin/AGENTS/ARCH.md index 5242d3e..0f585bb 100644 --- a/plugin/AGENTS/ARCH.md +++ b/plugin/AGENTS/ARCH.md @@ -43,6 +43,9 @@ Source attribution is retained in `ThirdPartyNotices.md`. - `src/indentation.js` computes block depth from earlier lines and returns edits to leading whitespace only. A selection ending at column zero excludes that final line. Formatting is idempotent and preserves line endings and other text. + It also exports selection boundaries and on-type triggers for both editor adapters. + The Bundle imports this module, `lexical.js`, `brackets.js`, the grammar and + language configuration directly; keep these modules free of editor APIs. - `src/lexical.js` masks quotes and comments while preserving UTF-16 offsets. - `src/brackets.js` uses a stack to report unmatched opening and closing brackets and crossed types. Only invalid brackets receive the red decoration; native diff --git a/plugin/src/extension.js b/plugin/src/extension.js index 6bd1255..8fd5d22 100644 --- a/plugin/src/extension.js +++ b/plugin/src/extension.js @@ -1,8 +1,7 @@ 'use strict'; const vscode = require('vscode'); -const { indentationEdits } = require('./indentation'); -const { codeOnly } = require('./lexical'); +const { indentationEdits, onTypeCharacters, shouldFormatOnType, selectionEndLine } = require('./indentation'); const { unmatchedBrackets } = require('./brackets'); const { registerRunner } = require('./runner'); const languageId = 'cie-pseudocode'; @@ -42,18 +41,16 @@ function activate(context) { vscode.window.onDidChangeVisibleTextEditors(() => refreshBrackets()), vscode.languages.registerDocumentRangeFormattingEditProvider(languageId, { provideDocumentRangeFormattingEdits(document, range) { - const endLine = range.end.character === 0 && range.end.line > range.start.line - ? range.end.line - 1 : range.end.line; + const endLine = selectionEndLine(range.start.line, range.end.line, range.end.character); return formatLines(document, range.start.line, endLine); } }), vscode.languages.registerOnTypeFormattingEditProvider(languageId, { provideOnTypeFormattingEdits(document, position, char) { - if (char !== '\n' && !/^\s*(?:ELSE|ENDIF|ENDWHILE|UNTIL|NEXT|ENDFUNCTION|ENDPROCEDURE|ENDTYPE|CATCH|ENDTRY)\b/.test( - codeOnly(document.lineAt(position.line).text))) return []; + if (!shouldFormatOnType(document.lineAt(position.line).text, char)) return []; return formatLines(document, position.line, position.line, char === '\n'); } - }, '\n', 'E', 'F', 'L', 'T', 'N', 'H', 'Y') + }, ...onTypeCharacters) ); refreshBrackets(); } diff --git a/plugin/src/indentation.js b/plugin/src/indentation.js index b7fca78..fd3f585 100644 --- a/plugin/src/indentation.js +++ b/plugin/src/indentation.js @@ -1,6 +1,15 @@ 'use strict'; const { codeOnly } = require('./lexical'); +const onTypeCharacters = ['\n', 'E', 'F', 'L', 'T', 'N', 'H', 'Y']; + +function shouldFormatOnType(line, character) { + return character === '\n' || /^\s*(?:ELSE|ENDIF|ENDWHILE|UNTIL|NEXT|ENDFUNCTION|ENDPROCEDURE|ENDTYPE|CATCH|ENDTRY)\b/.test(codeOnly(line)); +} + +function selectionEndLine(startLine, endLine, endColumn) { + return endColumn === 0 && endLine > startLine ? endLine - 1 : endLine; +} const closers = { ENDIF: 'IF', ENDWHILE: 'WHILE', UNTIL: 'REPEAT', NEXT: 'FOR', ENDFUNCTION: 'FUNCTION', ENDPROCEDURE: 'PROCEDURE', ENDTYPE: 'TYPE', ENDTRY: 'TRY' @@ -56,4 +65,4 @@ function indentationEdits(lines, startLine, endLine, keepBlankIndent = false) { return edits; } -module.exports = { lineDepths, indentationEdits }; +module.exports = { lineDepths, indentationEdits, onTypeCharacters, shouldFormatOnType, selectionEndLine }; -- cgit v1.3.1-4-g156e