Register concrete command from extension.
This commit is contained in:
28
extensions/ql-vscode/src/commands.ts
Normal file
28
extensions/ql-vscode/src/commands.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { commands } from "vscode";
|
||||
import { commandRunner } from "./commandRunner";
|
||||
import { CommandFunction, CommandManager } from "./packages/commands";
|
||||
|
||||
export function initializeVSCodeCommandManager<
|
||||
Commands extends Record<string, CommandFunction>,
|
||||
>(): CommandManager<Commands> {
|
||||
return new CommandManager(commandRunner, wrappedExecuteCommand);
|
||||
}
|
||||
|
||||
async function wrappedExecuteCommand<
|
||||
Commands extends Record<string, CommandFunction>,
|
||||
CommandName extends keyof Commands & string = keyof Commands & string,
|
||||
>(
|
||||
commandName: CommandName,
|
||||
...args: Parameters<Commands[CommandName]>
|
||||
): Promise<Awaited<ReturnType<Commands[CommandName]>>> {
|
||||
return await commands.executeCommand<
|
||||
Awaited<ReturnType<Commands[CommandName]>>
|
||||
>(commandName, ...args);
|
||||
}
|
||||
|
||||
export type ExtensionCommands = {
|
||||
"codeQL.openVariantAnalysisLogs": (
|
||||
variantAnalysisId: number,
|
||||
) => Promise<void>;
|
||||
};
|
||||
export type AllCommands = ExtensionCommands;
|
||||
@@ -137,6 +137,11 @@ import { RepositoriesFilterSortStateWithIds } from "./pure/variant-analysis-filt
|
||||
import { DbModule } from "./databases/db-module";
|
||||
import { redactableError } from "./pure/errors";
|
||||
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
|
||||
import {
|
||||
AllCommands,
|
||||
ExtensionCommands,
|
||||
initializeVSCodeCommandManager,
|
||||
} from "./commands";
|
||||
|
||||
/**
|
||||
* extension.ts
|
||||
@@ -168,6 +173,16 @@ let isInstallingOrUpdatingDistribution = false;
|
||||
const extensionId = "GitHub.vscode-codeql";
|
||||
const extension = extensions.getExtension(extensionId);
|
||||
|
||||
function getCommands(
|
||||
variantAnalysisManager: VariantAnalysisManager,
|
||||
): ExtensionCommands {
|
||||
return {
|
||||
"codeQL.openVariantAnalysisLogs": async (variantAnalysisId: number) => {
|
||||
await variantAnalysisManager.openVariantAnalysisLogs(variantAnalysisId);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* If the user tries to execute vscode commands after extension activation is failed, give
|
||||
* a sensible error message.
|
||||
@@ -1153,6 +1168,7 @@ async function activateWithInstalledDistribution(
|
||||
),
|
||||
);
|
||||
|
||||
/*
|
||||
ctx.subscriptions.push(
|
||||
commandRunner(
|
||||
"codeQL.openVariantAnalysisLogs",
|
||||
@@ -1161,6 +1177,17 @@ async function activateWithInstalledDistribution(
|
||||
},
|
||||
),
|
||||
);
|
||||
*/
|
||||
|
||||
const vsCommandRunner = initializeVSCodeCommandManager<AllCommands>();
|
||||
ctx.subscriptions.push(vsCommandRunner);
|
||||
const allCommands: Partial<AllCommands> = {
|
||||
...getCommands(variantAnalysisManager),
|
||||
};
|
||||
|
||||
for (const [commandName, command] of Object.entries(allCommands)) {
|
||||
vsCommandRunner.registerCommand(commandName as keyof AllCommands, command);
|
||||
}
|
||||
|
||||
ctx.subscriptions.push(
|
||||
commandRunner(
|
||||
|
||||
@@ -21,7 +21,7 @@ export class CommandManager<
|
||||
private readonly commandExecute: <T extends CommandName>(
|
||||
commandName: T,
|
||||
...args: Parameters<Commands[T]>
|
||||
) => Promise<ReturnType<Commands[T]>>,
|
||||
) => Promise<Awaited<ReturnType<Commands[T]>>>,
|
||||
) {}
|
||||
|
||||
registerCommand<T extends CommandName>(
|
||||
@@ -34,7 +34,7 @@ export class CommandManager<
|
||||
executeCommand<T extends CommandName>(
|
||||
commandName: T,
|
||||
...args: Parameters<Commands[T]>
|
||||
): Promise<ReturnType<Commands[T]>> {
|
||||
): Promise<Awaited<ReturnType<Commands[T]>>> {
|
||||
return this.commandExecute(commandName, ...args);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user