Skip to content

Commit 4f3faa0

Browse files
JonasHelmingvince-fugnitto
authored andcommitted
Added documentation for key binding, contributions and registry
fixes #8636 Contributed on behalf of STMicroelectronics Signed-off-by: Jonas Helming <[email protected]>
1 parent ecfe920 commit 4f3faa0

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

packages/core/src/browser/keybinding.ts

+38-12
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface ScopedKeybinding extends common.Keybinding {
6060

6161
export const KeybindingContribution = Symbol('KeybindingContribution');
6262
/**
63-
* Representation of a keybinding contribution.
63+
* Allows extensions to contribute {@link common.Keybinding}s
6464
*/
6565
export interface KeybindingContribution {
6666
/**
@@ -168,36 +168,36 @@ export class KeybindingRegistry {
168168
*
169169
* Keybindings registered later have higher priority during evaluation.
170170
*
171-
* @param binding
171+
* @param binding the keybinding to be registered
172172
*/
173173
registerKeybinding(binding: common.Keybinding): Disposable {
174174
return this.doRegisterKeybinding(binding);
175175
}
176176

177177
/**
178-
* Register default keybindings to the registry
178+
* Register multiple default keybindings to the registry
179179
*
180-
* @param bindings
180+
* @param bindings An array of keybinding to be registered
181181
*/
182182
registerKeybindings(...bindings: common.Keybinding[]): Disposable {
183183
return this.doRegisterKeybindings(bindings, KeybindingScope.DEFAULT);
184184
}
185185

186186
/**
187-
* Unregister keybindings from the registry using the key of the given keybinding
187+
* Unregister all keybindings from the registry that are bound to the key of the given keybinding
188188
*
189-
* @param binding a Keybinding specifying the key to be unregistered
189+
* @param binding a keybinding specifying the key to be unregistered
190190
*/
191191
unregisterKeybinding(binding: common.Keybinding): void;
192192
/**
193-
* Unregister keybindings with the given key from the registry
193+
* Unregister all keybindings with the given key from the registry
194194
*
195195
* @param key a key to be unregistered
196196
*/
197197
unregisterKeybinding(key: string): void;
198198
/**
199199
* Unregister all existing keybindings for the given command
200-
* @param command the command to unregister keybindings for
200+
* @param command the command to unregister all keybindings for
201201
*/
202202
unregisterKeybinding(command: Command): void;
203203

@@ -266,6 +266,12 @@ export class KeybindingRegistry {
266266
}
267267
}
268268

269+
/**
270+
* Checks whether a colliding {@link common.Keybinding} exists in a specific scope.
271+
* @param binding the keybinding to check
272+
* @param scope the keybinding scope to check
273+
* @returns true if there is a colliding keybinding
274+
*/
269275
containsKeybindingInScope(binding: common.Keybinding, scope = KeybindingScope.USER): boolean {
270276
const bindingKeySequence = this.resolveKeybinding(binding);
271277
const collisions = this.getKeySequenceCollisions(this.keymaps[scope], bindingKeySequence)
@@ -283,22 +289,31 @@ export class KeybindingRegistry {
283289
}
284290

285291
/**
286-
* Return a user visible representation of a keybinding.
292+
* Get a user visible representation of a {@link common.Keybinding}.
293+
* @returns an array of strings representing all elements of the {@link KeySequence} defined by the {@link common.Keybinding}
294+
* @param keybinding the keybinding
295+
* @param separator the separator to be used to stringify {@link KeyCode}s that are part of the {@link KeySequence}
287296
*/
288297
acceleratorFor(keybinding: common.Keybinding, separator: string = ' '): string[] {
289298
const bindingKeySequence = this.resolveKeybinding(keybinding);
290299
return this.acceleratorForSequence(bindingKeySequence, separator);
291300
}
292301

293302
/**
294-
* Return a user visible representation of a key sequence.
303+
* Get a user visible representation of a {@link KeySequence}.
304+
* @returns an array of strings representing all elements of the {@link KeySequence}
305+
* @param keySequence the keysequence
306+
* @param separator the separator to be used to stringify {@link KeyCode}s that are part of the {@link KeySequence}
295307
*/
296308
acceleratorForSequence(keySequence: KeySequence, separator: string = ' '): string[] {
297309
return keySequence.map(keyCode => this.acceleratorForKeyCode(keyCode, separator));
298310
}
299311

300312
/**
301-
* Return a user visible representation of a key code (a key with modifiers).
313+
* Get a user visible representation of a key code (a key with modifiers).
314+
* @returns a string representing the {@link KeyCode}
315+
* @param keyCode the keycode
316+
* @param separator the separator used to separate keys (key and modifiers) in the returning string
302317
*/
303318
acceleratorForKeyCode(keyCode: KeyCode, separator: string = ' '): string {
304319
const keyCodeResult = [];
@@ -383,9 +398,10 @@ export class KeybindingRegistry {
383398
}
384399

385400
/**
386-
* Get the keybindings associated to commandId.
401+
* Get all keybindings associated to a commandId.
387402
*
388403
* @param commandId The ID of the command for which we are looking for keybindings.
404+
* @returns an array of {@link ScopedKeybinding}
389405
*/
390406
getKeybindingsForCommand(commandId: string): ScopedKeybinding[] {
391407
const result: ScopedKeybinding[] = [];
@@ -577,6 +593,11 @@ export class KeybindingRegistry {
577593
return commandId === KeybindingRegistry.PASSTHROUGH_PSEUDO_COMMAND;
578594
}
579595

596+
/**
597+
* Sets a new keymap replacing all existing {@link common.Keybinding}s in the given scope.
598+
* @param scope the keybinding scope
599+
* @param bindings an array containing the new {@link common.Keybinding}s
600+
*/
580601
setKeymap(scope: KeybindingScope, bindings: common.Keybinding[]): void {
581602
this.resetKeybindingsForScope(scope);
582603
this.toResetKeymap.set(scope, this.doRegisterKeybindings(bindings, scope));
@@ -605,6 +626,11 @@ export class KeybindingRegistry {
605626
}
606627
}
607628

629+
/**
630+
* Get all {@link common.Keybinding}s for a {@link KeybindingScope}.
631+
* @returns an array of {@link common.ScopedKeybinding}
632+
* @param scope the keybinding scope to retrieve the {@link common.Keybinding}s for.
633+
*/
608634
getKeybindingsByScope(scope: KeybindingScope): ScopedKeybinding[] {
609635
return this.keymaps[scope];
610636
}

packages/core/src/common/keybinding.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@
1313
*
1414
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1515
********************************************************************************/
16-
16+
/**
17+
* A Keybinding binds a specific key sequence ({@link Keybinding#keybinding}) to trigger a command ({@link Keybinding#command}). A Keybinding optionally may
18+
* define a "when clause" ({@link Keybinding#when}) to specify in which context it becomes active.
19+
* @see KeyBindingRegistry
20+
*/
1721
export interface Keybinding {
18-
/** Command identifier, this needs to be a unique string. */
22+
/**
23+
* Unique command identifier of the command to be triggered by this keybinding.
24+
*/
1925
command: string;
20-
/** Keybinding string as defined in packages/keymaps/README.md. */
26+
/**
27+
* The key sequence for the keybinding as defined in packages/keymaps/README.md.
28+
*/
2129
keybinding: string;
2230
/**
2331
* The optional keybinding context where this binding belongs to.
@@ -28,12 +36,14 @@ export interface Keybinding {
2836
*/
2937
context?: string;
3038
/**
31-
* https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts
39+
* An optional clause defining the condition when the keybinding is active, e.g. based on the current focus.
40+
* See {@link https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts} for more details.
3241
*/
3342
when?: string;
3443

3544
/**
36-
* Specified when the command has arguments that are passed to the command handler.
45+
* Optional arguments that will be passed to the command when it gets triggered via this keybinding.
46+
* Needs to be specified when the triggered command expects arguments to be passed to the command handler.
3747
*/
3848
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3949
args?: any;

0 commit comments

Comments
 (0)