UNPKG

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