@@ -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.
@@ -303,6 +304,7 @@ function onMessage(msg: m.Message) {
303
304
hoverProvider : true ,
304
305
definitionProvider : true ,
305
306
referencesProvider : true ,
307
+ renameProvider : true ,
306
308
documentSymbolProvider : false ,
307
309
completionProvider : { triggerCharacters : [ "." , ">" , "@" , "~" ] } ,
308
310
} ,
@@ -383,11 +385,49 @@ function onMessage(msg: m.Message) {
383
385
// error: code and message set in case an exception happens during the definition request.
384
386
} ;
385
387
send ( definitionResponse ) ;
388
+ } else if ( msg . method === p . RenameRequest . method ) {
389
+ // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
390
+ let params = msg . params as p . RenameParams ;
391
+ let filePath = fileURLToPath ( params . textDocument . uri ) ;
392
+ let locations : Location [ ] | null = utils . runAnalysisAfterSanityCheck (
393
+ filePath ,
394
+ [
395
+ "references" ,
396
+ filePath ,
397
+ params . position . line ,
398
+ params . position . character ,
399
+ ]
400
+ ) ;
401
+
402
+ let result : WorkspaceEdit | null ;
403
+ if ( locations === null ) {
404
+ result = null ;
405
+ } else {
406
+ let changes : { [ uri : string ] : TextEdit [ ] } = { } ;
407
+ locations . forEach ( ( { uri, range } ) => {
408
+ let textEdit : TextEdit = { range, newText : params . newName } ;
409
+ if ( uri in changes ) {
410
+ changes [ uri ] . push ( textEdit ) ;
411
+ } else {
412
+ changes [ uri ] = [ textEdit ]
413
+ }
414
+ } ) ;
415
+
416
+ result = { changes} ;
417
+ }
418
+
419
+ let renameResponse : m . ResponseMessage = {
420
+ jsonrpc : c . jsonrpcVersion ,
421
+ id : msg . id ,
422
+ result,
423
+ } ;
424
+
425
+ send ( renameResponse ) ;
386
426
} else if ( msg . method === p . ReferencesRequest . method ) {
387
427
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
388
428
let params = msg . params as p . ReferenceParams ;
389
429
let filePath = fileURLToPath ( params . textDocument . uri ) ;
390
- let result : Location | null = utils . runAnalysisAfterSanityCheck (
430
+ let result : Location [ ] | null = utils . runAnalysisAfterSanityCheck (
391
431
filePath ,
392
432
[
393
433
"references" ,
0 commit comments