From 4c8b3a0d7ec1e4b1eec08f8f514e924288a97aed Mon Sep 17 00:00:00 2001 From: Grigory Date: Sat, 8 Feb 2025 20:16:08 +0500 Subject: [PATCH] fix: restore selections *after* the file save (#397) --- src/DocumentWatcher.ts | 21 +++++++++------------ src/test/suite/index.test.ts | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/DocumentWatcher.ts b/src/DocumentWatcher.ts index 3bb7907..e48585a 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( @@ -64,6 +65,11 @@ export default class DocumentWatcher { subscriptions.push( workspace.onDidSaveTextDocument(doc => { + const activeEditor = window.activeTextEditor + if (activeEditor && previousSelections.length) { + activeEditor.selections = previousSelections + } + if (path.basename(doc.fileName) === '.editorconfig') { this.log('.editorconfig file saved.') } @@ -72,25 +78,16 @@ export default class DocumentWatcher { 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) }), ) 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 } }