提交 1be59b45 编写于 作者: M Matt Bierner

Support typescript's fileToRename

Fixes #51175

Allows triggering renames on import paths
上级 0aaa7c9d
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
import * as Proto from '../protocol'; import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService'; import { ITypeScriptServiceClient } from '../typescriptService';
...@@ -55,7 +56,15 @@ class TypeScriptRenameProvider implements vscode.RenameProvider { ...@@ -55,7 +56,15 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
if (!renameInfo.canRename) { if (!renameInfo.canRename) {
return Promise.reject<vscode.WorkspaceEdit>(renameInfo.localizedErrorMessage); return Promise.reject<vscode.WorkspaceEdit>(renameInfo.localizedErrorMessage);
} }
return this.toWorkspaceEdit(body.locs, newName);
const edit = new vscode.WorkspaceEdit();
if (this.client.apiVersion.gte(API.v310)) {
if (renameInfo.fileToRename) {
this.renameFile(edit, renameInfo.fileToRename, newName);
}
}
this.updateLocs(edit, body.locs, newName);
return edit;
} }
public async execRename( public async execRename(
...@@ -82,20 +91,37 @@ class TypeScriptRenameProvider implements vscode.RenameProvider { ...@@ -82,20 +91,37 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
} }
} }
private toWorkspaceEdit( private updateLocs(
edit: vscode.WorkspaceEdit,
locations: ReadonlyArray<Proto.SpanGroup>, locations: ReadonlyArray<Proto.SpanGroup>,
newName: string newName: string
) { ) {
const result = new vscode.WorkspaceEdit();
for (const spanGroup of locations) { for (const spanGroup of locations) {
const resource = this.client.toResource(spanGroup.file); const resource = this.client.toResource(spanGroup.file);
if (resource) { if (resource) {
for (const textSpan of spanGroup.locs) { for (const textSpan of spanGroup.locs) {
result.replace(resource, typeConverters.Range.fromTextSpan(textSpan), newName); edit.replace(resource, typeConverters.Range.fromTextSpan(textSpan), newName);
} }
} }
} }
return result; return edit;
}
private renameFile(
edit: vscode.WorkspaceEdit,
fileToRename: string,
newName: string,
): vscode.WorkspaceEdit {
// Make sure we preserve file exension if none provided
if (path.extname(newName)) {
newName += path.extname(fileToRename);
}
const dirname = path.dirname(fileToRename);
const newFilePath = path.join(dirname, newName);
edit.renameFile(vscode.Uri.file(fileToRename), vscode.Uri.file(newFilePath));
return edit;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册