-
Notifications
You must be signed in to change notification settings - Fork 30k
/
codeActionMenu.ts
83 lines (75 loc) · 3.73 KB
/
codeActionMenu.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import 'vs/base/browser/ui/codicons/codiconStyles'; // The codicon symbol styles are defined here and must be loaded
import { Codicon } from 'vs/base/common/codicons';
import { ThemeIcon } from 'vs/base/common/themables';
import { ResolvedKeybinding } from 'vs/base/common/keybindings';
import { CodeAction } from 'vs/editor/common/languages';
import { CodeActionItem, CodeActionKind } from 'vs/editor/contrib/codeAction/common/types';
import 'vs/editor/contrib/symbolIcons/browser/symbolIcons'; // The codicon symbol colors are defined here and must be loaded to get colors
import { localize } from 'vs/nls';
import { ActionListItemKind, IActionListItem } from 'vs/platform/actionWidget/browser/actionList';
interface ActionGroup {
readonly kind: CodeActionKind;
readonly title: string;
readonly icon?: ThemeIcon;
}
const uncategorizedCodeActionGroup = Object.freeze<ActionGroup>({ kind: CodeActionKind.Empty, title: localize('codeAction.widget.id.more', 'More Actions...') });
const codeActionGroups = Object.freeze<ActionGroup[]>([
{ kind: CodeActionKind.QuickFix, title: localize('codeAction.widget.id.quickfix', 'Quick Fix') },
{ kind: CodeActionKind.RefactorExtract, title: localize('codeAction.widget.id.extract', 'Extract'), icon: Codicon.wrench },
{ kind: CodeActionKind.RefactorInline, title: localize('codeAction.widget.id.inline', 'Inline'), icon: Codicon.wrench },
{ kind: CodeActionKind.RefactorRewrite, title: localize('codeAction.widget.id.convert', 'Rewrite'), icon: Codicon.wrench },
{ kind: CodeActionKind.RefactorMove, title: localize('codeAction.widget.id.move', 'Move'), icon: Codicon.wrench },
{ kind: CodeActionKind.SurroundWith, title: localize('codeAction.widget.id.surround', 'Surround With'), icon: Codicon.symbolSnippet },
{ kind: CodeActionKind.Source, title: localize('codeAction.widget.id.source', 'Source Action'), icon: Codicon.symbolFile },
uncategorizedCodeActionGroup,
]);
export function toMenuItems(
inputCodeActions: readonly CodeActionItem[],
showHeaders: boolean,
keybindingResolver: (action: CodeAction) => ResolvedKeybinding | undefined
): IActionListItem<CodeActionItem>[] {
if (!showHeaders) {
return inputCodeActions.map((action): IActionListItem<CodeActionItem> => {
return {
kind: ActionListItemKind.Action,
item: action,
group: uncategorizedCodeActionGroup,
disabled: !!action.action.disabled,
label: action.action.disabled || action.action.title,
canPreview: !!action.action.edit?.edits.length,
};
});
}
// Group code actions
const menuEntries = codeActionGroups.map(group => ({ group, actions: [] as CodeActionItem[] }));
for (const action of inputCodeActions) {
const kind = action.action.kind ? new CodeActionKind(action.action.kind) : CodeActionKind.None;
for (const menuEntry of menuEntries) {
if (menuEntry.group.kind.contains(kind)) {
menuEntry.actions.push(action);
break;
}
}
}
const allMenuItems: IActionListItem<CodeActionItem>[] = [];
for (const menuEntry of menuEntries) {
if (menuEntry.actions.length) {
allMenuItems.push({ kind: ActionListItemKind.Header, group: menuEntry.group });
for (const action of menuEntry.actions) {
allMenuItems.push({
kind: ActionListItemKind.Action,
item: action,
group: menuEntry.group,
label: action.action.title,
disabled: !!action.action.disabled,
keybinding: keybindingResolver(action.action),
});
}
}
}
return allMenuItems;
}