Skip to content

Commit

Permalink
feat: support textDocument/prepareRename request (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl committed Dec 9, 2022
1 parent 01f7272 commit 9c66794
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ interface DefinitionSymbol {
- [x] textDocument/formatting
- [x] textDocument/hover
- [x] textDocument/inlayHint (no support for `inlayHint/resolve` or `workspace/inlayHint/refresh`)
- [x] textDocument/prepareRename
- [x] textDocument/rangeFormatting
- [x] textDocument/references
- [x] textDocument/rename
Expand Down
1 change: 1 addition & 0 deletions src/lsp-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function createLspConnection(options: LspConnectionOptions): lsp.Connecti
connection.onHover(server.hover.bind(server));
connection.onReferences(server.references.bind(server));
connection.onRenameRequest(server.rename.bind(server));
connection.onPrepareRename(server.prepareRename.bind(server));
connection.onSignatureHelp(server.signatureHelp.bind(server));
connection.onWorkspaceSymbol(server.workspaceSymbol.bind(server));
connection.onFoldingRanges(server.foldingRanges.bind(server));
Expand Down
33 changes: 22 additions & 11 deletions src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export class LspServer {
this.configurationManager.setAndConfigureTspClient(this.workspaceRoot, this._tspClient, hostInfo);
this.setCompilerOptionsForInferredProjects();

const prepareSupport = textDocument?.rename?.prepareSupport && this.tspClient.apiVersion.gte(API.v310);
const initializeResult: lsp.InitializeResult = {
capabilities: {
textDocumentSync: lsp.TextDocumentSyncKind.Incremental,
Expand Down Expand Up @@ -214,7 +215,7 @@ export class LspServer {
},
hoverProvider: true,
inlayHintProvider: true,
renameProvider: true,
renameProvider: prepareSupport ? { prepareProvider: true } : true,
referencesProvider: true,
signatureHelpProvider: {
triggerCharacters: ['(', ',', '<'],
Expand Down Expand Up @@ -673,21 +674,31 @@ export class LspServer {
}
}

async rename(params: lsp.RenameParams): Promise<lsp.WorkspaceEdit | undefined> {
async prepareRename(params: lsp.PrepareRenameParams): Promise<lsp.Range | { range: lsp.Range; placeholder: string; } | undefined | null> {
const file = uriToPath(params.textDocument.uri);
this.logger.log('onRename', params, file);
if (!file) {
return undefined;
return null;
}
const result = await this.tspClient.request(CommandTypes.Rename, Position.toFileLocationRequestArgs(file, params.position));
const renameInfo = result.body?.info;
if (!renameInfo) {
return null;
}
if (!renameInfo.canRename) {
throw new Error(renameInfo.localizedErrorMessage);
}
return Range.fromTextSpan(renameInfo.triggerSpan);
}

const result = await this.tspClient.request(CommandTypes.Rename, {
file,
line: params.position.line + 1,
offset: params.position.character + 1,
});

async rename(params: lsp.RenameParams): Promise<lsp.WorkspaceEdit | undefined | null> {
const file = uriToPath(params.textDocument.uri);
this.logger.log('onRename', params, file);
if (!file) {
return null;
}
const result = await this.tspClient.request(CommandTypes.Rename, Position.toFileLocationRequestArgs(file, params.position));
if (!result.body || !result.body.info.canRename || result.body.locs.length === 0) {
return undefined;
return null;
}
const workspaceEdit: lsp.WorkspaceEdit = {};
result.body.locs
Expand Down

0 comments on commit 9c66794

Please sign in to comment.