Skip to content

Commit

Permalink
fix: restore selections *after* the file save (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
SunsetTechuila authored Feb 8, 2025
1 parent ec849f9 commit 4c8b3a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 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 All @@ -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.')
}
Expand All @@ -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)
}),
)

Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 4c8b3a0

Please sign in to comment.