From bcc2281e45d3519e4fe401ecb0af99976b247458 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Thu, 17 Jan 2019 17:52:46 -0800 Subject: [PATCH] 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 --- .../src/features/quickFix.ts | 2 +- src/vs/editor/common/modes.ts | 2 +- .../editor/contrib/codeAction/codeAction.ts | 2 +- .../contrib/codeAction/codeActionCommands.ts | 23 ++++++++++++++----- .../contrib/codeAction/codeActionTrigger.ts | 9 ++++---- src/vs/monaco.d.ts | 2 +- src/vs/vscode.proposed.d.ts | 9 ++++---- src/vs/workbench/api/node/extHost.protocol.ts | 2 +- .../api/node/extHostLanguageFeatures.ts | 2 +- 9 files changed, 33 insertions(+), 20 deletions(-) diff --git a/extensions/typescript-language-features/src/features/quickFix.ts b/extensions/typescript-language-features/src/features/quickFix.ts index 0b4fa587ec0..16b2069c232 100644 --- a/extensions/typescript-language-features/src/features/quickFix.ts +++ b/extensions/typescript-language-features/src/features/quickFix.ts @@ -270,7 +270,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider { title: '' }; if (tsAction.fixName === 'spelling') { - codeAction.canAutoApply = true; + codeAction.isPreferred = true; } return codeAction; } diff --git a/src/vs/editor/common/modes.ts b/src/vs/editor/common/modes.ts index 80eb6e5d057..3e6e7bcc4cd 100644 --- a/src/vs/editor/common/modes.ts +++ b/src/vs/editor/common/modes.ts @@ -516,7 +516,7 @@ export interface CodeAction { edit?: WorkspaceEdit; diagnostics?: IMarkerData[]; kind?: string; - canAutoApply?: boolean; + isPreferred?: boolean; } /** diff --git a/src/vs/editor/contrib/codeAction/codeAction.ts b/src/vs/editor/contrib/codeAction/codeAction.ts index 8d012d07be3..d203fb26d8c 100644 --- a/src/vs/editor/contrib/codeAction/codeAction.ts +++ b/src/vs/editor/contrib/codeAction/codeAction.ts @@ -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 { diff --git a/src/vs/editor/contrib/codeAction/codeActionCommands.ts b/src/vs/editor/contrib/codeAction/codeActionCommands.ts index 072d6b4ea58..673688ef41f 100644 --- a/src/vs/editor/contrib/codeAction/codeActionCommands.ts +++ b/src/vs/editor/contrib/codeAction/codeActionCommands.ts @@ -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); } } diff --git a/src/vs/editor/contrib/codeAction/codeActionTrigger.ts b/src/vs/editor/contrib/codeAction/codeActionTrigger.ts index ad1bbfe85b1..305470be4a4 100644 --- a/src/vs/editor/contrib/codeAction/codeActionTrigger.ts +++ b/src/vs/editor/contrib/codeAction/codeActionTrigger.ts @@ -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 { diff --git a/src/vs/monaco.d.ts b/src/vs/monaco.d.ts index 0cd4c9504fd..a15aadccd86 100644 --- a/src/vs/monaco.d.ts +++ b/src/vs/monaco.d.ts @@ -4848,7 +4848,7 @@ declare namespace monaco.languages { edit?: WorkspaceEdit; diagnostics?: editor.IMarkerData[]; kind?: string; - canAutoApply?: boolean; + isPreferred?: boolean; } /** diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index f95e97c3ced..8cbeff02145 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -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 diff --git a/src/vs/workbench/api/node/extHost.protocol.ts b/src/vs/workbench/api/node/extHost.protocol.ts index d8e363a64a2..b39e87c00c5 100644 --- a/src/vs/workbench/api/node/extHost.protocol.ts +++ b/src/vs/workbench/api/node/extHost.protocol.ts @@ -864,7 +864,7 @@ export interface CodeActionDto { diagnostics?: IMarkerData[]; command?: modes.Command; kind?: string; - canAutoApply?: boolean; + isPreferred?: boolean; } export interface ExtHostLanguageFeaturesShape { diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index e12de7857c0..b8b1d17ff4c 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -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, }); } }