From 3eb313b8f02317b3b7a6b70d9fd8e614efd5923a Mon Sep 17 00:00:00 2001 From: Grigory Date: Sat, 8 Feb 2025 19:32:20 +0500 Subject: [PATCH 1/3] fix: restore selections *after* the file save --- src/DocumentWatcher.ts | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/DocumentWatcher.ts b/src/DocumentWatcher.ts index 3bb7907..d682a7e 100644 --- a/src/DocumentWatcher.ts +++ b/src/DocumentWatcher.ts @@ -8,7 +8,6 @@ import { TextEditorOptions, window, workspace, - WorkspaceEdit, } from 'vscode' import { InsertFinalNewline, @@ -40,6 +39,8 @@ export default class DocumentWatcher { const subscriptions: Disposable[] = [] + let previousSelections: Selection[] = [] + this.handleTextEditorChange(window.activeTextEditor) subscriptions.push( @@ -67,30 +68,26 @@ export default class DocumentWatcher { if (path.basename(doc.fileName) === '.editorconfig') { this.log('.editorconfig file saved.') } + + const activeEditor = window.activeTextEditor + if (activeEditor && previousSelections.length) { + activeEditor.selections = previousSelections + } }), ) subscriptions.push( workspace.onWillSaveTextDocument(async e => { - let selections: readonly Selection[] = [] const activeEditor = window.activeTextEditor const activeDoc = activeEditor?.document if (activeDoc && activeDoc === e.document && activeEditor) { - selections = [...activeEditor.selections] + previousSelections = [...activeEditor.selections] } const transformations = this.calculatePreSaveTransformations( e.document, e.reason, ) - - const workspaceEdit = new WorkspaceEdit() - const textEdits = await transformations - workspaceEdit.set(e.document.uri, textEdits) - await workspace.applyEdit(workspaceEdit) - - if (activeEditor && textEdits.length && selections.length) { - activeEditor.selections = selections - } + e.waitUntil(transformations) }), ) From 92bfe6e2e1085321fb533466502ca92750a4b111 Mon Sep 17 00:00:00 2001 From: Grigory Date: Sat, 8 Feb 2025 19:37:40 +0500 Subject: [PATCH 2/3] test: return to the 50ms timer --- src/test/suite/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/suite/index.test.ts b/src/test/suite/index.test.ts index 63dd883..a3e1a7f 100644 --- a/src/test/suite/index.test.ts +++ b/src/test/suite/index.test.ts @@ -342,7 +342,7 @@ function withSetting( ) const doc = await workspace.openTextDocument(uri) await window.showTextDocument(doc) - await wait(100) // wait for EditorConfig to apply new settings + await wait(50) // wait for EditorConfig to apply new settings return doc } } From b213ca1b40aeab30e6c184779af8e0be6e45a8f2 Mon Sep 17 00:00:00 2001 From: Grigory Date: Sat, 8 Feb 2025 19:55:18 +0500 Subject: [PATCH 3/3] restore selection first --- src/DocumentWatcher.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/DocumentWatcher.ts b/src/DocumentWatcher.ts index d682a7e..e48585a 100644 --- a/src/DocumentWatcher.ts +++ b/src/DocumentWatcher.ts @@ -65,14 +65,14 @@ export default class DocumentWatcher { subscriptions.push( workspace.onDidSaveTextDocument(doc => { - if (path.basename(doc.fileName) === '.editorconfig') { - this.log('.editorconfig file saved.') - } - const activeEditor = window.activeTextEditor if (activeEditor && previousSelections.length) { activeEditor.selections = previousSelections } + + if (path.basename(doc.fileName) === '.editorconfig') { + this.log('.editorconfig file saved.') + } }), )