Convert local query commands to typed commands

The local query commands are using a separate logger, and this is not
supported by the command manager because it is quite specific to this
extension. Therefore, we create a separate command manager which uses
a different logger to separate the commands.
This commit is contained in:
Koen Vlaswinkel
2023-03-21 10:25:34 +01:00
parent f37a6c5e9e
commit 7950c1c982
7 changed files with 181 additions and 210 deletions

View File

@@ -89,7 +89,7 @@ export type ProgressTaskWithArgs<R> = (
* @param args arguments passed to this task passed on from
* `commands.registerCommand`.
*/
type NoProgressTask = (...args: any[]) => Promise<any>;
export type NoProgressTask = (...args: any[]) => Promise<any>;
/**
* This mediates between the kind of progress callbacks we want to

View File

@@ -1,5 +1,5 @@
import type { CommandManager } from "../packages/commands";
import type { Uri } from "vscode";
import type { Uri, Range } from "vscode";
import type { DbTreeViewItem } from "../databases/ui/db-tree-view-item";
import type { DatabaseItem } from "../local-databases";
import type { QueryHistoryInfo } from "../query-history/query-history-info";
@@ -29,6 +29,21 @@ export type BaseCommands = {
"codeQL.openDocumentation": () => Promise<void>;
};
// Commands used for running local queries
export type LocalQueryCommands = {
"codeQL.runQuery": (uri?: Uri) => Promise<void>;
"codeQL.runQueryContextEditor": (uri?: Uri) => Promise<void>;
"codeQL.runQueryOnMultipleDatabases": (uri?: Uri) => Promise<void>;
"codeQL.runQueryOnMultipleDatabasesContextEditor": (
uri?: Uri,
) => Promise<void>;
"codeQL.runQueries": SelectionCommandFunction<Uri>;
"codeQL.quickEval": (uri: Uri) => Promise<void>;
"codeQL.quickEvalContextEditor": (uri: Uri) => Promise<void>;
"codeQL.codeLensQuickEval": (uri: Uri, range: Range) => Promise<void>;
"codeQL.quickQuery": () => Promise<void>;
};
// Commands used for the query history panel
export type QueryHistoryCommands = {
// Commands in the "navigation" group
@@ -115,3 +130,7 @@ export type AllCommands = BaseCommands &
DatabasePanelCommands;
export type AppCommandManager = CommandManager<AllCommands>;
// Separate command manager because it uses a different logger
export type QueryServerCommands = LocalQueryCommands;
export type QueryServerCommandManager = CommandManager<QueryServerCommands>;

View File

@@ -1,6 +1,7 @@
import { commands } from "vscode";
import { commandRunner } from "../../commandRunner";
import { commandRunner, NoProgressTask } from "../../commandRunner";
import { CommandFunction, CommandManager } from "../../packages/commands";
import { OutputChannelLogger } from "../logging";
/**
* Create a command manager for VSCode, wrapping the commandRunner
@@ -8,8 +9,10 @@ import { CommandFunction, CommandManager } from "../../packages/commands";
*/
export function createVSCodeCommandManager<
Commands extends Record<string, CommandFunction>,
>(): CommandManager<Commands> {
return new CommandManager(commandRunner, wrapExecuteCommand);
>(outputLogger?: OutputChannelLogger): CommandManager<Commands> {
return new CommandManager((commandId, task: NoProgressTask) => {
return commandRunner(commandId, task, outputLogger);
}, wrapExecuteCommand);
}
/**

View File

@@ -3,21 +3,23 @@ import { VSCodeCredentials } from "../../authentication";
import { Disposable } from "../../pure/disposable-object";
import { App, AppMode } from "../app";
import { AppEventEmitter } from "../events";
import { extLogger, Logger } from "../logging";
import { extLogger, Logger, queryServerLogger } from "../logging";
import { Memento } from "../memento";
import { VSCodeAppEventEmitter } from "./events";
import { AppCommandManager } from "../commands";
import { AppCommandManager, QueryServerCommandManager } from "../commands";
import { createVSCodeCommandManager } from "./commands";
export class ExtensionApp implements App {
public readonly credentials: VSCodeCredentials;
public readonly commands: AppCommandManager;
public readonly queryServerCommands: QueryServerCommandManager;
public constructor(
public readonly extensionContext: vscode.ExtensionContext,
) {
this.credentials = new VSCodeCredentials();
this.commands = createVSCodeCommandManager();
this.queryServerCommands = createVSCodeCommandManager(queryServerLogger);
extensionContext.subscriptions.push(this.commands);
}

View File

@@ -124,10 +124,14 @@ import { DbModule } from "./databases/db-module";
import { redactableError } from "./pure/errors";
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
import { DirResult } from "tmp";
import { AllCommands, BaseCommands } from "./common/commands";
import {
AllCommands,
BaseCommands,
QueryServerCommands,
} from "./common/commands";
import {
compileAndRunQuery,
registerLocalQueryCommands,
getLocalQueryCommands,
showResultsForCompletedQuery,
} from "./local-queries";
@@ -785,16 +789,6 @@ async function activateWithInstalledDistribution(
void extLogger.log("Registering top-level command palette commands.");
registerLocalQueryCommands(ctx, {
queryRunner: qs,
queryHistoryManager: qhm,
databaseManager: dbm,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
});
const allCommands: AllCommands = {
...getCommands(),
...qhm.getCommands(),
@@ -807,6 +801,26 @@ async function activateWithInstalledDistribution(
app.commands.register(commandName as keyof AllCommands, command);
}
const queryServerCommands: QueryServerCommands = {
...getLocalQueryCommands({
app,
queryRunner: qs,
queryHistoryManager: qhm,
databaseManager: dbm,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
}),
};
for (const [commandName, command] of Object.entries(queryServerCommands)) {
app.queryServerCommands.register(
commandName as keyof QueryServerCommands,
command,
);
}
ctx.subscriptions.push(
commandRunner(
"codeQL.copyVariantAnalysisRepoList",

View File

@@ -1,5 +1,4 @@
import {
commandRunner,
ProgressCallback,
ProgressUpdate,
withProgress,
@@ -7,13 +6,12 @@ import {
import {
CancellationToken,
CancellationTokenSource,
ExtensionContext,
QuickPickItem,
Range,
Uri,
window,
} from "vscode";
import { extLogger, queryServerLogger } from "./common";
import { extLogger } from "./common";
import { MAX_QUERIES } from "./config";
import { gatherQlFiles } from "./pure/files";
import { basename } from "path";
@@ -34,8 +32,11 @@ import { CompletedLocalQueryInfo, LocalQueryInfo } from "./query-results";
import { WebviewReveal } from "./interface-utils";
import { asError, getErrorMessage } from "./pure/helpers-pure";
import { CodeQLCliServer } from "./cli";
import { LocalQueryCommands } from "./common/commands";
import { App } from "./common/app";
type LocalQueryOptions = {
app: App;
queryRunner: QueryRunner;
queryHistoryManager: QueryHistoryManager;
databaseManager: DatabaseManager;
@@ -45,18 +46,16 @@ type LocalQueryOptions = {
queryStorageDir: string;
};
export function registerLocalQueryCommands(
ctx: ExtensionContext,
{
queryRunner,
queryHistoryManager,
databaseManager,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
}: LocalQueryOptions,
) {
export function getLocalQueryCommands({
app,
queryRunner,
queryHistoryManager,
databaseManager,
cliServer,
databaseUI,
localQueryResultsView,
queryStorageDir,
}: LocalQueryOptions): LocalQueryCommands {
const runQuery = async (uri: Uri | undefined) =>
withProgress(
async (progress, token) => {
@@ -79,25 +78,6 @@ export function registerLocalQueryCommands(
},
);
ctx.subscriptions.push(
commandRunner(
"codeQL.runQuery",
runQuery,
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
// Since we are tracking extension usage through commands, this command mirrors the runQuery command
ctx.subscriptions.push(
commandRunner(
"codeQL.runQueryContextEditor",
runQuery,
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
const runQueryOnMultipleDatabases = async (uri: Uri | undefined) =>
withProgress(
async (progress, token) =>
@@ -119,95 +99,73 @@ export function registerLocalQueryCommands(
},
);
ctx.subscriptions.push(
commandRunner(
"codeQL.runQueryOnMultipleDatabases",
runQueryOnMultipleDatabases,
),
);
// Since we are tracking extension usage through commands, this command mirrors the runQueryOnMultipleDatabases command
ctx.subscriptions.push(
commandRunner(
"codeQL.runQueryOnMultipleDatabasesContextEditor",
runQueryOnMultipleDatabases,
),
);
ctx.subscriptions.push(
commandRunner(
"codeQL.runQueries",
async (_: Uri | undefined, multi: Uri[]) =>
withProgress(
async (progress, token) => {
const maxQueryCount = MAX_QUERIES.getValue() as number;
const [files, dirFound] = await gatherQlFiles(
multi.map((uri) => uri.fsPath),
);
if (files.length > maxQueryCount) {
throw new Error(
`You tried to run ${files.length} queries, but the maximum is ${maxQueryCount}. Try selecting fewer queries or changing the 'codeQL.runningQueries.maxQueries' setting.`,
);
}
// warn user and display selected files when a directory is selected because some ql
// files may be hidden from the user.
if (dirFound) {
const fileString = files.map((file) => basename(file)).join(", ");
const res = await showBinaryChoiceDialog(
`You are about to run ${files.length} queries: ${fileString} Do you want to continue?`,
);
if (!res) {
return;
}
}
const queryUris = files.map((path) =>
Uri.parse(`file:${path}`, true),
);
const runQueries = async (_: Uri | undefined, multi: Uri[]) =>
withProgress(
async (progress, token) => {
const maxQueryCount = MAX_QUERIES.getValue() as number;
const [files, dirFound] = await gatherQlFiles(
multi.map((uri) => uri.fsPath),
);
if (files.length > maxQueryCount) {
throw new Error(
`You tried to run ${files.length} queries, but the maximum is ${maxQueryCount}. Try selecting fewer queries or changing the 'codeQL.runningQueries.maxQueries' setting.`,
);
}
// warn user and display selected files when a directory is selected because some ql
// files may be hidden from the user.
if (dirFound) {
const fileString = files.map((file) => basename(file)).join(", ");
const res = await showBinaryChoiceDialog(
`You are about to run ${files.length} queries: ${fileString} Do you want to continue?`,
);
if (!res) {
return;
}
}
const queryUris = files.map((path) => Uri.parse(`file:${path}`, true));
// Use a wrapped progress so that messages appear with the queries remaining in it.
let queriesRemaining = queryUris.length;
// Use a wrapped progress so that messages appear with the queries remaining in it.
let queriesRemaining = queryUris.length;
function wrappedProgress(update: ProgressUpdate) {
const message =
queriesRemaining > 1
? `${queriesRemaining} remaining. ${update.message}`
: update.message;
progress({
...update,
message,
});
}
function wrappedProgress(update: ProgressUpdate) {
const message =
queriesRemaining > 1
? `${queriesRemaining} remaining. ${update.message}`
: update.message;
progress({
...update,
message,
});
}
wrappedProgress({
maxStep: queryUris.length,
step: queryUris.length - queriesRemaining,
message: "",
});
wrappedProgress({
maxStep: queryUris.length,
step: queryUris.length - queriesRemaining,
message: "",
});
await Promise.all(
queryUris.map(async (uri) =>
compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
uri,
wrappedProgress,
token,
undefined,
).then(() => queriesRemaining--),
),
);
},
{
title: "Running queries",
cancellable: true,
},
),
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
await Promise.all(
queryUris.map(async (uri) =>
compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
false,
uri,
wrappedProgress,
token,
undefined,
).then(() => queriesRemaining--),
),
);
},
{
title: "Running queries",
cancellable: true,
},
);
const quickEval = async (uri: Uri) =>
withProgress(
@@ -231,69 +189,49 @@ export function registerLocalQueryCommands(
},
);
ctx.subscriptions.push(
commandRunner(
"codeQL.quickEval",
quickEval,
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.quickEval" command
ctx.subscriptions.push(
commandRunner(
"codeQL.quickEvalContextEditor",
quickEval,
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
ctx.subscriptions.push(
commandRunner(
"codeQL.codeLensQuickEval",
async (uri: Uri, range: Range) =>
withProgress(
async (progress, token) =>
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
true,
uri,
progress,
token,
undefined,
range,
),
{
title: "Running query",
cancellable: true,
},
const codeLensQuickEval = async (uri: Uri, range: Range) =>
withProgress(
async (progress, token) =>
await compileAndRunQuery(
queryRunner,
queryHistoryManager,
databaseUI,
localQueryResultsView,
queryStorageDir,
true,
uri,
progress,
token,
undefined,
range,
),
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
{
title: "Running query",
cancellable: true,
},
);
ctx.subscriptions.push(
commandRunner(
"codeQL.quickQuery",
async () =>
withProgress(
async (progress, token) =>
displayQuickQuery(ctx, cliServer, databaseUI, progress, token),
{
title: "Run Quick Query",
},
),
// Open the query server logger on error since that's usually where the interesting errors appear.
queryServerLogger,
),
);
const quickQuery = async () =>
withProgress(
async (progress, token) =>
displayQuickQuery(app, cliServer, databaseUI, progress, token),
{
title: "Run Quick Query",
},
);
return {
"codeQL.runQuery": runQuery,
"codeQL.runQueryContextEditor": runQuery,
"codeQL.runQueryOnMultipleDatabases": runQueryOnMultipleDatabases,
"codeQL.runQueryOnMultipleDatabasesContextEditor":
runQueryOnMultipleDatabases,
"codeQL.runQueries": runQueries,
"codeQL.quickEval": quickEval,
"codeQL.quickEvalContextEditor": quickEval,
"codeQL.codeLensQuickEval": codeLensQuickEval,
"codeQL.quickQuery": quickQuery,
};
}
export async function compileAndRunQuery(

View File

@@ -1,13 +1,7 @@
import { ensureDir, writeFile, pathExists, readFile } from "fs-extra";
import { dump, load } from "js-yaml";
import { basename, join } from "path";
import {
CancellationToken,
ExtensionContext,
window as Window,
workspace,
Uri,
} from "vscode";
import { CancellationToken, window as Window, workspace, Uri } from "vscode";
import { LSPErrorCodes, ResponseError } from "vscode-languageclient";
import { CodeQLCliServer } from "./cli";
import { DatabaseUI } from "./local-databases-ui";
@@ -20,6 +14,7 @@ import {
import { ProgressCallback, UserCancellationException } from "./commandRunner";
import { getErrorMessage } from "./pure/helpers-pure";
import { FALLBACK_QLPACK_FILENAME, getQlPackPath } from "./pure/ql";
import { App } from "./common/app";
const QUICK_QUERIES_DIR_NAME = "quick-queries";
const QUICK_QUERY_QUERY_NAME = "quick-query.ql";
@@ -30,8 +25,8 @@ export function isQuickQueryPath(queryPath: string): boolean {
return basename(queryPath) === QUICK_QUERY_QUERY_NAME;
}
async function getQuickQueriesDir(ctx: ExtensionContext): Promise<string> {
const storagePath = ctx.storagePath;
async function getQuickQueriesDir(app: App): Promise<string> {
const storagePath = app.workspaceStoragePath;
if (storagePath === undefined) {
throw new Error("Workspace storage path is undefined");
}
@@ -57,7 +52,7 @@ function findExistingQuickQueryEditor() {
* Show a buffer the user can enter a simple query into.
*/
export async function displayQuickQuery(
ctx: ExtensionContext,
app: App,
cliServer: CodeQLCliServer,
databaseUI: DatabaseUI,
progress: ProgressCallback,
@@ -73,7 +68,7 @@ export async function displayQuickQuery(
}
const workspaceFolders = workspace.workspaceFolders || [];
const queriesDir = await getQuickQueriesDir(ctx);
const queriesDir = await getQuickQueriesDir(app);
// We need to have a multi-root workspace to make quick query work
// at all. Changing the workspace from single-root to multi-root