Skip to content

Commit aadc95f

Browse files
committed
Add rename provider
1 parent ee6e843 commit aadc95f

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

server/src/server.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import { assert } from "console";
2020
import { fileURLToPath } from "url";
2121
import { ChildProcess } from "child_process";
2222
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";
2425

2526
// https://microsoft.github.io/language-server-protocol/specification#initialize
2627
// 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) {
303304
hoverProvider: true,
304305
definitionProvider: true,
305306
referencesProvider: true,
307+
renameProvider: true,
306308
documentSymbolProvider: false,
307309
completionProvider: { triggerCharacters: [".", ">", "@", "~"] },
308310
},
@@ -383,11 +385,49 @@ function onMessage(msg: m.Message) {
383385
// error: code and message set in case an exception happens during the definition request.
384386
};
385387
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);
386426
} else if (msg.method === p.ReferencesRequest.method) {
387427
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
388428
let params = msg.params as p.ReferenceParams;
389429
let filePath = fileURLToPath(params.textDocument.uri);
390-
let result: Location | null = utils.runAnalysisAfterSanityCheck(
430+
let result: Location[] | null = utils.runAnalysisAfterSanityCheck(
391431
filePath,
392432
[
393433
"references",

0 commit comments

Comments
 (0)