From fbc07d6f13bf3e318167349c5fba7ec964d8fe9c Mon Sep 17 00:00:00 2001 From: Phill Wolf Date: Wed, 1 Jan 2025 12:17:53 -0500 Subject: [PATCH 1/2] Update cursor context upon TextDocumentChangeEvent, but not redundantly with TextEditorSelectionChangeEvent --- src/extension.ts | 60 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 368eb0f93..bdbb63fc5 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -48,7 +48,6 @@ import * as inspector from './providers/inspector'; function onDidChangeEditorOrSelection(editor: vscode.TextEditor) { replHistory.setReplHistoryCommandsActiveContext(editor); - whenContexts.setCursorContextIfChanged(editor); } function setKeybindingsEnabledContext() { @@ -420,6 +419,54 @@ async function activate(context: vscode.ExtensionContext) { Object.entries(languageProviders).forEach(registerLangProvider); + // Coordinate event handling that may affect the 'when' cursor contexts. + // Update context upon editor change, selection change, or text change + // (e.g., upon deleting a comment-defining semicolon without moving the point) + // But try not to repeatedly update context in response to the same essential event + // (e.g., most text changes, which also move the point) + // without depending on the order of TextDocumentChangeEvent or TextEditorSelectionChangeEvent + let contextSettingEditor: vscode.TextEditor = undefined; + let contextSettingCircumstances = undefined; + function contextSettingOnChangeActiveTextEditor(editor: vscode.TextEditor) { + whenContexts.setCursorContextIfChanged(editor); + const circumstances = { + version: editor.document.version, + active: editor.selection.active, + }; + contextSettingEditor = editor; + contextSettingCircumstances = circumstances; + } + function contextSettingOnTextDocumentChangeEvent(dce: vscode.TextDocumentChangeEvent) { + if (contextSettingEditor) { + const circumstances = { + version: dce.document.version, + active: contextSettingEditor.selection.active, + }; + if ( + !( + (contextSettingEditor && dce.document !== contextSettingEditor.document) || + circumstances == contextSettingCircumstances + ) + ) { + whenContexts.setCursorContextIfChanged(contextSettingEditor); + contextSettingCircumstances = circumstances; + } + } + } + function contextSettingOnChangeTextEditorSelection(tsce: vscode.TextEditorSelectionChangeEvent) { + const circumstances = { + version: tsce.textEditor.document.version, + active: tsce.selections[0].active, + }; + if ( + !(contextSettingEditor === tsce.textEditor && circumstances == contextSettingCircumstances) + ) { + whenContexts.setCursorContextIfChanged(tsce.textEditor); + contextSettingEditor = tsce.textEditor; + contextSettingCircumstances = circumstances; + } + } + //EVENTS const onDidEvents = { workspace: { @@ -441,7 +488,10 @@ async function activate(context: vscode.ExtensionContext) { void testRunner.runNamespaceTests(testController, document); } }, - changeTextDocument: annotations.onDidChangeTextDocument, + changeTextDocument: (e: vscode.TextDocumentChangeEvent) => { + annotations.onDidChangeTextDocument(e); + contextSettingOnTextDocumentChangeEvent(e); + }, closeTextDocument: (document) => { if (outputWindow.isResultsDoc(document)) { outputWindow.setContextForReplWindowActive(false); @@ -458,8 +508,12 @@ async function activate(context: vscode.ExtensionContext) { changeActiveTextEditor: (editor) => { status.update(); onDidChangeEditorOrSelection(editor); + contextSettingOnChangeActiveTextEditor(editor); + }, + changeTextEditorSelection: (event) => { + onDidChangeEditorOrSelection(event.textEditor); + contextSettingOnChangeTextEditorSelection(event); }, - changeTextEditorSelection: (event) => onDidChangeEditorOrSelection(event.textEditor), changeVisibleTextEditors: (editors) => { if (!editors.some((editor) => outputWindow.isResultsDoc(editor.document))) { outputWindow.setContextForReplWindowActive(false); From aa0713fb08194da9c5a9c5fbd47a937f58c67756 Mon Sep 17 00:00:00 2001 From: Phill Wolf Date: Wed, 1 Jan 2025 12:21:31 -0500 Subject: [PATCH 2/2] Del key, after emptying a comment line, then imbalances the next form --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9b74cce7..3986beb5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Changes to Calva. ## [Unreleased] +- Fix: [Del key, after emptying a comment line, then imbalances the next form](https://github.com/BetterThanTomorrow/calva/issues/2686) + ## [2.0.482] - 2024-12-03 - Fix: [Added 'replace-refer-all-with-alias' & 'replace-refer-all-with-refer' actions to calva.](https://github.com/BetterThanTomorrow/calva/issues/2667)