Skip to content

Commit

Permalink
fix: restore selections *after* the file save
Browse files Browse the repository at this point in the history
  • Loading branch information
SunsetTechuila committed Feb 8, 2025
1 parent ec849f9 commit 3eb313b
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/DocumentWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
TextEditorOptions,
window,
workspace,
WorkspaceEdit,
} from 'vscode'
import {
InsertFinalNewline,
Expand Down Expand Up @@ -40,6 +39,8 @@ export default class DocumentWatcher {

const subscriptions: Disposable[] = []

let previousSelections: Selection[] = []

this.handleTextEditorChange(window.activeTextEditor)

subscriptions.push(
Expand Down Expand Up @@ -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)
}),
)

Expand Down

0 comments on commit 3eb313b

Please sign in to comment.