@@ -15,7 +15,7 @@ import * as utils from "./utils";
15
15
import * as c from "./constants" ;
16
16
import * as chokidar from "chokidar" ;
17
17
import { assert } from "console" ;
18
- import { fileURLToPath } from "url" ;
18
+ import { fileURLToPath , pathToFileURL } from "url" ;
19
19
import { ChildProcess } from "child_process" ;
20
20
import { WorkspaceEdit } from "vscode-languageserver" ;
21
21
import { TextEdit } from "vscode-languageserver-types" ;
@@ -294,16 +294,34 @@ function rename(msg: p.RequestMessage) {
294
294
if ( locations === null ) {
295
295
result = null ;
296
296
} else {
297
- let changes : { [ uri : string ] : TextEdit [ ] } = { } ;
297
+ let textEdits : { [ uri : string ] : TextEdit [ ] } = { } ;
298
+ let documentChanges : ( p . RenameFile | p . TextDocumentEdit ) [ ] = [ ] ;
299
+
298
300
locations . forEach ( ( { uri, range } ) => {
299
- let textEdit : TextEdit = { range, newText : params . newName } ;
300
- if ( uri in changes ) {
301
- changes [ uri ] . push ( textEdit ) ;
301
+ if ( utils . isRangeTopOfFile ( range ) ) {
302
+ let filePath = fileURLToPath ( uri ) ;
303
+ let newFilePath = `${ path . dirname ( filePath ) } /${ params . newName } ${ path . extname ( filePath ) } ` ;
304
+ let newUri = pathToFileURL ( newFilePath ) . href ;
305
+ let rename : p . RenameFile = { kind : "rename" , oldUri : uri , newUri } ;
306
+ documentChanges . push ( rename ) ;
302
307
} else {
303
- changes [ uri ] = [ textEdit ] ;
308
+ let textEdit : TextEdit = { range, newText : params . newName } ;
309
+ if ( uri in textEdits ) {
310
+ textEdits [ uri ] . push ( textEdit ) ;
311
+ } else {
312
+ textEdits [ uri ] = [ textEdit ] ;
313
+ }
304
314
}
305
315
} ) ;
306
- result = { changes } ;
316
+
317
+ Object . entries ( textEdits )
318
+ . forEach ( ( [ uri , edits ] ) => {
319
+ let textDocumentEdit = { textDocument : { uri, version : null } , edits } ;
320
+ documentChanges . push ( textDocumentEdit ) ;
321
+ } ) ;
322
+
323
+
324
+ result = { documentChanges } ;
307
325
}
308
326
let response : m . ResponseMessage = {
309
327
jsonrpc : c . jsonrpcVersion ,
0 commit comments