UNPKG

8.04 kBTypeScriptView Raw
1import { Event, Emitter, WaitUntilEvent } from './event';
2import { Disposable } from './disposable';
3import { ContributionProvider } from './contribution-provider';
4/**
5 * A command is a unique identifier of a function
6 * which can be executed by a user via a keyboard shortcut,
7 * a menu action or directly.
8 */
9export interface Command {
10 /**
11 * A unique identifier of this command.
12 */
13 id: string;
14 /**
15 * A label of this command.
16 */
17 label?: string;
18 originalLabel?: string;
19 /**
20 * An icon class of this command.
21 */
22 iconClass?: string;
23 /**
24 * A short title used for display in menus.
25 */
26 shortTitle?: string;
27 /**
28 * A category of this command.
29 */
30 category?: string;
31 originalCategory?: string;
32}
33export declare namespace Command {
34 function is(arg: unknown): arg is Command;
35 /** Utility function to easily translate commands */
36 function toLocalizedCommand(command: Command, nlsLabelKey?: string, nlsCategoryKey?: string): Command;
37 function toDefaultLocalizedCommand(command: Command): Command;
38 /** Comparator function for when sorting commands */
39 function compareCommands(a: Command, b: Command): number;
40 /**
41 * Determine if two commands are equal.
42 *
43 * @param a the first command for comparison.
44 * @param b the second command for comparison.
45 */
46 function equals(a: Command, b: Command): boolean;
47}
48/**
49 * A command handler is an implementation of a command.
50 *
51 * A command can have multiple handlers
52 * but they should be active in different contexts,
53 * otherwise first active will be executed.
54 */
55export interface CommandHandler {
56 /**
57 * Execute this handler.
58 *
59 * Don't call it directly, use `CommandService.executeCommand` instead.
60 */
61 execute(...args: any[]): any;
62 /**
63 * Test whether this handler is enabled (active).
64 */
65 isEnabled?(...args: any[]): boolean;
66 onDidChangeEnabled?: Event<void>;
67 /**
68 * Test whether menu items for this handler should be visible.
69 */
70 isVisible?(...args: any[]): boolean;
71 /**
72 * Test whether menu items for this handler should be toggled.
73 */
74 isToggled?(...args: any[]): boolean;
75}
76export declare const CommandContribution: unique symbol;
77/**
78 * The command contribution should be implemented to register custom commands and handler.
79 */
80export interface CommandContribution {
81 /**
82 * Register commands and handlers.
83 */
84 registerCommands(commands: CommandRegistry): void;
85}
86export interface CommandEvent {
87 commandId: string;
88 args: any[];
89}
90export interface WillExecuteCommandEvent extends WaitUntilEvent, CommandEvent {
91}
92export declare const commandServicePath = "/services/commands";
93export declare const CommandService: unique symbol;
94/**
95 * The command service should be used to execute commands.
96 */
97export interface CommandService {
98 /**
99 * Execute the active handler for the given command and arguments.
100 *
101 * Reject if a command cannot be executed.
102 */
103 executeCommand<T>(command: string, ...args: any[]): Promise<T | undefined>;
104 /**
105 * An event is emitted when a command is about to be executed.
106 *
107 * It can be used to install or activate a command handler.
108 */
109 readonly onWillExecuteCommand: Event<WillExecuteCommandEvent>;
110 /**
111 * An event is emitted when a command was executed.
112 */
113 readonly onDidExecuteCommand: Event<CommandEvent>;
114}
115/**
116 * The command registry manages commands and handlers.
117 */
118export declare class CommandRegistry implements CommandService {
119 protected readonly contributionProvider: ContributionProvider<CommandContribution>;
120 protected readonly _commands: {
121 [id: string]: Command;
122 };
123 protected readonly _handlers: {
124 [id: string]: CommandHandler[];
125 };
126 protected readonly toUnregisterCommands: Map<string, Disposable>;
127 protected _recent: string[];
128 protected readonly onWillExecuteCommandEmitter: Emitter<WillExecuteCommandEvent>;
129 readonly onWillExecuteCommand: Event<WillExecuteCommandEvent>;
130 protected readonly onDidExecuteCommandEmitter: Emitter<CommandEvent>;
131 readonly onDidExecuteCommand: Event<CommandEvent>;
132 protected readonly onCommandsChangedEmitter: Emitter<void>;
133 readonly onCommandsChanged: Event<void>;
134 constructor(contributionProvider: ContributionProvider<CommandContribution>);
135 onStart(): void;
136 getAllCommands(): IterableIterator<Readonly<Command & {
137 handlers: CommandHandler[];
138 }>>;
139 /**
140 * Register the given command and handler if present.
141 *
142 * Throw if a command is already registered for the given command identifier.
143 */
144 registerCommand(command: Command, handler?: CommandHandler): Disposable;
145 protected doRegisterCommand(command: Command): Disposable;
146 /**
147 * Unregister command from the registry
148 *
149 * @param command
150 */
151 unregisterCommand(command: Command): void;
152 /**
153 * Unregister command from the registry
154 *
155 * @param id
156 */
157 unregisterCommand(id: string): void;
158 /**
159 * Register the given handler for the given command identifier.
160 *
161 * If there is already a handler for the given command
162 * then the given handler is registered as more specific, and
163 * has higher priority during enablement, visibility and toggle state evaluations.
164 */
165 registerHandler(commandId: string, handler: CommandHandler): Disposable;
166 protected fireDidChange: () => Promise<void>;
167 protected doFireDidChange(): void;
168 /**
169 * Test whether there is an active handler for the given command.
170 */
171 isEnabled(command: string, ...args: any[]): boolean;
172 /**
173 * Test whether there is a visible handler for the given command.
174 */
175 isVisible(command: string, ...args: any[]): boolean;
176 /**
177 * Test whether there is a toggled handler for the given command.
178 */
179 isToggled(command: string, ...args: any[]): boolean;
180 /**
181 * Execute the active handler for the given command and arguments.
182 *
183 * Reject if a command cannot be executed.
184 */
185 executeCommand<T>(commandId: string, ...args: any[]): Promise<T | undefined>;
186 protected fireWillExecuteCommand(commandId: string, args?: any[]): Promise<void>;
187 /**
188 * Get a visible handler for the given command or `undefined`.
189 */
190 getVisibleHandler(commandId: string, ...args: any[]): CommandHandler | undefined;
191 /**
192 * Get an active handler for the given command or `undefined`.
193 */
194 getActiveHandler(commandId: string, ...args: any[]): CommandHandler | undefined;
195 /**
196 * Get a toggled handler for the given command or `undefined`.
197 */
198 getToggledHandler(commandId: string, ...args: any[]): CommandHandler | undefined;
199 /**
200 * Returns with all handlers for the given command. If the command does not have any handlers,
201 * or the command is not registered, returns an empty array.
202 */
203 getAllHandlers(commandId: string): CommandHandler[];
204 /**
205 * Get all registered commands.
206 */
207 get commands(): Command[];
208 /**
209 * Get a command for the given command identifier.
210 */
211 getCommand(id: string): Command | undefined;
212 /**
213 * Get all registered commands identifiers.
214 */
215 get commandIds(): string[];
216 /**
217 * Get the list of recently used commands.
218 */
219 get recent(): Command[];
220 /**
221 * Set the list of recently used commands.
222 * @param commands the list of recently used commands.
223 */
224 set recent(commands: Command[]);
225 /**
226 * Adds a command to recently used list.
227 * Prioritizes commands that were recently executed to be most recent.
228 *
229 * @param recent a recent command, or array of recent commands.
230 */
231 addRecentCommand(recent: Command | Command[]): void;
232 /**
233 * Clear the list of recently used commands.
234 */
235 clearCommandHistory(): void;
236}
237//# sourceMappingURL=command.d.ts.map
\No newline at end of file