提交 f0c9a475 编写于 作者: M Matt Bierner

Allow using arguments in keybindings for `editor.action.refactor` and `editor.action.sourceAction`

Fixes #64824
上级 580c7966
...@@ -206,33 +206,28 @@ export class QuickFixAction extends EditorAction { ...@@ -206,33 +206,28 @@ export class QuickFixAction extends EditorAction {
class CodeActionCommandArgs { class CodeActionCommandArgs {
public static fromUser(arg: any): CodeActionCommandArgs { public static fromUser(arg: any, defaults: { kind: CodeActionKind, apply: CodeActionAutoApply }): CodeActionCommandArgs {
if (!arg || typeof arg !== 'object') { if (!arg || typeof arg !== 'object') {
return new CodeActionCommandArgs(CodeActionKind.Empty, CodeActionAutoApply.IfSingle); return new CodeActionCommandArgs(defaults.kind, defaults.apply);
} }
return new CodeActionCommandArgs( return new CodeActionCommandArgs(
CodeActionCommandArgs.getKindFromUser(arg), CodeActionCommandArgs.getKindFromUser(arg, defaults.kind),
CodeActionCommandArgs.getApplyFromUser(arg)); CodeActionCommandArgs.getApplyFromUser(arg, defaults.apply));
} }
private static getApplyFromUser(arg: any) { private static getApplyFromUser(arg: any, defaultAutoApply: CodeActionAutoApply) {
switch (typeof arg.apply === 'string' ? arg.apply.toLowerCase() : '') { switch (typeof arg.apply === 'string' ? arg.apply.toLowerCase() : '') {
case 'first': case 'first': return CodeActionAutoApply.First;
return CodeActionAutoApply.First; case 'never': return CodeActionAutoApply.Never;
case 'ifsingle': return CodeActionAutoApply.IfSingle;
case 'never': default: return defaultAutoApply;
return CodeActionAutoApply.Never;
case 'ifsingle':
default:
return CodeActionAutoApply.IfSingle;
} }
} }
private static getKindFromUser(arg: any) { private static getKindFromUser(arg: any, defaultKind: CodeActionKind) {
return typeof arg.kind === 'string' return typeof arg.kind === 'string'
? new CodeActionKind(arg.kind) ? new CodeActionKind(arg.kind)
: CodeActionKind.Empty; : defaultKind;
} }
private constructor( private constructor(
...@@ -253,7 +248,10 @@ export class CodeActionCommand extends EditorCommand { ...@@ -253,7 +248,10 @@ export class CodeActionCommand extends EditorCommand {
} }
public runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor, userArg: any) { public runEditorCommand(_accessor: ServicesAccessor, editor: ICodeEditor, userArg: any) {
const args = CodeActionCommandArgs.fromUser(userArg); const args = CodeActionCommandArgs.fromUser(userArg, {
kind: CodeActionKind.Empty,
apply: CodeActionAutoApply.IfSingle,
});
return showCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"), { kind: args.kind, includeSourceActions: true }, args.apply); return showCodeActionsForEditorSelection(editor, nls.localize('editor.action.quickFix.noneMessage', "No code actions available"), { kind: args.kind, includeSourceActions: true }, args.apply);
} }
} }
...@@ -287,11 +285,15 @@ export class RefactorAction extends EditorAction { ...@@ -287,11 +285,15 @@ export class RefactorAction extends EditorAction {
}); });
} }
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void { public run(_accessor: ServicesAccessor, editor: ICodeEditor, userArg: any): void {
const args = CodeActionCommandArgs.fromUser(userArg, {
kind: CodeActionKind.Refactor,
apply: CodeActionAutoApply.Never
});
return showCodeActionsForEditorSelection(editor, return showCodeActionsForEditorSelection(editor,
nls.localize('editor.action.refactor.noneMessage', "No refactorings available"), nls.localize('editor.action.refactor.noneMessage', "No refactorings available"),
{ kind: CodeActionKind.Refactor }, { kind: CodeActionKind.Refactor.contains(args.kind.value) ? args.kind : CodeActionKind.Empty },
CodeActionAutoApply.Never); args.apply);
} }
} }
...@@ -316,11 +318,15 @@ export class SourceAction extends EditorAction { ...@@ -316,11 +318,15 @@ export class SourceAction extends EditorAction {
}); });
} }
public run(_accessor: ServicesAccessor, editor: ICodeEditor): void { public run(_accessor: ServicesAccessor, editor: ICodeEditor, userArg: any): void {
const args = CodeActionCommandArgs.fromUser(userArg, {
kind: CodeActionKind.Source,
apply: CodeActionAutoApply.Never
});
return showCodeActionsForEditorSelection(editor, return showCodeActionsForEditorSelection(editor,
nls.localize('editor.action.source.noneMessage', "No source actions available"), nls.localize('editor.action.source.noneMessage', "No source actions available"),
{ kind: CodeActionKind.Source, includeSourceActions: true }, { kind: CodeActionKind.Source.contains(args.kind.value) ? args.kind : CodeActionKind.Empty, includeSourceActions: true },
CodeActionAutoApply.Never); args.apply);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册