Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/browser/actions/quickAccessActions.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { localize, localize2 } from '../../../nls.js';
7
import { MenuId, Action2, registerAction2 } from '../../../platform/actions/common/actions.js';
8
import { KeyMod, KeyCode } from '../../../base/common/keyCodes.js';
9
import { KeybindingsRegistry, KeybindingWeight, IKeybindingRule } from '../../../platform/keybinding/common/keybindingsRegistry.js';
10
import { IQuickInputService, ItemActivation } from '../../../platform/quickinput/common/quickInput.js';
11
import { IKeybindingService } from '../../../platform/keybinding/common/keybinding.js';
12
import { CommandsRegistry } from '../../../platform/commands/common/commands.js';
13
import { ServicesAccessor } from '../../../platform/instantiation/common/instantiation.js';
14
import { inQuickPickContext, defaultQuickAccessContext, getQuickNavigateHandler } from '../quickaccess.js';
15
import { ILocalizedString } from '../../../platform/action/common/action.js';
16
import { AnythingQuickAccessProviderRunOptions } from '../../../platform/quickinput/common/quickAccess.js';
17
import { Codicon } from '../../../base/common/codicons.js';
18
19
//#region Quick access management commands and keys
20
21
const globalQuickAccessKeybinding = {
22
primary: KeyMod.CtrlCmd | KeyCode.KeyP,
23
secondary: [KeyMod.CtrlCmd | KeyCode.KeyE],
24
mac: { primary: KeyMod.CtrlCmd | KeyCode.KeyP, secondary: undefined }
25
};
26
27
KeybindingsRegistry.registerCommandAndKeybindingRule({
28
id: 'workbench.action.closeQuickOpen',
29
weight: KeybindingWeight.WorkbenchContrib,
30
when: inQuickPickContext,
31
primary: KeyCode.Escape, secondary: [KeyMod.Shift | KeyCode.Escape],
32
handler: accessor => {
33
const quickInputService = accessor.get(IQuickInputService);
34
return quickInputService.cancel();
35
}
36
});
37
38
KeybindingsRegistry.registerCommandAndKeybindingRule({
39
id: 'workbench.action.acceptSelectedQuickOpenItem',
40
weight: KeybindingWeight.WorkbenchContrib,
41
when: inQuickPickContext,
42
primary: 0,
43
handler: accessor => {
44
const quickInputService = accessor.get(IQuickInputService);
45
return quickInputService.accept();
46
}
47
});
48
49
KeybindingsRegistry.registerCommandAndKeybindingRule({
50
id: 'workbench.action.alternativeAcceptSelectedQuickOpenItem',
51
weight: KeybindingWeight.WorkbenchContrib,
52
when: inQuickPickContext,
53
primary: 0,
54
handler: accessor => {
55
const quickInputService = accessor.get(IQuickInputService);
56
return quickInputService.accept({ ctrlCmd: true, alt: false });
57
}
58
});
59
60
KeybindingsRegistry.registerCommandAndKeybindingRule({
61
id: 'workbench.action.focusQuickOpen',
62
weight: KeybindingWeight.WorkbenchContrib,
63
when: inQuickPickContext,
64
primary: 0,
65
handler: accessor => {
66
const quickInputService = accessor.get(IQuickInputService);
67
quickInputService.focus();
68
}
69
});
70
71
const quickAccessNavigateNextInFilePickerId = 'workbench.action.quickOpenNavigateNextInFilePicker';
72
KeybindingsRegistry.registerCommandAndKeybindingRule({
73
id: quickAccessNavigateNextInFilePickerId,
74
weight: KeybindingWeight.WorkbenchContrib + 50,
75
handler: getQuickNavigateHandler(quickAccessNavigateNextInFilePickerId, true),
76
when: defaultQuickAccessContext,
77
primary: globalQuickAccessKeybinding.primary,
78
secondary: globalQuickAccessKeybinding.secondary,
79
mac: globalQuickAccessKeybinding.mac
80
});
81
82
const quickAccessNavigatePreviousInFilePickerId = 'workbench.action.quickOpenNavigatePreviousInFilePicker';
83
KeybindingsRegistry.registerCommandAndKeybindingRule({
84
id: quickAccessNavigatePreviousInFilePickerId,
85
weight: KeybindingWeight.WorkbenchContrib + 50,
86
handler: getQuickNavigateHandler(quickAccessNavigatePreviousInFilePickerId, false),
87
when: defaultQuickAccessContext,
88
primary: globalQuickAccessKeybinding.primary | KeyMod.Shift,
89
secondary: [globalQuickAccessKeybinding.secondary[0] | KeyMod.Shift],
90
mac: {
91
primary: globalQuickAccessKeybinding.mac.primary | KeyMod.Shift,
92
secondary: undefined
93
}
94
});
95
96
KeybindingsRegistry.registerCommandAndKeybindingRule({
97
id: 'workbench.action.quickPickManyToggle',
98
weight: KeybindingWeight.WorkbenchContrib,
99
when: inQuickPickContext,
100
primary: 0,
101
handler: accessor => {
102
const quickInputService = accessor.get(IQuickInputService);
103
quickInputService.toggle();
104
}
105
});
106
107
KeybindingsRegistry.registerCommandAndKeybindingRule({
108
id: 'workbench.action.quickInputBack',
109
weight: KeybindingWeight.WorkbenchContrib + 50,
110
when: inQuickPickContext,
111
primary: 0,
112
win: { primary: KeyMod.Alt | KeyCode.LeftArrow },
113
mac: { primary: KeyMod.WinCtrl | KeyCode.Minus },
114
linux: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.Minus },
115
handler: accessor => {
116
const quickInputService = accessor.get(IQuickInputService);
117
quickInputService.back();
118
}
119
});
120
121
registerAction2(class QuickAccessAction extends Action2 {
122
constructor() {
123
super({
124
id: 'workbench.action.quickOpen',
125
title: localize2('quickOpen', "Go to File..."),
126
metadata: {
127
description: `Quick access`,
128
args: [{
129
name: 'prefix',
130
schema: {
131
'type': 'string'
132
}
133
}]
134
},
135
keybinding: {
136
weight: KeybindingWeight.WorkbenchContrib,
137
primary: globalQuickAccessKeybinding.primary,
138
secondary: globalQuickAccessKeybinding.secondary,
139
mac: globalQuickAccessKeybinding.mac
140
},
141
f1: true
142
});
143
}
144
145
run(accessor: ServicesAccessor, prefix: undefined): void {
146
const quickInputService = accessor.get(IQuickInputService);
147
quickInputService.quickAccess.show(typeof prefix === 'string' ? prefix : undefined, { preserveValue: typeof prefix === 'string' /* preserve as is if provided */ });
148
}
149
});
150
151
registerAction2(class QuickAccessAction extends Action2 {
152
constructor() {
153
super({
154
id: 'workbench.action.quickOpenWithModes',
155
title: localize('quickOpenWithModes', "Quick Open"),
156
icon: Codicon.search,
157
menu: {
158
id: MenuId.CommandCenterCenter,
159
order: 100
160
}
161
});
162
}
163
164
run(accessor: ServicesAccessor): void {
165
const quickInputService = accessor.get(IQuickInputService);
166
const providerOptions: AnythingQuickAccessProviderRunOptions = {
167
includeHelp: true,
168
from: 'commandCenter',
169
};
170
quickInputService.quickAccess.show(undefined, {
171
preserveValue: true,
172
providerOptions
173
});
174
}
175
});
176
177
CommandsRegistry.registerCommand('workbench.action.quickOpenPreviousEditor', async accessor => {
178
const quickInputService = accessor.get(IQuickInputService);
179
180
quickInputService.quickAccess.show('', { itemActivation: ItemActivation.SECOND });
181
});
182
183
//#endregion
184
185
//#region Workbench actions
186
187
class BaseQuickAccessNavigateAction extends Action2 {
188
189
constructor(
190
private id: string,
191
title: ILocalizedString,
192
private next: boolean,
193
private quickNavigate: boolean,
194
keybinding?: Omit<IKeybindingRule, 'id'>
195
) {
196
super({ id, title, f1: true, keybinding });
197
}
198
199
async run(accessor: ServicesAccessor): Promise<void> {
200
const keybindingService = accessor.get(IKeybindingService);
201
const quickInputService = accessor.get(IQuickInputService);
202
203
const keys = keybindingService.lookupKeybindings(this.id);
204
const quickNavigate = this.quickNavigate ? { keybindings: keys } : undefined;
205
206
quickInputService.navigate(this.next, quickNavigate);
207
}
208
}
209
210
class QuickAccessNavigateNextAction extends BaseQuickAccessNavigateAction {
211
212
constructor() {
213
super('workbench.action.quickOpenNavigateNext', localize2('quickNavigateNext', 'Navigate Next in Quick Open'), true, true);
214
}
215
}
216
217
class QuickAccessNavigatePreviousAction extends BaseQuickAccessNavigateAction {
218
219
constructor() {
220
super('workbench.action.quickOpenNavigatePrevious', localize2('quickNavigatePrevious', 'Navigate Previous in Quick Open'), false, true);
221
}
222
}
223
224
class QuickAccessSelectNextAction extends BaseQuickAccessNavigateAction {
225
226
constructor() {
227
super(
228
'workbench.action.quickOpenSelectNext',
229
localize2('quickSelectNext', 'Select Next in Quick Open'),
230
true,
231
false,
232
{
233
weight: KeybindingWeight.WorkbenchContrib + 50,
234
when: inQuickPickContext,
235
primary: 0,
236
mac: { primary: KeyMod.WinCtrl | KeyCode.KeyN }
237
}
238
);
239
}
240
}
241
242
class QuickAccessSelectPreviousAction extends BaseQuickAccessNavigateAction {
243
244
constructor() {
245
super(
246
'workbench.action.quickOpenSelectPrevious',
247
localize2('quickSelectPrevious', 'Select Previous in Quick Open'),
248
false,
249
false,
250
{
251
weight: KeybindingWeight.WorkbenchContrib + 50,
252
when: inQuickPickContext,
253
primary: 0,
254
mac: { primary: KeyMod.WinCtrl | KeyCode.KeyP }
255
}
256
);
257
}
258
}
259
260
registerAction2(QuickAccessSelectNextAction);
261
registerAction2(QuickAccessSelectPreviousAction);
262
registerAction2(QuickAccessNavigateNextAction);
263
registerAction2(QuickAccessNavigatePreviousAction);
264
265
//#endregion
266
267