Add command manager to app
This commit is contained in:
@@ -3,6 +3,7 @@ import { Disposable } from "../pure/disposable-object";
|
||||
import { AppEventEmitter } from "./events";
|
||||
import { Logger } from "./logging";
|
||||
import { Memento } from "./memento";
|
||||
import { ExtensionCommandManager } from "./commands";
|
||||
|
||||
export interface App {
|
||||
createEventEmitter<T>(): AppEventEmitter<T>;
|
||||
@@ -15,6 +16,7 @@ export interface App {
|
||||
readonly workspaceStoragePath?: string;
|
||||
readonly workspaceState: Memento;
|
||||
readonly credentials: Credentials;
|
||||
readonly commandManager: ExtensionCommandManager;
|
||||
}
|
||||
|
||||
export enum AppMode {
|
||||
|
||||
11
extensions/ql-vscode/src/common/commands.ts
Normal file
11
extensions/ql-vscode/src/common/commands.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { CommandManager } from "../packages/commands";
|
||||
|
||||
export type ExtensionCommands = {
|
||||
"codeQL.openVariantAnalysisLogs": (
|
||||
variantAnalysisId: number,
|
||||
) => Promise<void>;
|
||||
};
|
||||
|
||||
export type AllCommands = ExtensionCommands;
|
||||
|
||||
export type ExtensionCommandManager = CommandManager<AllCommands>;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { commands } from "vscode";
|
||||
import { commandRunner } from "./commandRunner";
|
||||
import { CommandFunction, CommandManager } from "./packages/commands";
|
||||
import { commandRunner } from "../../commandRunner";
|
||||
import { CommandFunction, CommandManager } from "../../packages/commands";
|
||||
|
||||
export function initializeVSCodeCommandManager<
|
||||
Commands extends Record<string, CommandFunction>,
|
||||
@@ -19,10 +19,3 @@ async function wrappedExecuteCommand<
|
||||
Awaited<ReturnType<Commands[CommandName]>>
|
||||
>(commandName, ...args);
|
||||
}
|
||||
|
||||
export type ExtensionCommands = {
|
||||
"codeQL.openVariantAnalysisLogs": (
|
||||
variantAnalysisId: number,
|
||||
) => Promise<void>;
|
||||
};
|
||||
export type AllCommands = ExtensionCommands;
|
||||
@@ -6,14 +6,19 @@ import { AppEventEmitter } from "../events";
|
||||
import { extLogger, Logger } from "../logging";
|
||||
import { Memento } from "../memento";
|
||||
import { VSCodeAppEventEmitter } from "./events";
|
||||
import { ExtensionCommandManager } from "../commands";
|
||||
import { initializeVSCodeCommandManager } from "./commands";
|
||||
|
||||
export class ExtensionApp implements App {
|
||||
public readonly credentials: VSCodeCredentials;
|
||||
public readonly commandManager: ExtensionCommandManager;
|
||||
|
||||
public constructor(
|
||||
public readonly extensionContext: vscode.ExtensionContext,
|
||||
) {
|
||||
this.credentials = new VSCodeCredentials();
|
||||
this.commandManager = initializeVSCodeCommandManager();
|
||||
extensionContext.subscriptions.push(this.commandManager);
|
||||
}
|
||||
|
||||
public get extensionPath(): string {
|
||||
|
||||
@@ -137,11 +137,7 @@ 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";
|
||||
import { AllCommands, ExtensionCommands } from "./common/commands";
|
||||
|
||||
/**
|
||||
* extension.ts
|
||||
@@ -1179,14 +1175,15 @@ 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);
|
||||
app.commandManager.registerCommand(
|
||||
commandName as keyof AllCommands,
|
||||
command,
|
||||
);
|
||||
}
|
||||
|
||||
ctx.subscriptions.push(
|
||||
|
||||
@@ -6,6 +6,8 @@ import { createMockLogger } from "./loggerMock";
|
||||
import { createMockMemento } from "../mock-memento";
|
||||
import { testCredentialsWithStub } from "../factories/authentication";
|
||||
import { Credentials } from "../../src/common/authentication";
|
||||
import { ExtensionCommandManager } from "../../src/common/commands";
|
||||
import { createMockCommandManager } from "./commandsMock";
|
||||
|
||||
export function createMockApp({
|
||||
extensionPath = "/mock/extension/path",
|
||||
@@ -15,6 +17,7 @@ export function createMockApp({
|
||||
executeCommand = jest.fn(() => Promise.resolve()),
|
||||
workspaceState = createMockMemento(),
|
||||
credentials = testCredentialsWithStub(),
|
||||
commandManager = createMockCommandManager(),
|
||||
}: {
|
||||
extensionPath?: string;
|
||||
workspaceStoragePath?: string;
|
||||
@@ -23,6 +26,7 @@ export function createMockApp({
|
||||
executeCommand?: () => Promise<void>;
|
||||
workspaceState?: Memento;
|
||||
credentials?: Credentials;
|
||||
commandManager?: ExtensionCommandManager;
|
||||
}): App {
|
||||
return {
|
||||
mode: AppMode.Test,
|
||||
@@ -35,6 +39,7 @@ export function createMockApp({
|
||||
createEventEmitter,
|
||||
executeCommand,
|
||||
credentials,
|
||||
commandManager,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
16
extensions/ql-vscode/test/__mocks__/commandsMock.ts
Normal file
16
extensions/ql-vscode/test/__mocks__/commandsMock.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { ExtensionCommandManager } from "../../src/common/commands";
|
||||
import {
|
||||
CommandFunction,
|
||||
CommandManager,
|
||||
Disposable,
|
||||
} from "../../src/packages/commands";
|
||||
|
||||
export function createMockCommandManager({
|
||||
registerCommand = jest.fn(),
|
||||
executeCommand = jest.fn(),
|
||||
}: {
|
||||
registerCommand?: (commandName: string, fn: CommandFunction) => Disposable;
|
||||
executeCommand?: (commandName: string, ...args: any[]) => Promise<any>;
|
||||
} = {}): ExtensionCommandManager {
|
||||
return new CommandManager(registerCommand, executeCommand);
|
||||
}
|
||||
Reference in New Issue
Block a user