Skip to content

Add rename provider #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import * as chokidar from "chokidar";
import { assert } from "console";
import { fileURLToPath } from "url";
import { ChildProcess } from "child_process";
import { Location } from "vscode-languageserver";
import { SymbolInformation } from "vscode-languageserver";
import { WorkspaceEdit } from "vscode-languageserver";
import { TextEdit } from "vscode-languageserver-types";

// https://microsoft.github.io/language-server-protocol/specification#initialize
// According to the spec, there could be requests before the 'initialize' request. Link in comment tells how to handle them.
Expand Down Expand Up @@ -307,6 +307,7 @@ function onMessage(msg: m.Message) {
hoverProvider: true,
definitionProvider: true,
referencesProvider: true,
renameProvider: true,
documentSymbolProvider: false,
completionProvider: { triggerCharacters: [".", ">", "@", "~"] },
},
Expand Down Expand Up @@ -386,19 +387,40 @@ function onMessage(msg: m.Message) {
// error: code and message set in case an exception happens during the definition request.
};
send(definitionResponse);
} else if (msg.method === p.RenameRequest.method) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
let params = msg.params as p.RenameParams;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bit of code duplication with the rename command.
Maybe it makes sense to move out functionality that could lead to errors in future. E.g. definitely the way the references command is called, which parameters, etc. So that a future edit does not change only one place out of two, by mistake.

let filePath = fileURLToPath(params.textDocument.uri);
let locations: p.Location[] | null = utils.getReferencesForPosition(filePath, params.position);
let result: WorkspaceEdit | null;
if (locations === null) {
result = null;
} else {
let changes: { [uri: string]: TextEdit[] } = {};
locations.forEach(({ uri, range }) => {
let textEdit: TextEdit = {range, newText: params.newName};
if (uri in changes) {
changes[uri].push(textEdit);
} else {
changes[uri] = [textEdit]
}
});

result = {changes};
}

let renameResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
result,
};

send(renameResponse);
} else if (msg.method === p.ReferencesRequest.method) {
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_references
let params = msg.params as p.ReferenceParams;
let filePath = fileURLToPath(params.textDocument.uri);
let result: typeof p.ReferencesRequest.type = utils.runAnalysisAfterSanityCheck(
filePath,
[
"references",
filePath,
params.position.line,
params.position.character,
]
);
let result: typeof p.ReferencesRequest.type = utils.getReferencesForPosition(filePath, params.position);
let definitionResponse: m.ResponseMessage = {
jsonrpc: c.jsonrpcVersion,
id: msg.id,
Expand Down
3 changes: 3 additions & 0 deletions server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export let runAnalysisAfterSanityCheck = (
return JSON.parse(stdout.toString());
};

export let getReferencesForPosition = (filePath: p.DocumentUri, position: p.Position) =>
runAnalysisAfterSanityCheck(filePath, ['references', filePath, position.line, position.character]);

export let replaceFileExtension = (filePath: string, ext: string): string => {
let name = path.basename(filePath, path.extname(filePath));
return path.format({ dir: path.dirname(filePath), name, ext })
Expand Down