Don't mark fix all actions as preferred

Fixes #97866
This commit is contained in:
Matt Bierner 2020-05-14 21:57:43 -07:00
parent 72e6925981
commit 541b9eb7a6
2 changed files with 57 additions and 9 deletions

View file

@ -132,6 +132,7 @@ class VsCodeCodeAction extends vscode.CodeAction {
public readonly tsAction: Proto.CodeFixAction,
title: string,
kind: vscode.CodeActionKind,
public readonly isFixAll: boolean,
) {
super(title, kind);
}
@ -232,9 +233,8 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
}
const allActions = Array.from(results.values);
const allTsActions = allActions.map(x => x.tsAction);
for (const action of allActions) {
action.isPreferred = isPreferredFix(action.tsAction, allTsActions);
action.isPreferred = isPreferredFix(action, allActions);
}
return allActions;
}
@ -277,7 +277,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeFixAction
): VsCodeCodeAction {
const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix);
const codeAction = new VsCodeCodeAction(tsAction, tsAction.description, vscode.CodeActionKind.QuickFix, false);
codeAction.edit = getEditForCodeAction(this.client, tsAction);
codeAction.diagnostics = [diagnostic];
codeAction.command = {
@ -313,7 +313,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
const action = new VsCodeCodeAction(
tsAction,
tsAction.fixAllDescription || localize('fixAllInFileLabel', '{0} (Fix all in file)', tsAction.description),
vscode.CodeActionKind.QuickFix);
vscode.CodeActionKind.QuickFix, true);
action.diagnostics = [diagnostic];
action.command = {
command: ApplyFixAllCodeAction.ID,
@ -347,18 +347,23 @@ const preferredFixes = new Map<string, /* priorty */number>([
]);
function isPreferredFix(
tsAction: Proto.CodeFixAction,
allActions: readonly Proto.CodeFixAction[]
action: VsCodeCodeAction,
allActions: readonly VsCodeCodeAction[]
): boolean {
const priority = preferredFixes.get(tsAction.fixName);
if (action.isFixAll) {
return false;
}
const priority = preferredFixes.get(action.tsAction.fixName);
if (typeof priority === 'undefined') {
return false;
}
return allActions.every(otherAction => {
if (otherAction === tsAction) {
if (otherAction.tsAction === action.tsAction) {
return true;
}
const otherPriority = preferredFixes.get(otherAction.fixName);
const otherPriority = preferredFixes.get(otherAction.tsAction.fixName);
if (typeof otherPriority === 'undefined') {
return true;
}

View file

@ -0,0 +1,43 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import 'mocha';
import * as vscode from 'vscode';
import { disposeAll } from '../utils/dispose';
import { createTestEditor, joinLines, wait } from './testUtils';
const testDocumentUri = vscode.Uri.parse('untitled:test.ts');
suite('TypeScript Quick Fix', () => {
const _disposables: vscode.Disposable[] = [];
teardown(async () => {
disposeAll(_disposables);
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
test('Fix all should not be marked as preferred #97866', async () => {
const editor = await createTestEditor(testDocumentUri,
`export const _ = 1;`,
`const a$0 = 1;`,
`const b = 2;`,
);
await wait(2000);
await vscode.commands.executeCommand('editor.action.autoFix');
await wait(500);
assert.strictEqual(editor.document.getText(), joinLines(
`export const _ = 1;`,
`const b = 2;`,
));
});
});