提交 1904cd8d 编写于 作者: M Matt Bierner

Prototype auto fixable quick fixes

Part of #62110

- Adds a new field `canAutoApply` to code actions. This field indicates that a code action can be applied without additional user input. For quick fixes, this should be set if the fix properly addresses the error

- Enable auto fixes for TS spelling errors

- Added a `editor.action.autoFix` command to triggers auto fixes at the current cursor position
上级 b76ea579
......@@ -240,7 +240,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
const results = new CodeActionSet();
for (const tsCodeFix of response.body) {
this.addAllFixesForTsCodeAction(results, document, file, diagnostic, tsCodeFix);
this.addAllFixesForTsCodeAction(results, document, file, diagnostic, tsCodeFix as Proto.CodeFixAction);
}
return results.values;
}
......@@ -250,7 +250,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
document: vscode.TextDocument,
file: string,
diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeAction
tsAction: Proto.CodeFixAction
): CodeActionSet {
results.addAction(this.getSingleFixForTsCodeAction(diagnostic, tsAction));
this.addFixAllForTsCodeAction(results, document, file, diagnostic, tsAction as Proto.CodeFixAction);
......@@ -259,7 +259,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
private getSingleFixForTsCodeAction(
diagnostic: vscode.Diagnostic,
tsAction: Proto.CodeAction
tsAction: Proto.CodeFixAction
): vscode.CodeAction {
const codeAction = new vscode.CodeAction(tsAction.description, vscode.CodeActionKind.QuickFix);
codeAction.edit = getEditForCodeAction(this.client, tsAction);
......@@ -269,6 +269,9 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
arguments: [tsAction],
title: ''
};
if (tsAction.fixName === 'spelling') {
codeAction.canAutoApply = true;
}
return codeAction;
}
......
......@@ -516,6 +516,7 @@ export interface CodeAction {
edit?: WorkspaceEdit;
diagnostics?: IMarkerData[];
kind?: string;
canAutoApply?: boolean;
}
/**
......
......@@ -15,7 +15,12 @@ import { CodeAction, CodeActionContext, CodeActionProviderRegistry, CodeActionTr
import { IModelService } from 'vs/editor/common/services/modelService';
import { CodeActionFilter, CodeActionKind, CodeActionTrigger } from './codeActionTrigger';
export function getCodeActions(model: ITextModel, rangeOrSelection: Range | Selection, trigger?: CodeActionTrigger, token: CancellationToken = CancellationToken.None): Promise<CodeAction[]> {
export function getCodeActions(
model: ITextModel,
rangeOrSelection: Range | Selection,
trigger?: CodeActionTrigger,
token: CancellationToken = CancellationToken.None
): Promise<CodeAction[]> {
const codeActionContext: CodeActionContext = {
only: trigger && trigger.filter && trigger.filter.kind ? trigger.filter.kind.value : undefined,
trigger: trigger && trigger.type === 'manual' ? CodeActionTriggerKind.Manual : CodeActionTriggerKind.Automatic
......@@ -65,7 +70,9 @@ export function getCodeActions(model: ITextModel, rangeOrSelection: Range | Sele
}
function isValidAction(filter: CodeActionFilter | undefined, action: CodeAction): boolean {
return action && isValidActionKind(filter, action.kind);
return action
&& isValidActionKind(filter, action.kind)
&& (filter && filter.autoFixesOnly ? !!action.canAutoApply : true);
}
function isValidActionKind(filter: CodeActionFilter | undefined, kind: string | undefined): boolean {
......
......@@ -358,3 +358,31 @@ export class OrganizeImportsAction extends EditorAction {
CodeActionAutoApply.IfSingle);
}
}
export class AutoFixAction extends EditorAction {
static readonly Id = 'editor.action.autoFix';
constructor() {
super({
id: AutoFixAction.Id,
label: nls.localize('autoFix.label', "Auto Fix"),
alias: 'Auto Fix',
precondition: ContextKeyExpr.and(
EditorContextKeys.writable,
contextKeyForSupportedActions(CodeActionKind.QuickFix)),
kbOpts: {
kbExpr: EditorContextKeys.editorTextFocus,
primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.US_DOT,
weight: KeybindingWeight.EditorContrib
}
});
}
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 },
CodeActionAutoApply.IfSingle);
}
}
......@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { registerEditorAction, registerEditorCommand, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { CodeActionCommand, OrganizeImportsAction, QuickFixAction, QuickFixController, RefactorAction, SourceAction } from 'vs/editor/contrib/codeAction/codeActionCommands';
import { CodeActionCommand, OrganizeImportsAction, QuickFixAction, QuickFixController, RefactorAction, SourceAction, AutoFixAction } from 'vs/editor/contrib/codeAction/codeActionCommands';
registerEditorContribution(QuickFixController);
......@@ -12,4 +12,5 @@ registerEditorAction(QuickFixAction);
registerEditorAction(RefactorAction);
registerEditorAction(SourceAction);
registerEditorAction(OrganizeImportsAction);
registerEditorAction(AutoFixAction);
registerEditorCommand(new CodeActionCommand());
......@@ -32,6 +32,7 @@ export const enum CodeActionAutoApply {
export interface CodeActionFilter {
readonly kind?: CodeActionKind;
readonly includeSourceActions?: boolean;
readonly autoFixesOnly?: boolean;
}
export interface CodeActionTrigger {
......
......@@ -4848,6 +4848,7 @@ declare namespace monaco.languages {
edit?: WorkspaceEdit;
diagnostics?: editor.IMarkerData[];
kind?: string;
canAutoApply?: boolean;
}
/**
......
......@@ -1098,4 +1098,17 @@ declare module 'vscode' {
readonly activeSignatureHelp?: SignatureHelp;
}
//#endregion
//#region CodeAction.canAutoApply - mjbvz
export interface CodeAction {
/**
* If the action can be safely automatically applied without the user selecting it from a list.
*
* Set this on quick fixes to indicate that the fix properly addresses the underlying error.
*/
canAutoApply?: boolean;
}
//#endregion
}
......@@ -864,6 +864,7 @@ export interface CodeActionDto {
diagnostics?: IMarkerData[];
command?: modes.Command;
kind?: string;
canAutoApply?: boolean;
}
export interface ExtHostLanguageFeaturesShape {
......
......@@ -342,7 +342,8 @@ class CodeActionAdapter {
command: candidate.command && this._commands.toInternal(candidate.command),
diagnostics: candidate.diagnostics && candidate.diagnostics.map(typeConvert.Diagnostic.from),
edit: candidate.edit && typeConvert.WorkspaceEdit.from(candidate.edit),
kind: candidate.kind && candidate.kind.value
kind: candidate.kind && candidate.kind.value,
canAutoApply: candidate.canAutoApply,
});
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册