提交 7c96862d 编写于 作者: M Matt Bierner

Use workspace edit to create new file on refactor

#10659
上级 fae2d736
......@@ -39,7 +39,7 @@ class OrganizeImportsCommand implements Command {
return false;
}
const edits = typeconverts.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body);
const edits = typeconverts.WorkspaceEdit.fromFileCodeEdits(this.client, response.body);
return await vscode.workspace.applyEdit(edits);
}
}
......
......@@ -92,7 +92,7 @@ class ApplyFixAllCodeAction implements Command {
return;
}
const edit = typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, combinedCodeFixesResponse.body.changes);
const edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(this.client, combinedCodeFixesResponse.body.changes);
await vscode.workspace.applyEdit(edit);
if (combinedCodeFixesResponse.command) {
......
......@@ -4,15 +4,14 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as Proto from '../protocol';
import { ITypeScriptServiceClient } from '../typescriptService';
import API from '../utils/api';
import { Command, CommandManager } from '../utils/commandManager';
import { VersionDependentRegistration } from '../utils/dependentRegistration';
import * as typeConverters from '../utils/typeConverters';
import FormattingOptionsManager from './fileConfigurationManager';
import { CommandManager, Command } from '../utils/commandManager';
import { VersionDependentRegistration } from '../utils/dependentRegistration';
import API from '../utils/api';
class ApplyRefactoringCommand implements Command {
public static readonly ID = '_typescript.applyRefactoring';
......@@ -35,30 +34,17 @@ class ApplyRefactoringCommand implements Command {
action
};
const response = await this.client.execute('getEditsForRefactor', args);
if (!response || !response.body || !response.body.edits.length) {
const body = response && response.body;
if (!body || !body.edits.length) {
return false;
}
for (const edit of response.body.edits) {
try {
await vscode.workspace.openTextDocument(edit.fileName);
} catch {
try {
if (!fs.existsSync(edit.fileName)) {
fs.writeFileSync(edit.fileName, '');
}
} catch {
// noop
}
}
}
const edit = typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, response.body.edits);
if (!(await vscode.workspace.applyEdit(edit))) {
const workspaceEdit = await this.toWorkspaceEdit(body);
if (!(await vscode.workspace.applyEdit(workspaceEdit))) {
return false;
}
const renameLocation = response.body.renameLocation;
const renameLocation = body.renameLocation;
if (renameLocation) {
await vscode.commands.executeCommand('editor.action.rename', [
document.uri,
......@@ -67,6 +53,19 @@ class ApplyRefactoringCommand implements Command {
}
return true;
}
private async toWorkspaceEdit(body: Proto.RefactorEditInfo) {
const workspaceEdit = new vscode.WorkspaceEdit();
for (const edit of body.edits) {
try {
await vscode.workspace.openTextDocument(edit.fileName);
} catch {
workspaceEdit.createFile(this.client.toResource(edit.fileName));
}
}
typeConverters.WorkspaceEdit.withFileCodeEdits(workspaceEdit, this.client, body.edits);
return workspaceEdit;
}
}
class SelectRefactorCommand implements Command {
......
......@@ -227,7 +227,7 @@ export class UpdateImportsOnFileRenameHandler {
for (const edit of response.body) {
edits.push(await this.fixEdit(edit, isDirectoryRename, oldFile, newFile));
}
return typeConverters.WorkspaceEdit.fromFromFileCodeEdits(this.client, edits);
return typeConverters.WorkspaceEdit.fromFileCodeEdits(this.client, edits);
}
private async fixEdit(
......
......@@ -13,7 +13,7 @@ export function getEditForCodeAction(
action: Proto.CodeAction
): WorkspaceEdit | undefined {
return action.changes && action.changes.length
? typeConverters.WorkspaceEdit.fromFromFileCodeEdits(client, action.changes)
? typeConverters.WorkspaceEdit.fromFileCodeEdits(client, action.changes)
: undefined;
}
......
......@@ -50,11 +50,18 @@ export namespace TextEdit {
}
export namespace WorkspaceEdit {
export function fromFromFileCodeEdits(
export function fromFileCodeEdits(
client: ITypeScriptServiceClient,
edits: Iterable<Proto.FileCodeEdits>
): vscode.WorkspaceEdit {
return withFileCodeEdits(new vscode.WorkspaceEdit(), client, edits);
}
export function withFileCodeEdits(
workspaceEdit: vscode.WorkspaceEdit,
client: ITypeScriptServiceClient,
edits: Iterable<Proto.FileCodeEdits>
): vscode.WorkspaceEdit {
const workspaceEdit = new vscode.WorkspaceEdit();
for (const edit of edits) {
for (const textChange of edit.textChanges) {
workspaceEdit.replace(client.toResource(edit.fileName),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册