Skip to content

Commit 5d5c9db

Browse files
committed
Add prepare rename provider
1 parent bc5e9d0 commit 5d5c9db

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

server/src/server.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,48 @@ function references(msg: p.RequestMessage) {
282282
return response;
283283
}
284284

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+
285327
function rename(msg: p.RequestMessage) {
286328
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
287329
let params = msg.params as p.RenameParams;
@@ -591,7 +633,7 @@ function onMessage(msg: m.Message) {
591633
hoverProvider: true,
592634
definitionProvider: true,
593635
referencesProvider: true,
594-
renameProvider: true,
636+
renameProvider: { prepareProvider: true },
595637
documentSymbolProvider: false,
596638
completionProvider: { triggerCharacters: [".", ">", "@", "~"] },
597639
},
@@ -642,6 +684,8 @@ function onMessage(msg: m.Message) {
642684
send(definition(msg));
643685
} else if (msg.method === p.ReferencesRequest.method) {
644686
send(references(msg));
687+
} else if (msg.method === p.PrepareRenameRequest.method) {
688+
send(prepareRename(msg));
645689
} else if (msg.method === p.RenameRequest.method) {
646690
send(rename(msg));
647691
} else if (msg.method === p.DocumentSymbolRequest.method) {

0 commit comments

Comments
 (0)