Skip to content

Commit 79d15cc

Browse files
committed
Add comments.
1 parent d990f31 commit 79d15cc

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

extensions/ql-vscode/src/common/commands.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import { CommandManager } from "../packages/commands";
22

3+
/**
4+
* Contains type definitions for all commands used by the extension.
5+
*
6+
* To add a new command first define its type here, then provide
7+
* the implementation in the corresponding `getCommands` function.
8+
*/
9+
10+
// Commands directly in the extension
311
export type ExtensionCommands = {
412
"codeQL.openDocumentation": () => Promise<void>;
513
};
614

15+
// Commands tied to variant analysis
716
export type VariantAnalysisCommands = {
817
"codeQL.openVariantAnalysisLogs": (
918
variantAnalysisId: number,

extensions/ql-vscode/src/common/vscode/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import { commands } from "vscode";
22
import { commandRunner } from "../../commandRunner";
33
import { CommandFunction, CommandManager } from "../../packages/commands";
44

5+
/**
6+
* Intializes a command manager for VSCode, wrapping the commandRunner
7+
* and vscode.executeCommand.
8+
*/
59
export function initializeVSCodeCommandManager<
610
Commands extends Record<string, CommandFunction>,
711
>(): CommandManager<Commands> {

extensions/ql-vscode/src/packages/commands/CommandManager.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
/**
2+
* Contains a generic implementation of typed commands.
3+
*
4+
* This allows different parts of the extension to register commands with a certain type,
5+
* and then allow other parts to call those commands in a well-typed manner.
6+
*/
7+
18
export interface Disposable {
29
dispose(): void;
310
}
411

12+
/**
13+
* A command function is a completely untyped command.
14+
*/
515
export type CommandFunction = (...args: any[]) => Promise<unknown>;
616

17+
/**
18+
* The command manager basically takes a single input, the type
19+
* of all the known commands. The second parameter is provided by
20+
* default (and should not be needed by the caller) it is a
21+
* technicality to allow the type system to look up commands.
22+
*/
723
export class CommandManager<
824
Commands extends Record<string, CommandFunction>,
925
CommandName extends keyof Commands & string = keyof Commands & string,
@@ -24,20 +40,29 @@ export class CommandManager<
2440
) => Promise<Awaited<ReturnType<Commands[T]>>>,
2541
) {}
2642

43+
/**
44+
* Register a command with the specified name and implementation.
45+
*/
2746
registerCommand<T extends CommandName>(
2847
commandName: T,
2948
definition: Commands[T],
3049
): void {
3150
this.commands.push(this.commandRegister(commandName, definition));
3251
}
3352

53+
/**
54+
* Execute a command with the specified name and the provided arguments.
55+
*/
3456
executeCommand<T extends CommandName>(
3557
commandName: T,
3658
...args: Parameters<Commands[T]>
3759
): Promise<Awaited<ReturnType<Commands[T]>>> {
3860
return this.commandExecute(commandName, ...args);
3961
}
4062

63+
/**
64+
* Dispose the manager, disposing all the registered commands.
65+
*/
4166
dispose(): void {
4267
this.commands.forEach((cmd) => {
4368
cmd.dispose();

0 commit comments

Comments
 (0)