-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcommand.ts
444 lines (405 loc) · 14.6 KB
/
command.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
import { injectable, inject, named } from 'inversify';
import { Event, Emitter, WaitUntilEvent } from './event';
import { Disposable, DisposableCollection } from './disposable';
import { ContributionProvider } from './contribution-provider';
/**
* A command is a unique identifier of a function
* which can be executed by a user via a keyboard shortcut,
* a menu action or directly.
*/
export interface Command {
/**
* A unique identifier of this command.
*/
id: string;
/**
* A label of this command.
*/
label?: string;
/**
* An icon class of this command.
*/
iconClass?: string;
/**
* A category of this command.
*/
category?: string;
}
export namespace Command {
/* Determine whether object is a Command */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function is(arg: Command | any): arg is Command {
return !!arg && arg === Object(arg) && 'id' in arg;
}
/** Comparator function for when sorting commands */
export function compareCommands(a: Command, b: Command): number {
if (a.label && b.label) {
const aCommand = (a.category ? `${a.category}: ${a.label}` : a.label).toLowerCase();
const bCommand = (b.category ? `${b.category}: ${b.label}` : b.label).toLowerCase();
return (aCommand).localeCompare(bCommand);
} else {
return 0;
}
}
/**
* Determine if two commands are equal.
*
* @param a the first command for comparison.
* @param b the second command for comparison.
*/
export function equals(a: Command, b: Command): boolean {
return (
a.id === b.id &&
a.label === b.label &&
a.iconClass === b.iconClass &&
a.category === b.category
);
}
}
/**
* A command handler is an implementation of a command.
*
* A command can have multiple handlers
* but they should be active in different contexts,
* otherwise first active will be executed.
*/
export interface CommandHandler {
/**
* Execute this handler.
*
* Don't call it directly, use `CommandService.executeCommand` instead.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
execute(...args: any[]): any;
/**
* Test whether this handler is enabled (active).
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isEnabled?(...args: any[]): boolean;
/**
* Test whether menu items for this handler should be visible.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isVisible?(...args: any[]): boolean;
/**
* Test whether menu items for this handler should be toggled.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isToggled?(...args: any[]): boolean;
}
export const CommandContribution = Symbol('CommandContribution');
/**
* The command contribution should be implemented to register custom commands and handler.
*/
export interface CommandContribution {
/**
* Register commands and handlers.
*/
registerCommands(commands: CommandRegistry): void;
}
export interface CommandEvent {
commandId: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
args: any[]
}
export interface WillExecuteCommandEvent extends WaitUntilEvent, CommandEvent {
}
export const commandServicePath = '/services/commands';
export const CommandService = Symbol('CommandService');
/**
* The command service should be used to execute commands.
*/
export interface CommandService {
/**
* Execute the active handler for the given command and arguments.
*
* Reject if a command cannot be executed.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
executeCommand<T>(command: string, ...args: any[]): Promise<T | undefined>;
/**
* An event is emitted when a command is about to be executed.
*
* It can be used to install or activate a command handler.
*/
readonly onWillExecuteCommand: Event<WillExecuteCommandEvent>;
/**
* An event is emitted when a command was executed.
*/
readonly onDidExecuteCommand: Event<CommandEvent>;
}
/**
* The command registry manages commands and handlers.
*/
@injectable()
export class CommandRegistry implements CommandService {
protected readonly _commands: { [id: string]: Command } = {};
protected readonly _handlers: { [id: string]: CommandHandler[] } = {};
protected readonly toUnregisterCommands = new Map<string, Disposable>();
// List of recently used commands.
protected _recent: Command[] = [];
protected readonly onWillExecuteCommandEmitter = new Emitter<WillExecuteCommandEvent>();
readonly onWillExecuteCommand = this.onWillExecuteCommandEmitter.event;
protected readonly onDidExecuteCommandEmitter = new Emitter<CommandEvent>();
readonly onDidExecuteCommand = this.onDidExecuteCommandEmitter.event;
constructor(
@inject(ContributionProvider) @named(CommandContribution)
protected readonly contributionProvider: ContributionProvider<CommandContribution>
) { }
onStart(): void {
const contributions = this.contributionProvider.getContributions();
for (const contrib of contributions) {
contrib.registerCommands(this);
}
}
/**
* Register the given command and handler if present.
*
* Throw if a command is already registered for the given command identifier.
*/
registerCommand(command: Command, handler?: CommandHandler): Disposable {
if (this._commands[command.id]) {
console.warn(`A command ${command.id} is already registered.`);
return Disposable.NULL;
}
const toDispose = new DisposableCollection(this.doRegisterCommand(command));
if (handler) {
toDispose.push(this.registerHandler(command.id, handler));
}
this.toUnregisterCommands.set(command.id, toDispose);
toDispose.push(Disposable.create(() => this.toUnregisterCommands.delete(command.id)));
return toDispose;
}
protected doRegisterCommand(command: Command): Disposable {
this._commands[command.id] = command;
return {
dispose: () => {
delete this._commands[command.id];
}
};
}
/**
* Unregister command from the registry
*
* @param command
*/
unregisterCommand(command: Command): void;
/**
* Unregister command from the registry
*
* @param id
*/
unregisterCommand(id: string): void;
unregisterCommand(commandOrId: Command | string): void {
const id = Command.is(commandOrId) ? commandOrId.id : commandOrId;
const toUnregister = this.toUnregisterCommands.get(id);
if (toUnregister) {
toUnregister.dispose();
}
}
/**
* Register the given handler for the given command identifier.
*
* If there is already a handler for the given command
* then the given handler is registered as more specific, and
* has higher priority during enablement, visibility and toggle state evaluations.
*/
registerHandler(commandId: string, handler: CommandHandler): Disposable {
let handlers = this._handlers[commandId];
if (!handlers) {
this._handlers[commandId] = handlers = [];
}
handlers.unshift(handler);
return {
dispose: () => {
const idx = handlers.indexOf(handler);
if (idx >= 0) {
handlers.splice(idx, 1);
}
}
};
}
/**
* Test whether there is an active handler for the given command.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isEnabled(command: string, ...args: any[]): boolean {
return typeof this.getActiveHandler(command, ...args) !== 'undefined';
}
/**
* Test whether there is a visible handler for the given command.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isVisible(command: string, ...args: any[]): boolean {
return typeof this.getVisibleHandler(command, ...args) !== 'undefined';
}
/**
* Test whether there is a toggled handler for the given command.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isToggled(command: string, ...args: any[]): boolean {
return typeof this.getToggledHandler(command, ...args) !== 'undefined';
}
/**
* Execute the active handler for the given command and arguments.
*
* Reject if a command cannot be executed.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async executeCommand<T>(commandId: string, ...args: any[]): Promise<T | undefined> {
const handler = this.getActiveHandler(commandId, ...args);
if (handler) {
await this.fireWillExecuteCommand(commandId, args);
const result = await handler.execute(...args);
this.onDidExecuteCommandEmitter.fire({ commandId, args });
return result;
}
throw Object.assign(new Error(`The command '${commandId}' cannot be executed. There are no active handlers available for the command.`), { code: 'NO_ACTIVE_HANDLER' });
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected async fireWillExecuteCommand(commandId: string, args: any[] = []): Promise<void> {
await WaitUntilEvent.fire(this.onWillExecuteCommandEmitter, { commandId, args }, 30000);
}
/**
* Get a visible handler for the given command or `undefined`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getVisibleHandler(commandId: string, ...args: any[]): CommandHandler | undefined {
const handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
try {
if (!handler.isVisible || handler.isVisible(...args)) {
return handler;
}
} catch (error) {
console.error(error);
}
}
}
return undefined;
}
/**
* Get an active handler for the given command or `undefined`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getActiveHandler(commandId: string, ...args: any[]): CommandHandler | undefined {
const handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
try {
if (!handler.isEnabled || handler.isEnabled(...args)) {
return handler;
}
} catch (error) {
console.error(error);
}
}
}
return undefined;
}
/**
* Get a toggled handler for the given command or `undefined`.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getToggledHandler(commandId: string, ...args: any[]): CommandHandler | undefined {
const handlers = this._handlers[commandId];
if (handlers) {
for (const handler of handlers) {
try {
if (handler.isToggled && handler.isToggled(...args)) {
return handler;
}
} catch (error) {
console.error(error);
}
}
}
return undefined;
}
/**
* Returns with all handlers for the given command. If the command does not have any handlers,
* or the command is not registered, returns an empty array.
*/
getAllHandlers(commandId: string): CommandHandler[] {
const handlers = this._handlers[commandId];
return handlers ? handlers.slice() : [];
}
/**
* Get all registered commands.
*/
get commands(): Command[] {
const commands: Command[] = [];
for (const id of this.commandIds) {
const cmd = this.getCommand(id);
if (cmd) {
commands.push(cmd);
}
}
return commands;
}
/**
* Get a command for the given command identifier.
*/
getCommand(id: string): Command | undefined {
return this._commands[id];
}
/**
* Get all registered commands identifiers.
*/
get commandIds(): string[] {
return Object.keys(this._commands);
}
/**
* Get the list of recently used commands.
*/
get recent(): Command[] {
return this._recent;
}
/**
* Set the list of recently used commands.
* @param commands the list of recently used commands.
*/
set recent(commands: Command[]) {
this._recent = commands;
}
/**
* Adds a command to recently used list.
* Prioritizes commands that were recently executed to be most recent.
*
* @param recent a recent command, or array of recent commands.
*/
addRecentCommand(recent: Command | Command[]): void {
if (Array.isArray(recent)) {
recent.forEach((command: Command) => this.addRecentCommand(command));
} else {
// Determine if the command currently exists in the recently used list.
const index = this._recent.findIndex((command: Command) => Command.equals(recent, command));
// If the command exists, remove it from the array so it can later be placed at the top.
if (index >= 0) { this._recent.splice(index, 1); }
// Add the recent command to the beginning of the array (most recent).
this._recent.unshift(recent);
}
}
/**
* Clear the list of recently used commands.
*/
clearCommandHistory(): void {
this.recent = [];
}
}