Skip to content

Commit

Permalink
Switches the 'Fix all' quick fix provider to use resolveCodeAction
Browse files Browse the repository at this point in the history
Made while testing microsoft#106410
  • Loading branch information
mjbvz committed Sep 30, 2020
1 parent 706857d commit 01d4b00
Showing 1 changed file with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ApplyCodeActionCommand implements Command {
}
}

type ApplyFixAllCodeAction_args = {
readonly action: VsCodeFixAllCodeAction;
};

class ApplyFixAllCodeAction implements Command {
public static readonly ID = '_typescript.applyFixAllCodeAction';
Expand All @@ -61,14 +64,7 @@ class ApplyFixAllCodeAction implements Command {
private readonly telemetryReporter: TelemetryReporter,
) { }

public async execute(
file: string,
tsAction: Proto.CodeFixAction,
): Promise<void> {
if (!tsAction.fixId) {
return;
}

public async execute(args: ApplyFixAllCodeAction_args): Promise<void> {
/* __GDPR__
"quickFixAll.execute" : {
"fixName" : { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" },
Expand All @@ -78,25 +74,12 @@ class ApplyFixAllCodeAction implements Command {
}
*/
this.telemetryReporter.logTelemetry('quickFixAll.execute', {
fixName: tsAction.fixName
fixName: args.action.tsAction.fixName
});

const args: Proto.GetCombinedCodeFixRequestArgs = {
scope: {
type: 'file',
args: { file }
},
fixId: tsAction.fixId,
};

const response = await this.client.execute('getCombinedCodeFix', args, nulToken);
if (response.type !== 'response' || !response.body) {
return undefined;
if (args.action.combinedResponse) {
await applyCodeActionCommands(this.client, args.action.combinedResponse.body.commands, nulToken);
}

const edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(this.client, response.body.changes);
await vscode.workspace.applyEdit(edit);
await applyCodeActionCommands(this.client, response.body.commands, nulToken);
}
}

Expand Down Expand Up @@ -134,13 +117,25 @@ class VsCodeCodeAction extends vscode.CodeAction {
constructor(
public readonly tsAction: Proto.CodeFixAction,
title: string,
kind: vscode.CodeActionKind,
public readonly isFixAll: boolean,
kind: vscode.CodeActionKind
) {
super(title, kind);
}
}

class VsCodeFixAllCodeAction extends VsCodeCodeAction {
constructor(
tsAction: Proto.CodeFixAction,
public readonly file: string,
title: string,
kind: vscode.CodeActionKind
) {
super(tsAction, title, kind);
}

public combinedResponse?: Proto.GetCombinedCodeFixResponse;
}

class CodeActionSet {
private readonly _actions = new Set<VsCodeCodeAction>();
private readonly _fixAllActions = new Map<{}, VsCodeCodeAction>();
Expand Down Expand Up @@ -202,7 +197,7 @@ class SupportedCodeActionProvider {
}
}

class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
class TypeScriptQuickFixProvider implements vscode.CodeActionProvider<VsCodeCodeAction> {

public static readonly metadata: vscode.CodeActionProviderMetadata = {
providedCodeActionKinds: [vscode.CodeActionKind.QuickFix]
Expand Down Expand Up @@ -257,6 +252,28 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
return allActions;
}

public async resolveCodeAction(codeAction: VsCodeCodeAction, token: vscode.CancellationToken): Promise<VsCodeCodeAction> {
if (!(codeAction instanceof VsCodeFixAllCodeAction) || !codeAction.tsAction.fixId) {
return codeAction;
}

const arg: Proto.GetCombinedCodeFixRequestArgs = {
scope: {
type: 'file',
args: { file: codeAction.file }
},
fixId: codeAction.tsAction.fixId,
};

const response = await this.client.execute('getCombinedCodeFix', arg, token);
if (response.type === 'response') {
codeAction.combinedResponse = response;
codeAction.edit = typeConverters.WorkspaceEdit.fromFileCodeEdits(this.client, response.body.changes);
}

return codeAction;
}

private async getFixesForDiagnostic(
document: vscode.TextDocument,
file: string,
Expand Down Expand Up @@ -295,7 +312,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeFixAction
): VsCodeCodeAction {
const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix, false);
const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix);
codeAction.edit = getEditForCodeAction(this.client, tsAction);
codeAction.diagnostics = [diagnostic];
codeAction.command = {
Expand Down Expand Up @@ -328,14 +345,16 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
return results;
}

const action = new VsCodeCodeAction(
const action = new VsCodeFixAllCodeAction(
tsAction,
file,
tsAction.fixAllDescription || localize('fixAllInFileLabel', '{0} (Fix all in file)', tsAction.description),
vscode.CodeActionKind.QuickFix, true);
vscode.CodeActionKind.QuickFix);

action.diagnostics = [diagnostic];
action.command = {
command: ApplyFixAllCodeAction.ID,
arguments: [file, tsAction],
arguments: [<ApplyFixAllCodeAction_args>{ action }],
title: ''
};
results.addFixAllAction(tsAction.fixId, action);
Expand Down Expand Up @@ -370,7 +389,7 @@ function isPreferredFix(
action: VsCodeCodeAction,
allActions: readonly VsCodeCodeAction[]
): boolean {
if (action.isFixAll) {
if (action instanceof VsCodeFixAllCodeAction) {
return false;
}

Expand All @@ -384,7 +403,7 @@ function isPreferredFix(
return true;
}

if (otherAction.isFixAll) {
if (otherAction instanceof VsCodeFixAllCodeAction) {
return true;
}

Expand Down

0 comments on commit 01d4b00

Please sign in to comment.