mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 10:08:49 +01:00
Switches the 'Fix all' quick fix provider to use resolveCodeAction (#107853)
Made while testing #106410
This commit is contained in:
@@ -51,6 +51,9 @@ class ApplyCodeActionCommand implements Command {
|
||||
}
|
||||
}
|
||||
|
||||
type ApplyFixAllCodeAction_args = {
|
||||
readonly action: VsCodeFixAllCodeAction;
|
||||
};
|
||||
|
||||
class ApplyFixAllCodeAction implements Command {
|
||||
public static readonly ID = '_typescript.applyFixAllCodeAction';
|
||||
@@ -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" },
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>();
|
||||
@@ -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]
|
||||
@@ -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,
|
||||
@@ -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 = {
|
||||
@@ -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);
|
||||
@@ -370,7 +389,7 @@ function isPreferredFix(
|
||||
action: VsCodeCodeAction,
|
||||
allActions: readonly VsCodeCodeAction[]
|
||||
): boolean {
|
||||
if (action.isFixAll) {
|
||||
if (action instanceof VsCodeFixAllCodeAction) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -384,7 +403,7 @@ function isPreferredFix(
|
||||
return true;
|
||||
}
|
||||
|
||||
if (otherAction.isFixAll) {
|
||||
if (otherAction instanceof VsCodeFixAllCodeAction) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user