Skip to content

Commit

Permalink
fix: restore selections only after applied changes
Browse files Browse the repository at this point in the history
  • Loading branch information
SunsetTechuila committed Feb 17, 2025
1 parent 0d3042d commit 60249a0
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/DocumentWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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') {
Expand All @@ -80,14 +91,16 @@ 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(
e.document,
e.reason,
)
e.waitUntil(transformations)
shouldRestoreSelection = (await transformations).length > 0
editedDoc = activeDoc
}),
)

Expand Down

0 comments on commit 60249a0

Please sign in to comment.