@@ -282,6 +282,48 @@ function references(msg: p.RequestMessage) {
282
282
return response ;
283
283
}
284
284
285
+ function prepareRename ( msg : p . RequestMessage ) {
286
+ // https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_prepareRename
287
+ let params = msg . params as p . PrepareRenameParams ;
288
+ let filePath = fileURLToPath ( params . textDocument . uri ) ;
289
+ let locations : null | p . Location [ ] = utils . getReferencesForPosition (
290
+ filePath ,
291
+ params . position
292
+ ) ;
293
+
294
+ let result : p . Range | null = null ;
295
+
296
+ if ( locations !== null && locations . length > 0 ) {
297
+ let targetLoc = locations . find ( loc => {
298
+ if (
299
+ path . normalize ( fileURLToPath ( loc . uri ) ) ===
300
+ path . normalize ( fileURLToPath ( params . textDocument . uri ) )
301
+ ) {
302
+ let { start, end } = loc . range ;
303
+ let pos = params . position ;
304
+
305
+ return (
306
+ start . character <= pos . character &&
307
+ start . line <= pos . line &&
308
+ end . character >= pos . character &&
309
+ end . line >= pos . line
310
+ ) ;
311
+ }
312
+ } ) ;
313
+
314
+ if ( targetLoc != null ) {
315
+ result = targetLoc . range ;
316
+ }
317
+ }
318
+
319
+ let response : m . ResponseMessage = {
320
+ jsonrpc : c . jsonrpcVersion ,
321
+ id : msg . id ,
322
+ result
323
+ } ;
324
+ return response ;
325
+ }
326
+
285
327
function rename ( msg : p . RequestMessage ) {
286
328
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
287
329
let params = msg . params as p . RenameParams ;
@@ -591,7 +633,7 @@ function onMessage(msg: m.Message) {
591
633
hoverProvider : true ,
592
634
definitionProvider : true ,
593
635
referencesProvider : true ,
594
- renameProvider : true ,
636
+ renameProvider : { prepareProvider : true } ,
595
637
documentSymbolProvider : false ,
596
638
completionProvider : { triggerCharacters : [ "." , ">" , "@" , "~" ] } ,
597
639
} ,
@@ -642,6 +684,8 @@ function onMessage(msg: m.Message) {
642
684
send ( definition ( msg ) ) ;
643
685
} else if ( msg . method === p . ReferencesRequest . method ) {
644
686
send ( references ( msg ) ) ;
687
+ } else if ( msg . method === p . PrepareRenameRequest . method ) {
688
+ send ( prepareRename ( msg ) ) ;
645
689
} else if ( msg . method === p . RenameRequest . method ) {
646
690
send ( rename ( msg ) ) ;
647
691
} else if ( msg . method === p . DocumentSymbolRequest . method ) {
0 commit comments