Skip to content

Commit c9aab8a

Browse files
amiraliescristianoc
authored andcommitted
Update server to use rename command
1 parent 452985f commit c9aab8a

File tree

2 files changed

+13
-41
lines changed

2 files changed

+13
-41
lines changed

server/src/server.ts

+13-33
Original file line numberDiff line numberDiff line change
@@ -286,48 +286,28 @@ function rename(msg: p.RequestMessage) {
286286
// https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_rename
287287
let params = msg.params as p.RenameParams;
288288
let filePath = fileURLToPath(params.textDocument.uri);
289-
let locations: p.Location[] | null = utils.getReferencesForPosition(
289+
let documentChanges:
290+
| (p.RenameFile | p.TextDocumentEdit)[]
291+
| null = utils.runAnalysisAfterSanityCheck(filePath, [
292+
"rename",
290293
filePath,
291-
params.position
292-
);
293-
let result: WorkspaceEdit | null;
294-
if (locations === null) {
295-
result = null;
296-
} else {
297-
let textEdits: { [uri: string]: TextEdit[] } = {};
298-
let documentChanges: (p.RenameFile | p.TextDocumentEdit)[] = [];
299-
300-
locations.forEach(({ uri, range }) => {
301-
if (utils.isRangeTopOfFile(range)) {
302-
let filePath = fileURLToPath(uri);
303-
let newFilePath = `${path.dirname(filePath)}/${params.newName}${path.extname(filePath)}`;
304-
let newUri = pathToFileURL(newFilePath).href;
305-
let rename: p.RenameFile = { kind: "rename", oldUri: uri, newUri };
306-
documentChanges.push(rename);
307-
} else {
308-
let textEdit: TextEdit = { range, newText: params.newName };
309-
if (uri in textEdits) {
310-
textEdits[uri].push(textEdit);
311-
} else {
312-
textEdits[uri] = [textEdit];
313-
}
314-
}
315-
});
316-
317-
Object.entries(textEdits)
318-
.forEach(([uri, edits]) => {
319-
let textDocumentEdit = { textDocument: { uri, version: null }, edits };
320-
documentChanges.push(textDocumentEdit);
321-
});
294+
params.position.line,
295+
params.position.character,
296+
params.newName
297+
]);
322298

299+
let result: WorkspaceEdit | null = null;
323300

301+
if (documentChanges !== null) {
324302
result = { documentChanges };
325303
}
304+
326305
let response: m.ResponseMessage = {
327306
jsonrpc: c.jsonrpcVersion,
328307
id: msg.id,
329-
result,
308+
result
330309
};
310+
331311
return response;
332312
}
333313

server/src/utils.ts

-8
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,3 @@ export let parseCompilerLogOutput = (
476476

477477
return { done, result };
478478
};
479-
480-
export let isRangeTopOfFile = (range: p.Range) =>
481-
[
482-
range.start.character,
483-
range.start.line,
484-
range.end.character,
485-
range.end.line
486-
].every(n => n === 0);

0 commit comments

Comments
 (0)