Skip to content

Commit 5a42e22

Browse files
committed
feat: add CommandQuickAccessProvider and CommandQuickAccessViewAction
1 parent f891935 commit 5a42e22

File tree

1 file changed

+202
-0
lines changed

1 file changed

+202
-0
lines changed

src/monaco/quickAccessViewAction.ts

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import 'reflect-metadata';
2+
import { localize } from 'monaco-editor/esm/vs/nls';
3+
import { DisposableStore } from 'monaco-editor/esm/vs/base/common/lifecycle';
4+
import { QuickCommandNLS } from 'monaco-editor/esm/vs/editor/common/standaloneStrings';
5+
import { CancellationToken } from 'monaco-editor/esm/vs/base/common/cancellation';
6+
import { IQuickInputService } from 'monaco-editor/esm/vs/platform/quickinput/common/quickInput';
7+
import {
8+
IQuickAccessRegistry,
9+
Extensions,
10+
} from 'monaco-editor/esm/vs/platform/quickinput/common/quickAccess';
11+
import { ICommandQuickPick } from 'monaco-editor/esm/vs/platform/quickinput/browser/commandsQuickAccess';
12+
import { AbstractEditorCommandsQuickAccessProvider } from 'monaco-editor/esm/vs/editor/contrib/quickAccess/commandsQuickAccess';
13+
import { IInstantiationService } from 'monaco-editor/esm/vs/platform/instantiation/common/instantiation';
14+
import { IKeybindingService } from 'monaco-editor/esm/vs/platform/keybinding/common/keybinding';
15+
import { ICommandService } from 'monaco-editor/esm/vs/platform/commands/common/commands';
16+
import { ITelemetryService } from 'monaco-editor/esm/vs/platform/telemetry/common/telemetry';
17+
import { INotificationService } from 'monaco-editor/esm/vs/platform/notification/common/notification';
18+
import { Registry } from 'monaco-editor/esm/vs/platform/registry/common/platform';
19+
import { Codicon } from 'monaco-editor/esm/vs/base/common/codicons';
20+
import { TriggerAction } from 'monaco-editor/esm/vs/platform/quickinput/browser/pickerQuickAccess';
21+
import {
22+
MenuId,
23+
MenuRegistry,
24+
} from 'monaco-editor/esm/vs/platform/actions/common/actions';
25+
import { stripIcons } from 'monaco-editor/esm/vs/base/common/iconLabels';
26+
27+
import { IMonacoService, MonacoService } from './monacoService';
28+
import { KeyMod, KeyCode } from 'mo/monaco';
29+
import { container } from 'tsyringe';
30+
import { Action2, KeybindingWeight } from './common';
31+
import { EditorService, IEditorService } from 'mo/services';
32+
33+
const monacoService = container.resolve<IMonacoService>(MonacoService);
34+
35+
export class CommandQuickAccessProvider extends AbstractEditorCommandsQuickAccessProvider {
36+
static PREFIX = '>';
37+
protected readonly editorService: IEditorService | undefined;
38+
protected get activeTextEditorControl(): IStandaloneCodeEditor | undefined {
39+
return this.editorService?.editorInstance;
40+
}
41+
42+
constructor() {
43+
super(
44+
{
45+
showAlias: false,
46+
noResultsPick: {
47+
label: localize('noCommandResults', 'No matching commands'),
48+
commandId: '',
49+
},
50+
},
51+
monacoService.services.get(IInstantiationService),
52+
monacoService.services.get(IKeybindingService),
53+
monacoService.services.get(ICommandService),
54+
monacoService.services.get(ITelemetryService),
55+
monacoService.services.get(INotificationService)
56+
);
57+
this.editorService = container.resolve(EditorService);
58+
}
59+
60+
protected async getCommandPicks(
61+
disposables: DisposableStore,
62+
token: CancellationToken
63+
): Promise<Array<ICommandQuickPick>> {
64+
if (token.isCancellationRequested) {
65+
return [];
66+
}
67+
68+
return [
69+
...(<AbstractEditorCommandsQuickAccessProvider>(
70+
this
71+
)).getCodeEditorCommandPicks(),
72+
...this.getGlobalCommandPicks(disposables),
73+
].map((c) => ({
74+
...c,
75+
buttons: [
76+
{
77+
iconClass: Codicon.gear.classNames,
78+
tooltip: localize(
79+
'configure keybinding',
80+
'Configure Keybinding'
81+
),
82+
},
83+
],
84+
trigger: (): TriggerAction => {
85+
return TriggerAction.CLOSE_PICKER;
86+
},
87+
}));
88+
}
89+
90+
protected getGlobalCommandPicks(
91+
disposables: DisposableStore
92+
): ICommandQuickPick[] {
93+
const globalCommandPicks: ICommandQuickPick[] = [];
94+
const globalCommandsMenu = MenuRegistry.getMenuItems(
95+
MenuId.CommandPalette
96+
);
97+
98+
for (const menu of globalCommandsMenu) {
99+
// Label
100+
let label =
101+
(typeof menu.command.title === 'string'
102+
? menu.command.title
103+
: menu.command.title.value) || menu.command.id;
104+
105+
// Category
106+
const category =
107+
typeof menu.command.category === 'string'
108+
? menu.command.category
109+
: menu.command.category?.value;
110+
if (category) {
111+
label = localize(
112+
'commandWithCategory',
113+
'{0}: {1}',
114+
category,
115+
label
116+
);
117+
}
118+
119+
// Alias
120+
const aliasLabel =
121+
typeof menu.command.title !== 'string'
122+
? menu.command.title.original
123+
: undefined;
124+
const aliasCategory =
125+
category &&
126+
menu.command.category &&
127+
typeof menu.command.category !== 'string'
128+
? menu.command.category.original
129+
: undefined;
130+
const commandAlias =
131+
aliasLabel && category
132+
? aliasCategory
133+
? `${aliasCategory}: ${aliasLabel}`
134+
: `${category}: ${aliasLabel}`
135+
: aliasLabel;
136+
137+
globalCommandPicks.push({
138+
commandId: menu.command.id,
139+
commandAlias,
140+
label: stripIcons(label),
141+
});
142+
}
143+
144+
return globalCommandPicks;
145+
}
146+
}
147+
148+
Registry.as<IQuickAccessRegistry>(
149+
Extensions.Quickaccess
150+
).registerQuickAccessProvider({
151+
ctor: CommandQuickAccessProvider,
152+
prefix: CommandQuickAccessProvider.PREFIX,
153+
placeholder: localize(
154+
'commandsQuickAccessPlaceholder',
155+
'Type the name of a command to run.'
156+
),
157+
helpEntries: [
158+
{
159+
description: localize(
160+
'commandsQuickAccess',
161+
'Show and Run Commands'
162+
),
163+
needsEditor: false,
164+
},
165+
],
166+
});
167+
168+
export class CommandQuickAccessViewAction extends Action2 {
169+
static ID = 'workbench.action.quickCommand';
170+
private readonly quickInputService: IQuickInputService;
171+
172+
constructor() {
173+
super({
174+
id: CommandQuickAccessViewAction.ID,
175+
label: QuickCommandNLS.quickCommandActionLabel,
176+
alias: 'Command Palette',
177+
title: {
178+
value: localize('showTriggerActions', 'Command Palette'),
179+
original: 'Command Palette',
180+
},
181+
f1: false,
182+
keybinding: {
183+
weight: KeybindingWeight.WorkbenchContrib,
184+
when: undefined,
185+
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_P,
186+
secondary: [KeyCode.F1],
187+
},
188+
menu: {
189+
id: MenuId.EditorContext,
190+
group: 'z_commands',
191+
order: 1,
192+
},
193+
});
194+
this.quickInputService = monacoService.services.get(IQuickInputService);
195+
}
196+
197+
run(): void {
198+
this.quickInputService.quickAccess.show(
199+
CommandQuickAccessProvider.PREFIX
200+
);
201+
}
202+
}

0 commit comments

Comments
 (0)