Skip to content

Commit

Permalink
Prototype TS Codefix commands (#35002)
Browse files Browse the repository at this point in the history
* Prototype TS Codefix commands

Initial prototype to support commands on JS/TS quick fixes

Part of #34787

* use command for argument name

* Update to use published api
  • Loading branch information
mjbvz authored Oct 20, 2017
1 parent 1c71515 commit dc44eab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
46 changes: 30 additions & 16 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 @@ -72,31 +72,45 @@ export default class TypeScriptCodeActionProvider implements CodeActionProvider
return this._supportedCodeActions;
}

private getSupportedActionsForContext(context: CodeActionContext): Thenable<Set<number>> {
return this.supportedCodeActions.then(supportedActions =>
new Set(context.diagnostics
.map(diagnostic => +diagnostic.code)
.filter(code => supportedActions[code])));
private async getSupportedActionsForContext(context: CodeActionContext): Promise<Set<number>> {
const supportedActions = await this.supportedCodeActions;
return new Set(context.diagnostics
.map(diagnostic => +diagnostic.code)
.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('applyCodeActionCommand', { file, command });
if (!response || !response.body) {
return false;
}
}
}
return true;
}
}
5 changes: 5 additions & 0 deletions extensions/typescript/src/features/refactorProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ export default class TypeScriptRefactorProvider implements CodeActionProvider {

const actions: Command[] = [];
for (const info of response.body) {
// Workaround for https://github.com/Microsoft/TypeScript/issues/19378
if (info.name.startsWith('Install missing ')) {
continue;
}

if (info.inlineable === false) {
actions.push({
title: info.description,
Expand Down
1 change: 1 addition & 0 deletions extensions/typescript/src/typescriptService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface ITypescriptServiceClient {
execute(command: 'docCommentTemplate', args: Proto.FileLocationRequestArgs, token?: CancellationToken): Promise<Proto.DocCommandTemplateResponse>;
execute(command: 'getApplicableRefactors', args: Proto.GetApplicableRefactorsRequestArgs, token?: CancellationToken): Promise<Proto.GetApplicableRefactorsResponse>;
execute(command: 'getEditsForRefactor', args: Proto.GetEditsForRefactorRequestArgs, token?: CancellationToken): Promise<Proto.GetEditsForRefactorResponse>;
execute(command: 'applyCodeActionCommand', args: Proto.ApplyCodeActionCommandRequestArgs, token?: CancellationToken): Promise<Proto.ApplyCodeActionCommandResponse>;
// execute(command: 'compileOnSaveAffectedFileList', args: Proto.CompileOnSaveEmitFileRequestArgs, token?: CancellationToken): Promise<Proto.CompileOnSaveAffectedFileListResponse>;
// execute(command: 'compileOnSaveEmitFile', args: Proto.CompileOnSaveEmitFileRequestArgs, token?: CancellationToken): Promise<any>;
execute(command: string, args: any, expectedResult: boolean | CancellationToken, token?: CancellationToken): Promise<any>;
Expand Down

0 comments on commit dc44eab

Please sign in to comment.