-
Notifications
You must be signed in to change notification settings - Fork 120
/
common.ts
120 lines (111 loc) · 3.53 KB
/
common.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
IDisposable,
DisposableStore,
} from 'monaco-editor/esm/vs/base/common/lifecycle';
import { ContextKeyExpr } from 'monaco-editor/esm/vs/platform/contextkey/common/contextkey';
import { KeybindingsRegistry } from 'monaco-editor/esm/vs/platform/keybinding/common/keybindingsRegistry';
import { ServicesAccessor } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
import { CommandsRegistry } from 'monaco-editor/esm/vs/platform/commands/common/commands';
import { localize } from 'monaco-editor/esm/vs/nls';
import {
MenuRegistry,
MenuId,
} from 'monaco-editor/esm/vs/platform/actions/common/actions';
export enum KeybindingWeight {
EditorCore = 0,
EditorContrib = 100,
WorkbenchContrib = 200,
BuiltinExtension = 300,
ExternalExtension = 400,
}
export const CATEGORIES = {
View: { value: localize('view', 'View'), original: 'View' },
Help: { value: localize('help', 'Help'), original: 'Help' },
Preferences: {
value: localize('preferences', 'Preferences'),
original: 'Preferences',
},
Developer: {
value: localize(
{
key: 'developer',
comment: [
'A developer on Code itself or someone diagnosing issues in Code',
],
},
'Developer'
),
original: 'Developer',
},
};
export abstract class Action2 {
constructor(
readonly desc: Readonly<{
/**
* Specify visible in quick access view
*/
f1: boolean;
[key: string]: any;
}>
) {}
abstract run(accessor: ServicesAccessor, ...args: any[]): any;
}
export function registerAction2(ctor: { new (): Action2 }): IDisposable {
const disposables = new DisposableStore();
// eslint-disable-next-line new-cap
const action = new ctor();
const { f1, menu, keybinding, description, ...command } = action.desc;
// command
disposables.add(
CommandsRegistry.registerCommand({
id: command.id,
handler: (accessor, ...args) => action.run(accessor, ...args),
description: description,
})
);
// menu
if (Array.isArray(menu)) {
disposables.add(
MenuRegistry.appendMenuItems(
menu.map((item) => ({
id: item.id,
item: { command, ...item },
}))
)
);
} else if (menu) {
disposables.add(
MenuRegistry.appendMenuItem(menu.id, { command, ...menu })
);
}
if (f1) {
disposables.add(
MenuRegistry.appendMenuItem(MenuId.CommandPalette, {
command,
when: command.precondition,
})
);
disposables.add(MenuRegistry.addCommand(command));
}
// keybinding
if (Array.isArray(keybinding)) {
for (const item of keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...item,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, item.when)
: item.when,
});
}
} else if (keybinding) {
KeybindingsRegistry.registerKeybindingRule({
...keybinding,
id: command.id,
when: command.precondition
? ContextKeyExpr.and(command.precondition, keybinding.when)
: keybinding.when,
});
}
return disposables;
}