11import * as assert from 'assert'
22import * as os from 'os'
3- import { Position , window , workspace , WorkspaceEdit } from 'vscode'
3+ import { Position , window , workspace , WorkspaceEdit , Uri } from 'vscode'
44import { getFixturePath , getOptionsForFixture , wait } from '../testUtils'
55
66import * 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
289308function 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