Skip to content

Commit bd06877

Browse files
committed
Add rename provider
1 parent d9aa929 commit bd06877

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

server/src/server.ts

+41-1
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.
@@ -307,6 +308,7 @@ function onMessage(msg: m.Message) {
307308
hoverProvider: true,
308309
definitionProvider: true,
309310
referencesProvider: true,
311+
renameProvider: true,
310312
documentSymbolProvider: false,
311313
completionProvider: { triggerCharacters: [".", ">", "@", "~"] },
312314
},
@@ -386,6 +388,44 @@ function onMessage(msg: m.Message) {
386388
// error: code and message set in case an exception happens during the definition request.
387389
};
388390
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);
389429
} else if (msg.method === p.ReferencesRequest.method) {
390430
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
391431
let params = msg.params as p.ReferenceParams;

0 commit comments

Comments
 (0)