12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- import { Emitter } from '../../../base/common/event.js';
- import { Iterable } from '../../../base/common/iterator.js';
- import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
- import { LinkedList } from '../../../base/common/linkedList.js';
- import { validateConstraints } from '../../../base/common/types.js';
- import { createDecorator } from '../../instantiation/common/instantiation.js';
- export const ICommandService = createDecorator('commandService');
- export const CommandsRegistry = new class {
- constructor() {
- this._commands = new Map();
- this._onDidRegisterCommand = new Emitter();
- this.onDidRegisterCommand = this._onDidRegisterCommand.event;
- }
- registerCommand(idOrCommand, handler) {
- if (!idOrCommand) {
- throw new Error(`invalid command`);
- }
- if (typeof idOrCommand === 'string') {
- if (!handler) {
- throw new Error(`invalid command`);
- }
- return this.registerCommand({ id: idOrCommand, handler });
- }
- // add argument validation if rich command metadata is provided
- if (idOrCommand.description) {
- const constraints = [];
- for (let arg of idOrCommand.description.args) {
- constraints.push(arg.constraint);
- }
- const actualHandler = idOrCommand.handler;
- idOrCommand.handler = function (accessor, ...args) {
- validateConstraints(args, constraints);
- return actualHandler(accessor, ...args);
- };
- }
- // find a place to store the command
- const { id } = idOrCommand;
- let commands = this._commands.get(id);
- if (!commands) {
- commands = new LinkedList();
- this._commands.set(id, commands);
- }
- let removeFn = commands.unshift(idOrCommand);
- let ret = toDisposable(() => {
- removeFn();
- const command = this._commands.get(id);
- if (command === null || command === void 0 ? void 0 : command.isEmpty()) {
- this._commands.delete(id);
- }
- });
- // tell the world about this command
- this._onDidRegisterCommand.fire(id);
- return ret;
- }
- registerCommandAlias(oldId, newId) {
- return CommandsRegistry.registerCommand(oldId, (accessor, ...args) => accessor.get(ICommandService).executeCommand(newId, ...args));
- }
- getCommand(id) {
- const list = this._commands.get(id);
- if (!list || list.isEmpty()) {
- return undefined;
- }
- return Iterable.first(list);
- }
- getCommands() {
- const result = new Map();
- for (const key of this._commands.keys()) {
- const command = this.getCommand(key);
- if (command) {
- result.set(key, command);
- }
- }
- return result;
- }
- };
- export const NullCommandService = {
- _serviceBrand: undefined,
- onWillExecuteCommand: () => Disposable.None,
- onDidExecuteCommand: () => Disposable.None,
- executeCommand() {
- return Promise.resolve(undefined);
- }
- };
- CommandsRegistry.registerCommand('noop', () => { });
|