Skip to content

Commit 7059f46

Browse files
authored
Merge pull request #2209 from github/koesie10/query-editor-typed-commands
Convert query editing commands to typed commands
2 parents 322c1a8 + be3459c commit 7059f46

File tree

3 files changed

+99
-80
lines changed

3 files changed

+99
-80
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ export type BaseCommands = {
3939
"codeQL.restartQueryServer": () => Promise<void>;
4040
};
4141

42+
// Commands used when working with queries in the editor
43+
export type QueryEditorCommands = {
44+
"codeQL.openReferencedFile": (selectedQuery: Uri) => Promise<void>;
45+
"codeQL.openReferencedFileContextEditor": (
46+
selectedQuery: Uri,
47+
) => Promise<void>;
48+
"codeQL.openReferencedFileContextExplorer": (
49+
selectedQuery: Uri,
50+
) => Promise<void>;
51+
"codeQL.previewQueryHelp": (selectedQuery: Uri) => Promise<void>;
52+
};
53+
4254
// Commands used for running local queries
4355
export type LocalQueryCommands = {
4456
"codeQL.runQuery": (uri?: Uri) => Promise<void>;
@@ -219,6 +231,7 @@ export type MockGitHubApiServerCommands = {
219231
};
220232

221233
export type AllCommands = BaseCommands &
234+
QueryEditorCommands &
222235
ResultsViewCommands &
223236
QueryHistoryCommands &
224237
LocalDatabasesCommands &

extensions/ql-vscode/src/extension.ts

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ import {
1111
Uri,
1212
version as vscodeVersion,
1313
window as Window,
14-
window,
1514
workspace,
1615
} from "vscode";
1716
import { LanguageClient } from "vscode-languageclient/node";
1817
import { arch, platform } from "os";
1918
import { ensureDir } from "fs-extra";
20-
import { basename, join } from "path";
19+
import { join } from "path";
2120
import { dirSync } from "tmp-promise";
2221
import { testExplorerExtensionId, TestHub } from "vscode-test-adapter-api";
2322
import { lt, parse } from "semver";
@@ -111,7 +110,6 @@ import { ExtensionApp } from "./common/vscode/vscode-app";
111110
import { DbModule } from "./databases/db-module";
112111
import { redactableError } from "./pure/errors";
113112
import { QueryHistoryDirs } from "./query-history/query-history-dirs";
114-
import { DirResult } from "tmp";
115113
import {
116114
AllCommands,
117115
BaseCommands,
@@ -123,6 +121,7 @@ import {
123121
showResultsForCompletedQuery,
124122
} from "./local-queries";
125123
import { getAstCfgCommands } from "./ast-cfg-commands";
124+
import { getQueryEditorCommands } from "./query-editor";
126125

127126
/**
128127
* extension.ts
@@ -834,6 +833,11 @@ async function activateWithInstalledDistribution(
834833

835834
const allCommands: AllCommands = {
836835
...getCommands(cliServer, qs),
836+
...getQueryEditorCommands({
837+
queryRunner: qs,
838+
cliServer,
839+
qhelpTmpDir: qhelpTmpDir.name,
840+
}),
837841
...localQueryResultsView.getCommands(),
838842
...qhm.getCommands(),
839843
...variantAnalysisManager.getCommands(),
@@ -883,38 +887,6 @@ async function activateWithInstalledDistribution(
883887
);
884888
}
885889

886-
ctx.subscriptions.push(
887-
commandRunner("codeQL.openReferencedFile", async (selectedQuery: Uri) => {
888-
await openReferencedFile(qs, cliServer, selectedQuery);
889-
}),
890-
);
891-
892-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
893-
ctx.subscriptions.push(
894-
commandRunner(
895-
"codeQL.openReferencedFileContextEditor",
896-
async (selectedQuery: Uri) => {
897-
await openReferencedFile(qs, cliServer, selectedQuery);
898-
},
899-
),
900-
);
901-
902-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
903-
ctx.subscriptions.push(
904-
commandRunner(
905-
"codeQL.openReferencedFileContextExplorer",
906-
async (selectedQuery: Uri) => {
907-
await openReferencedFile(qs, cliServer, selectedQuery);
908-
},
909-
),
910-
);
911-
912-
ctx.subscriptions.push(
913-
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
914-
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
915-
}),
916-
);
917-
918890
ctx.subscriptions.push(
919891
commandRunner("codeQL.copyVersion", async () => {
920892
const text = `CodeQL extension version: ${
@@ -1020,51 +992,6 @@ async function showResultsForComparison(
1020992
}
1021993
}
1022994

1023-
async function previewQueryHelp(
1024-
cliServer: CodeQLCliServer,
1025-
qhelpTmpDir: DirResult,
1026-
selectedQuery: Uri,
1027-
): Promise<void> {
1028-
// selectedQuery is unpopulated when executing through the command palette
1029-
const pathToQhelp = selectedQuery
1030-
? selectedQuery.fsPath
1031-
: window.activeTextEditor?.document.uri.fsPath;
1032-
if (pathToQhelp) {
1033-
// Create temporary directory
1034-
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
1035-
const absolutePathToMd = join(qhelpTmpDir.name, relativePathToMd);
1036-
const uri = Uri.file(absolutePathToMd);
1037-
try {
1038-
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
1039-
await commands.executeCommand("markdown.showPreviewToSide", uri);
1040-
} catch (e) {
1041-
const errorMessage = getErrorMessage(e).includes(
1042-
"Generating qhelp in markdown",
1043-
)
1044-
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
1045-
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
1046-
void showAndLogExceptionWithTelemetry(errorMessage, {
1047-
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
1048-
});
1049-
}
1050-
}
1051-
}
1052-
1053-
async function openReferencedFile(
1054-
qs: QueryRunner,
1055-
cliServer: CodeQLCliServer,
1056-
selectedQuery: Uri,
1057-
): Promise<void> {
1058-
// If no file is selected, the path of the file in the editor is selected
1059-
const path =
1060-
selectedQuery?.fsPath || window.activeTextEditor?.document.uri.fsPath;
1061-
if (qs !== undefined && path) {
1062-
const resolved = await cliServer.resolveQlref(path);
1063-
const uri = Uri.file(resolved.resolvedPath);
1064-
await window.showTextDocument(uri, { preview: false });
1065-
}
1066-
}
1067-
1068995
function addUnhandledRejectionListener() {
1069996
const handler = (error: unknown) => {
1070997
// This listener will be triggered for errors from other extensions as
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { commands, Uri, window } from "vscode";
2+
import { CodeQLCliServer } from "./cli";
3+
import { QueryRunner } from "./queryRunner";
4+
import { basename, join } from "path";
5+
import { getErrorMessage } from "./pure/helpers-pure";
6+
import { redactableError } from "./pure/errors";
7+
import { showAndLogExceptionWithTelemetry } from "./helpers";
8+
import { QueryEditorCommands } from "./common/commands";
9+
10+
type QueryEditorOptions = {
11+
queryRunner: QueryRunner;
12+
cliServer: CodeQLCliServer;
13+
14+
qhelpTmpDir: string;
15+
};
16+
17+
export function getQueryEditorCommands({
18+
queryRunner,
19+
cliServer,
20+
qhelpTmpDir,
21+
}: QueryEditorOptions): QueryEditorCommands {
22+
const openReferencedFileCommand = async (selectedQuery: Uri) =>
23+
await openReferencedFile(queryRunner, cliServer, selectedQuery);
24+
25+
return {
26+
"codeQL.openReferencedFile": openReferencedFileCommand,
27+
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
28+
"codeQL.openReferencedFileContextEditor": openReferencedFileCommand,
29+
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
30+
"codeQL.openReferencedFileContextExplorer": openReferencedFileCommand,
31+
"codeQL.previewQueryHelp": async (selectedQuery: Uri) =>
32+
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery),
33+
};
34+
}
35+
36+
async function previewQueryHelp(
37+
cliServer: CodeQLCliServer,
38+
qhelpTmpDir: string,
39+
selectedQuery: Uri,
40+
): Promise<void> {
41+
// selectedQuery is unpopulated when executing through the command palette
42+
const pathToQhelp = selectedQuery
43+
? selectedQuery.fsPath
44+
: window.activeTextEditor?.document.uri.fsPath;
45+
if (pathToQhelp) {
46+
// Create temporary directory
47+
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
48+
const absolutePathToMd = join(qhelpTmpDir, relativePathToMd);
49+
const uri = Uri.file(absolutePathToMd);
50+
try {
51+
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
52+
await commands.executeCommand("markdown.showPreviewToSide", uri);
53+
} catch (e) {
54+
const errorMessage = getErrorMessage(e).includes(
55+
"Generating qhelp in markdown",
56+
)
57+
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
58+
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
59+
void showAndLogExceptionWithTelemetry(errorMessage, {
60+
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
61+
});
62+
}
63+
}
64+
}
65+
66+
async function openReferencedFile(
67+
qs: QueryRunner,
68+
cliServer: CodeQLCliServer,
69+
selectedQuery: Uri,
70+
): Promise<void> {
71+
// If no file is selected, the path of the file in the editor is selected
72+
const path =
73+
selectedQuery?.fsPath || window.activeTextEditor?.document.uri.fsPath;
74+
if (qs !== undefined && path) {
75+
const resolved = await cliServer.resolveQlref(path);
76+
const uri = Uri.file(resolved.resolvedPath);
77+
await window.showTextDocument(uri, { preview: false });
78+
}
79+
}

0 commit comments

Comments
 (0)