|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// tslint:disable:no-any |
| 5 | + |
| 6 | +import { injectable } from 'inversify'; |
| 7 | +import { commands, Disposable, TextEditor, TextEditorEdit } from 'vscode'; |
| 8 | +import { ICommandManager } from './types'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class CommandManager implements ICommandManager { |
| 12 | + |
| 13 | + /** |
| 14 | + * Registers a command that can be invoked via a keyboard shortcut, |
| 15 | + * a menu item, an action, or directly. |
| 16 | + * |
| 17 | + * Registering a command with an existing command identifier twice |
| 18 | + * will cause an error. |
| 19 | + * |
| 20 | + * @param command A unique identifier for the command. |
| 21 | + * @param callback A command handler function. |
| 22 | + * @param thisArg The `this` context used when invoking the handler function. |
| 23 | + * @return Disposable which unregisters this command on disposal. |
| 24 | + */ |
| 25 | + public registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): Disposable { |
| 26 | + return commands.registerCommand(command, callback, thisArg); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Registers a text editor command that can be invoked via a keyboard shortcut, |
| 31 | + * a menu item, an action, or directly. |
| 32 | + * |
| 33 | + * Text editor commands are different from ordinary [commands](#commands.registerCommand) as |
| 34 | + * they only execute when there is an active editor when the command is called. Also, the |
| 35 | + * command handler of an editor command has access to the active editor and to an |
| 36 | + * [edit](#TextEditorEdit)-builder. |
| 37 | + * |
| 38 | + * @param command A unique identifier for the command. |
| 39 | + * @param callback A command handler function with access to an [editor](#TextEditor) and an [edit](#TextEditorEdit). |
| 40 | + * @param thisArg The `this` context used when invoking the handler function. |
| 41 | + * @return Disposable which unregisters this command on disposal. |
| 42 | + */ |
| 43 | + public registerTextEditorCommand(command: string, callback: (textEditor: TextEditor, edit: TextEditorEdit, ...args: any[]) => void, thisArg?: any): Disposable { |
| 44 | + return commands.registerTextEditorCommand(command, callback, thisArg); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Executes the command denoted by the given command identifier. |
| 49 | + * |
| 50 | + * * *Note 1:* When executing an editor command not all types are allowed to |
| 51 | + * be passed as arguments. Allowed are the primitive types `string`, `boolean`, |
| 52 | + * `number`, `undefined`, and `null`, as well as [`Position`](#Position), [`Range`](#Range), [`Uri`](#Uri) and [`Location`](#Location). |
| 53 | + * * *Note 2:* There are no restrictions when executing commands that have been contributed |
| 54 | + * by extensions. |
| 55 | + * |
| 56 | + * @param command Identifier of the command to execute. |
| 57 | + * @param rest Parameters passed to the command function. |
| 58 | + * @return A thenable that resolves to the returned value of the given command. `undefined` when |
| 59 | + * the command handler function doesn't return anything. |
| 60 | + */ |
| 61 | + public executeCommand<T>(command: string, ...rest: any[]): Thenable<T | undefined> { |
| 62 | + return commands.executeCommand<T>(command, ...rest); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Retrieve the list of all available commands. Commands starting an underscore are |
| 67 | + * treated as internal commands. |
| 68 | + * |
| 69 | + * @param filterInternal Set `true` to not see internal commands (starting with an underscore) |
| 70 | + * @return Thenable that resolves to a list of command ids. |
| 71 | + */ |
| 72 | + public getCommands(filterInternal?: boolean): Thenable<string[]> { |
| 73 | + return commands.getCommands(filterInternal); |
| 74 | + } |
| 75 | +} |
0 commit comments