From 3eb313b8f02317b3b7a6b70d9fd8e614efd5923a Mon Sep 17 00:00:00 2001 From: Grigory Date: Sat, 8 Feb 2025 19:32:20 +0500 Subject: [PATCH] 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) }), )