Skip to content

Commit

Permalink
Prototype TS Codefix commands
Browse files Browse the repository at this point in the history
Initial prototype to support commands on JS/TS quick fixes

Part of microsoft#34787
  • Loading branch information
mjbvz committed Oct 20, 2017
1 parent 1c71515 commit 8a3475a
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions extensions/typescript/src/features/codeActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NumberSet> {
Expand All @@ -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<boolean> {
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<boolean> {
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;
}
}

0 comments on commit 8a3475a

Please sign in to comment.