From 8a3475ac8c69421ca5fb84c85e66088dbae37f34 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Mon, 25 Sep 2017 16:05:32 -0700 Subject: [PATCH] Prototype TS Codefix commands Initial prototype to support commands on JS/TS quick fixes Part of #34787 --- .../src/features/codeActionProvider.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/extensions/typescript/src/features/codeActionProvider.ts b/extensions/typescript/src/features/codeActionProvider.ts index 32f50fef59fc7..5bff441f397b2 100644 --- a/extensions/typescript/src/features/codeActionProvider.ts +++ b/extensions/typescript/src/features/codeActionProvider.ts @@ -55,7 +55,7 @@ export default class TypeScriptCodeActionProvider implements CodeActionProvider errorCodes: Array.from(supportedActions) }; const response = await this.client.execute('getCodeFixes', args, token); - return (response.body || []).map(action => this.getCommandForAction(action)); + return (response.body || []).map(action => this.getCommandForAction(action, file)); } private get supportedCodeActions(): Thenable { @@ -79,24 +79,38 @@ export default class TypeScriptCodeActionProvider implements CodeActionProvider .filter(code => supportedActions[code]))); } - private getCommandForAction(action: Proto.CodeAction): Command { + private getCommandForAction(action: Proto.CodeAction, file: string): Command { return { title: action.description, command: this.commandId, - arguments: [action] + arguments: [action, file] }; } - private async onCodeAction(action: Proto.CodeAction): Promise { - const workspaceEdit = new WorkspaceEdit(); - for (const change of action.changes) { - for (const textChange of change.textChanges) { - workspaceEdit.replace(this.client.asUrl(change.fileName), - tsTextSpanToVsRange(textChange), - textChange.newText); + private async onCodeAction(action: Proto.CodeAction, file: string): Promise { + if (action.changes && action.changes.length) { + const workspaceEdit = new WorkspaceEdit(); + for (const change of action.changes) { + for (const textChange of change.textChanges) { + workspaceEdit.replace(this.client.asUrl(change.fileName), + tsTextSpanToVsRange(textChange), + textChange.newText); + } + } + + if (!(await workspace.applyEdit(workspaceEdit))) { + return false; } } - return workspace.applyEdit(workspaceEdit); + if (action.commands && action.commands.length) { + for (const command of action.commands) { + const response = await this.client.execute('applyCodeFixCommand', { file, action: command }); + if (!response || !response.body) { + return false; + } + } + } + return true; } } \ No newline at end of file