@@ -20,7 +20,8 @@ import { assert } from "console";
20
20
import { fileURLToPath } from "url" ;
21
21
import { ChildProcess } from "child_process" ;
22
22
import { Location } from "vscode-languageserver" ;
23
- import { SymbolInformation } from "vscode-languageserver" ;
23
+ import { SymbolInformation , WorkspaceEdit } from "vscode-languageserver" ;
24
+ import { TextEdit } from "vscode-languageserver-types" ;
24
25
25
26
// https://microsoft.github.io/language-server-protocol/specification#initialize
26
27
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
@@ -307,6 +308,7 @@ function onMessage(msg: m.Message) {
307
308
hoverProvider : true ,
308
309
definitionProvider : true ,
309
310
referencesProvider : true ,
311
+ renameProvider : true ,
310
312
documentSymbolProvider : false ,
311
313
completionProvider : { triggerCharacters : [ "." , ">" , "@" , "~" ] } ,
312
314
} ,
@@ -386,6 +388,44 @@ function onMessage(msg: m.Message) {
386
388
// error: code and message set in case an exception happens during the definition request.
387
389
} ;
388
390
send ( definitionResponse ) ;
391
+ } else if ( msg . method === p . RenameRequest . method ) {
392
+ // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
393
+ let params = msg . params as p . RenameParams ;
394
+ let filePath = fileURLToPath ( params . textDocument . uri ) ;
395
+ let locations : Location [ ] | null = utils . runAnalysisAfterSanityCheck (
396
+ filePath ,
397
+ [
398
+ "references" ,
399
+ filePath ,
400
+ params . position . line ,
401
+ params . position . character ,
402
+ ]
403
+ ) ;
404
+
405
+ let result : WorkspaceEdit | null ;
406
+ if ( locations === null ) {
407
+ result = null ;
408
+ } else {
409
+ let changes : { [ uri : string ] : TextEdit [ ] } = { } ;
410
+ locations . forEach ( ( { uri, range } ) => {
411
+ let textEdit : TextEdit = { range, newText : params . newName } ;
412
+ if ( uri in changes ) {
413
+ changes [ uri ] . push ( textEdit ) ;
414
+ } else {
415
+ changes [ uri ] = [ textEdit ]
416
+ }
417
+ } ) ;
418
+
419
+ result = { changes} ;
420
+ }
421
+
422
+ let renameResponse : m . ResponseMessage = {
423
+ jsonrpc : c . jsonrpcVersion ,
424
+ id : msg . id ,
425
+ result,
426
+ } ;
427
+
428
+ send ( renameResponse ) ;
389
429
} else if ( msg . method === p . ReferencesRequest . method ) {
390
430
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
391
431
let params = msg . params as p . ReferenceParams ;
0 commit comments