From 60249a05d9fc056f8c4c60a2b894bcfb186d3268 Mon Sep 17 00:00:00 2001 From: Grigory Date: Wed, 12 Feb 2025 03:28:49 +0500 Subject: [PATCH] fix: restore selections only after applied changes --- src/DocumentWatcher.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/DocumentWatcher.ts b/src/DocumentWatcher.ts index e48585a..cf89602 100644 --- a/src/DocumentWatcher.ts +++ b/src/DocumentWatcher.ts @@ -39,6 +39,10 @@ export default class DocumentWatcher { const subscriptions: Disposable[] = [] + let editedDoc: TextDocument | undefined + + let shouldRestoreSelection = false + let previousSelections: Selection[] = [] this.handleTextEditorChange(window.activeTextEditor) @@ -66,8 +70,15 @@ export default class DocumentWatcher { subscriptions.push( workspace.onDidSaveTextDocument(doc => { const activeEditor = window.activeTextEditor - if (activeEditor && previousSelections.length) { - activeEditor.selections = previousSelections + const activeDoc = activeEditor?.document + if ( + shouldRestoreSelection && + activeEditor && + doc === editedDoc && + doc === activeDoc + ) { + activeEditor.selections = [...previousSelections] + shouldRestoreSelection = false } if (path.basename(doc.fileName) === '.editorconfig') { @@ -80,7 +91,7 @@ export default class DocumentWatcher { workspace.onWillSaveTextDocument(async e => { const activeEditor = window.activeTextEditor const activeDoc = activeEditor?.document - if (activeDoc && activeDoc === e.document && activeEditor) { + if (activeEditor && activeDoc === e.document) { previousSelections = [...activeEditor.selections] } const transformations = this.calculatePreSaveTransformations( @@ -88,6 +99,8 @@ export default class DocumentWatcher { e.reason, ) e.waitUntil(transformations) + shouldRestoreSelection = (await transformations).length > 0 + editedDoc = activeDoc }), )