Skip to content

Commit 9f87be5

Browse files
fix: keep selection on formatting (#372)
1 parent 8b130a8 commit 9f87be5

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

src/DocumentWatcher.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
TextEditorOptions,
99
window,
1010
workspace,
11+
WorkspaceEdit,
1112
} from 'vscode'
1213
import {
1314
InsertFinalNewline,
@@ -75,18 +76,20 @@ export default class DocumentWatcher {
7576
const activeEditor = window.activeTextEditor
7677
const activeDoc = activeEditor?.document
7778
if (activeDoc && activeDoc === e.document && activeEditor) {
78-
selections = activeEditor.selections
79+
selections = [...activeEditor.selections]
7980
}
8081
const transformations = this.calculatePreSaveTransformations(
8182
e.document,
8283
e.reason,
8384
)
84-
e.waitUntil(transformations)
85-
if (selections.length) {
86-
const edits = await transformations
87-
if (activeEditor && edits.length) {
88-
activeEditor.selections = selections
89-
}
85+
86+
const workspaceEdit = new WorkspaceEdit()
87+
const textEdits = await transformations
88+
workspaceEdit.set(e.document.uri, textEdits)
89+
await workspace.applyEdit(workspaceEdit)
90+
91+
if (activeEditor && textEdits.length && selections.length) {
92+
activeEditor.selections = selections
9093
}
9194
}),
9295
)

src/test/suite/index.test.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as assert from 'assert'
22
import * as os from 'os'
3-
import { Position, window, workspace, WorkspaceEdit } from 'vscode'
3+
import { Position, window, workspace, WorkspaceEdit, Uri } from 'vscode'
44
import { getFixturePath, getOptionsForFixture, wait } from '../testUtils'
55

66
import * as utils from 'vscode-test-utils'
@@ -284,22 +284,42 @@ suite('EditorConfig extension', function () {
284284
`editor has insertSpaces: ${options.insertSpaces}`,
285285
)
286286
})
287+
288+
test('keep selection on format', async () => {
289+
await withSetting('insert_final_newline', 'true', {
290+
fileName: 'test-selection',
291+
}).saveText('foobar')
292+
assert(window.activeTextEditor, 'no active editor')
293+
294+
// Before saving, the selection is on line 0. This should remain unchanged.
295+
assert.strictEqual(
296+
window.activeTextEditor.selection.start.line,
297+
0,
298+
'editor selection start line changed',
299+
)
300+
assert.strictEqual(
301+
window.activeTextEditor.selection.end.line,
302+
0,
303+
'editor selection end line changed',
304+
)
305+
})
287306
})
288307

289308
function withSetting(
290309
rule: string,
291310
value: string,
292311
options: {
293312
contents?: string
313+
fileName?: string
294314
} = {},
295315
) {
296316
return {
297317
async getText() {
298-
return (await createDoc(options.contents)).getText()
318+
return (await createDoc(options.contents, options.fileName)).getText()
299319
},
300320
saveText(text: string) {
301321
return new Promise<string>(async resolve => {
302-
const doc = await createDoc(options.contents)
322+
const doc = await createDoc(options.contents, options.fileName)
303323
workspace.onDidChangeTextDocument(doc.save)
304324
workspace.onDidSaveTextDocument(savedDoc => {
305325
assert.strictEqual(savedDoc.isDirty, false, 'dirty saved doc')
@@ -315,12 +335,16 @@ function withSetting(
315335
})
316336
},
317337
}
338+
async function createDoc(contents = '', name = 'test') {
339+
const fixturePath = getFixturePath([rule, value, name])
318340

319-
async function createDoc(contents = '') {
320-
const uri = await utils.createFile(
321-
contents,
322-
getFixturePath([rule, value, 'test']),
323-
)
341+
try {
342+
await workspace.fs.delete(Uri.file(fixturePath))
343+
} catch {
344+
// ignore
345+
}
346+
347+
const uri = await utils.createFile(contents, fixturePath)
324348
const doc = await workspace.openTextDocument(uri)
325349
await window.showTextDocument(doc)
326350
await wait(50) // wait for EditorConfig to apply new settings

0 commit comments

Comments
 (0)