Skip to content

Commit

Permalink
feat: support workspace/willRenameFiles request (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
antosha417 committed Feb 12, 2023
1 parent af10a97 commit c3f3529
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/lsp-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function createLspConnection(options: LspConnectionOptions): lsp.Connecti
connection.languages.inlayHint.on(server.inlayHints.bind(server));
connection.languages.semanticTokens.on(server.semanticTokensFull.bind(server));
connection.languages.semanticTokens.onRange(server.semanticTokensRange.bind(server));
connection.workspace.onWillRenameFiles(server.willRenameFiles.bind(server));

return connection;
}
19 changes: 19 additions & 0 deletions src/lsp-server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2184,3 +2184,22 @@ describe('completions without client snippet support', () => {
);
});
});

describe('fileOperations', () => {
it('willRenameFiles', async () => {
const edit = await server.willRenameFiles({
files: [{ oldUri: uri('module1.ts'), newUri: uri('new_module1_name.ts') }],
});
expect(edit.changes).toBeDefined();
expect(Object.keys(edit.changes!)).toHaveLength(1);
expect(edit.changes![uri('module2.ts')]).toEqual([
{
range: {
start:{ line: 0, character: 25 },
end: { line: 0, character: 34 },
},
newText:'./new_module1_name',
},
]);
});
});
32 changes: 26 additions & 6 deletions src/lsp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,16 @@ export class LspServer {
full: true,
range: true,
},
workspace: {
fileOperations: {
willRename: {
filters: [{
scheme: 'file',
pattern: { glob: '**/*.{ts,js,jsx,tsx,mjs,mts,cjs,cts}', matches: 'file' },
}],
},
},
},
},
};
if (textDocument?.callHierarchy && typescriptVersion.version?.gte(API.v380)) {
Expand Down Expand Up @@ -745,14 +755,11 @@ export class LspServer {
if (!result.body?.info.canRename || result.body.locs.length === 0) {
return null;
}
const workspaceEdit: lsp.WorkspaceEdit = {};
const changes: lsp.WorkspaceEdit['changes'] = {};
result.body.locs
.forEach((spanGroup) => {
const uri = pathToUri(spanGroup.file, this.documents);
if (!workspaceEdit.changes) {
workspaceEdit.changes = {};
}
const textEdits = workspaceEdit.changes[uri] || (workspaceEdit.changes[uri] = []);
const textEdits = changes[uri] || (changes[uri] = []);

spanGroup.locs.forEach((textSpan) => {
textEdits.push({
Expand All @@ -765,7 +772,7 @@ export class LspServer {
});
});

return workspaceEdit;
return { changes };
}

async references(params: lsp.ReferenceParams, token?: lsp.CancellationToken): Promise<lsp.Location[]> {
Expand Down Expand Up @@ -1081,6 +1088,19 @@ export class LspServer {
return applied;
}

async willRenameFiles(params: lsp.RenameFilesParams, token?: lsp.CancellationToken): Promise<lsp.WorkspaceEdit> {
const changes: lsp.WorkspaceEdit['changes'] = {};
for (const rename of params.files) {
const codeEdits = await this.getEditsForFileRename(rename.oldUri, rename.newUri, token);
for (const codeEdit of codeEdits) {
const uri = pathToUri(codeEdit.fileName, this.documents);
const textEdits = changes[uri] || (changes[uri] = []);
textEdits.push(...codeEdit.textChanges.map(toTextEdit));
}
}
return { changes };
}

protected async applyRenameFile(sourceUri: string, targetUri: string, token?: lsp.CancellationToken): Promise<void> {
const edits = await this.getEditsForFileRename(sourceUri, targetUri, token);
this.applyFileCodeEdits(edits);
Expand Down

0 comments on commit c3f3529

Please sign in to comment.