Move query editing commands to separate file

This commit is contained in:
Koen Vlaswinkel
2023-03-22 16:50:44 +01:00
parent 125af1139b
commit 5ac5de8a5b
2 changed files with 104 additions and 79 deletions

View File

@@ -11,13 +11,12 @@ import {
Uri,
version as vscodeVersion,
window as Window,
window,
workspace,
} from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { arch, platform } from "os";
import { ensureDir } from "fs-extra";
import { basename, join } from "path";
import { join } from "path";
import { dirSync } from "tmp-promise";
import { testExplorerExtensionId, TestHub } from "vscode-test-adapter-api";
import { lt, parse } from "semver";
@@ -111,7 +110,6 @@ import { ExtensionApp } from "./common/vscode/vscode-app";
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,
@@ -122,6 +120,7 @@ import {
showResultsForCompletedQuery,
} from "./local-queries";
import { getAstCfgCommands } from "./ast-cfg-commands";
import { registerQueryEditorCommands } from "./query-editor";
/**
* extension.ts
@@ -878,37 +877,11 @@ async function activateWithInstalledDistribution(
);
}
ctx.subscriptions.push(
commandRunner("codeQL.openReferencedFile", async (selectedQuery: Uri) => {
await openReferencedFile(qs, cliServer, selectedQuery);
}),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
ctx.subscriptions.push(
commandRunner(
"codeQL.openReferencedFileContextEditor",
async (selectedQuery: Uri) => {
await openReferencedFile(qs, cliServer, selectedQuery);
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
ctx.subscriptions.push(
commandRunner(
"codeQL.openReferencedFileContextExplorer",
async (selectedQuery: Uri) => {
await openReferencedFile(qs, cliServer, selectedQuery);
},
),
);
ctx.subscriptions.push(
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
}),
);
registerQueryEditorCommands(ctx, {
queryRunner: qs,
cliServer,
qhelpTmpDir: qhelpTmpDir.name,
});
ctx.subscriptions.push(
commandRunner("codeQL.copyVersion", async () => {
@@ -1015,51 +988,6 @@ async function showResultsForComparison(
}
}
async function previewQueryHelp(
cliServer: CodeQLCliServer,
qhelpTmpDir: DirResult,
selectedQuery: Uri,
): Promise<void> {
// selectedQuery is unpopulated when executing through the command palette
const pathToQhelp = selectedQuery
? selectedQuery.fsPath
: window.activeTextEditor?.document.uri.fsPath;
if (pathToQhelp) {
// Create temporary directory
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
const absolutePathToMd = join(qhelpTmpDir.name, relativePathToMd);
const uri = Uri.file(absolutePathToMd);
try {
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
await commands.executeCommand("markdown.showPreviewToSide", uri);
} catch (e) {
const errorMessage = getErrorMessage(e).includes(
"Generating qhelp in markdown",
)
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
void showAndLogExceptionWithTelemetry(errorMessage, {
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
});
}
}
}
async function openReferencedFile(
qs: QueryRunner,
cliServer: CodeQLCliServer,
selectedQuery: Uri,
): Promise<void> {
// If no file is selected, the path of the file in the editor is selected
const path =
selectedQuery?.fsPath || window.activeTextEditor?.document.uri.fsPath;
if (qs !== undefined && path) {
const resolved = await cliServer.resolveQlref(path);
const uri = Uri.file(resolved.resolvedPath);
await window.showTextDocument(uri, { preview: false });
}
}
function addUnhandledRejectionListener() {
const handler = (error: unknown) => {
// This listener will be triggered for errors from other extensions as

View File

@@ -0,0 +1,97 @@
import { commands, ExtensionContext, Uri, window } from "vscode";
import { CodeQLCliServer } from "./cli";
import { QueryRunner } from "./queryRunner";
import { commandRunner } from "./commandRunner";
import { basename, join } from "path";
import { getErrorMessage } from "./pure/helpers-pure";
import { redactableError } from "./pure/errors";
import { showAndLogExceptionWithTelemetry } from "./helpers";
type QueryEditorOptions = {
queryRunner: QueryRunner;
cliServer: CodeQLCliServer;
qhelpTmpDir: string;
};
export function registerQueryEditorCommands(
ctx: ExtensionContext,
{ queryRunner, cliServer, qhelpTmpDir }: QueryEditorOptions,
) {
ctx.subscriptions.push(
commandRunner("codeQL.openReferencedFile", async (selectedQuery: Uri) => {
await openReferencedFile(queryRunner, cliServer, selectedQuery);
}),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
ctx.subscriptions.push(
commandRunner(
"codeQL.openReferencedFileContextEditor",
async (selectedQuery: Uri) => {
await openReferencedFile(queryRunner, cliServer, selectedQuery);
},
),
);
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
ctx.subscriptions.push(
commandRunner(
"codeQL.openReferencedFileContextExplorer",
async (selectedQuery: Uri) => {
await openReferencedFile(queryRunner, cliServer, selectedQuery);
},
),
);
ctx.subscriptions.push(
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
}),
);
}
async function previewQueryHelp(
cliServer: CodeQLCliServer,
qhelpTmpDir: string,
selectedQuery: Uri,
): Promise<void> {
// selectedQuery is unpopulated when executing through the command palette
const pathToQhelp = selectedQuery
? selectedQuery.fsPath
: window.activeTextEditor?.document.uri.fsPath;
if (pathToQhelp) {
// Create temporary directory
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
const absolutePathToMd = join(qhelpTmpDir, relativePathToMd);
const uri = Uri.file(absolutePathToMd);
try {
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
await commands.executeCommand("markdown.showPreviewToSide", uri);
} catch (e) {
const errorMessage = getErrorMessage(e).includes(
"Generating qhelp in markdown",
)
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
void showAndLogExceptionWithTelemetry(errorMessage, {
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
});
}
}
}
async function openReferencedFile(
qs: QueryRunner,
cliServer: CodeQLCliServer,
selectedQuery: Uri,
): Promise<void> {
// If no file is selected, the path of the file in the editor is selected
const path =
selectedQuery?.fsPath || window.activeTextEditor?.document.uri.fsPath;
if (qs !== undefined && path) {
const resolved = await cliServer.resolveQlref(path);
const uri = Uri.file(resolved.resolvedPath);
await window.showTextDocument(uri, { preview: false });
}
}