Skip to content

Commit 1ebda14

Browse files
fix: use insertFinalNewLine command (#428)
1 parent c7c4380 commit 1ebda14

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

src/test/suite/index.test.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -285,24 +285,24 @@ suite('EditorConfig extension', function () {
285285
)
286286
})
287287

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')
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')
293293

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-
// })
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+
})
306306
})
307307

308308
function withSetting(

src/transformations/InsertFinalNewline.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { KnownProps } from 'editorconfig'
2-
import { Position, TextDocument, TextEdit } from 'vscode'
2+
import {
3+
commands,
4+
Position,
5+
TextDocument,
6+
TextEdit,
7+
window,
8+
} from 'vscode'
39

410
import { PreSaveTransformation } from './PreSaveTransformation'
511

@@ -24,6 +30,14 @@ export class InsertFinalNewline extends PreSaveTransformation {
2430
return { edits: [] }
2531
}
2632

33+
if (window.activeTextEditor && window.activeTextEditor.document === doc) {
34+
commands.executeCommand('editor.action.insertFinalNewLine')
35+
return {
36+
edits: [],
37+
message: 'editor.action.insertFinalNewLine',
38+
}
39+
}
40+
2741
const position = new Position(lastLine.lineNumber, lastLine.text.length)
2842

2943
const eol = (editorconfigProperties.end_of_line ?? 'lf').toUpperCase()

0 commit comments

Comments
 (0)