Rename CodeAction.canAutoApply -> CodeAction.preferred

Part of #62110

Use the more generic name as suggested in https://github.com/Dart-Code/Dart-Code/issues/1393. This makes the intent of the field more clear and also allows us to extend the concept of preferred code actions to refactorings and other classes of code actions

Experimentally also allows a `preferred` value for `apply` when configuring code aciton keyboard shortcuts. This applies the preferred code action returned from the list of code actions returned
This commit is contained in:
Matt Bierner
2019-01-17 17:52:46 -08:00
parent 0e90c2e328
commit bcc2281e45
9 changed files with 33 additions and 20 deletions

View File

@@ -270,7 +270,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
title: ''
};
if (tsAction.fixName === 'spelling') {
codeAction.canAutoApply = true;
codeAction.isPreferred = true;
}
return codeAction;
}

View File

@@ -516,7 +516,7 @@ export interface CodeAction {
edit?: WorkspaceEdit;
diagnostics?: IMarkerData[];
kind?: string;
canAutoApply?: boolean;
isPreferred?: boolean;
}
/**

View File

@@ -72,7 +72,7 @@ export function getCodeActions(
function isValidAction(filter: CodeActionFilter | undefined, action: CodeAction): boolean {
return action
&& isValidActionKind(filter, action.kind)
&& (filter && filter.autoFixesOnly ? !!action.canAutoApply : true);
&& (filter && filter.onlyIncludePreferredActions ? !!action.isPreferred : true);
}
function isValidActionKind(filter: CodeActionFilter | undefined, kind: string | undefined): boolean {

View File

@@ -90,13 +90,23 @@ export class QuickFixController implements IEditorContribution {
if (newState.trigger.filter && newState.trigger.filter.kind) {
// Triggered for specific scope
// Apply if we only have one action or requested autoApply, otherwise show menu
newState.actions.then(fixes => {
if (fixes.length > 0 && newState.trigger.autoApply === CodeActionAutoApply.First || (newState.trigger.autoApply === CodeActionAutoApply.IfSingle && fixes.length === 1)) {
this._onApplyCodeAction(fixes[0]);
} else {
this._codeActionContextMenu.show(newState.actions, newState.position);
if (fixes.length > 0) {
// Apply if we only have one action or requested autoApply
if (newState.trigger.autoApply === CodeActionAutoApply.First || (newState.trigger.autoApply === CodeActionAutoApply.IfSingle && fixes.length === 1)) {
this._onApplyCodeAction(fixes[0]);
return;
}
// Or if we have a single preferred action
const preferred = fixes.filter(fix => fix.isPreferred);
if (preferred.length === 0) {
this._onApplyCodeAction(preferred[0]);
return;
}
}
this._codeActionContextMenu.show(newState.actions, newState.position);
}).catch(onUnexpectedError);
} else if (newState.trigger.type === 'manual') {
this._codeActionContextMenu.show(newState.actions, newState.position);
@@ -221,6 +231,7 @@ class CodeActionCommandArgs {
case 'first': return CodeActionAutoApply.First;
case 'never': return CodeActionAutoApply.Never;
case 'ifsingle': return CodeActionAutoApply.IfSingle;
case 'preferred': return CodeActionAutoApply.Preferred;
default: return defaultAutoApply;
}
}
@@ -382,7 +393,7 @@ export class AutoFixAction extends EditorAction {
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void {
return showCodeActionsForEditorSelection(editor,
nls.localize('editor.action.autoFix.noneMessage', "No auto fixes available"),
{ kind: CodeActionKind.QuickFix, autoFixesOnly: true },
{ kind: CodeActionKind.QuickFix, onlyIncludePreferredActions: true },
CodeActionAutoApply.IfSingle);
}
}

View File

@@ -24,15 +24,16 @@ export class CodeActionKind {
}
export const enum CodeActionAutoApply {
IfSingle = 1,
First = 2,
Never = 3
IfSingle,
First,
Preferred,
Never,
}
export interface CodeActionFilter {
readonly kind?: CodeActionKind;
readonly includeSourceActions?: boolean;
readonly autoFixesOnly?: boolean;
readonly onlyIncludePreferredActions?: boolean;
}
export interface CodeActionTrigger {

2
src/vs/monaco.d.ts vendored
View File

@@ -4848,7 +4848,7 @@ declare namespace monaco.languages {
edit?: WorkspaceEdit;
diagnostics?: editor.IMarkerData[];
kind?: string;
canAutoApply?: boolean;
isPreferred?: boolean;
}
/**

View File

@@ -1110,14 +1110,15 @@ declare module 'vscode' {
}
//#endregion
//#region CodeAction.canAutoApply - mjbvz
//#region CodeAction.isPreferred - mjbvz
export interface CodeAction {
/**
* If the action can be safely automatically applied without the user selecting it from a list.
* If the action is a prefered action or fix to take.
*
* Set this on quick fixes to indicate that the fix properly addresses the underlying error.
* A quick fix should be marked preferred if it properly addresses the underlying error.
* A refactoring should be marked preferred if it is the most reasonable choice of actions to take.
*/
canAutoApply?: boolean;
isPreferred?: boolean;
}
//#endregion

View File

@@ -864,7 +864,7 @@ export interface CodeActionDto {
diagnostics?: IMarkerData[];
command?: modes.Command;
kind?: string;
canAutoApply?: boolean;
isPreferred?: boolean;
}
export interface ExtHostLanguageFeaturesShape {

View File

@@ -347,7 +347,7 @@ class CodeActionAdapter {
diagnostics: candidate.diagnostics && candidate.diagnostics.map(typeConvert.Diagnostic.from),
edit: candidate.edit && typeConvert.WorkspaceEdit.from(candidate.edit),
kind: candidate.kind && candidate.kind.value,
canAutoApply: candidate.canAutoApply,
isPreferred: candidate.isPreferred,
});
}
}