Skip to content

Commit 5ac5de8

Browse files
committed
Move query editing commands to separate file
1 parent 125af11 commit 5ac5de8

File tree

2 files changed

+104
-79
lines changed

2 files changed

+104
-79
lines changed

extensions/ql-vscode/src/extension.ts

Lines changed: 7 additions & 79 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,
@@ -122,6 +120,7 @@ import {
122120
showResultsForCompletedQuery,
123121
} from "./local-queries";
124122
import { getAstCfgCommands } from "./ast-cfg-commands";
123+
import { registerQueryEditorCommands } from "./query-editor";
125124

126125
/**
127126
* extension.ts
@@ -878,37 +877,11 @@ async function activateWithInstalledDistribution(
878877
);
879878
}
880879

881-
ctx.subscriptions.push(
882-
commandRunner("codeQL.openReferencedFile", async (selectedQuery: Uri) => {
883-
await openReferencedFile(qs, cliServer, selectedQuery);
884-
}),
885-
);
886-
887-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
888-
ctx.subscriptions.push(
889-
commandRunner(
890-
"codeQL.openReferencedFileContextEditor",
891-
async (selectedQuery: Uri) => {
892-
await openReferencedFile(qs, cliServer, selectedQuery);
893-
},
894-
),
895-
);
896-
897-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
898-
ctx.subscriptions.push(
899-
commandRunner(
900-
"codeQL.openReferencedFileContextExplorer",
901-
async (selectedQuery: Uri) => {
902-
await openReferencedFile(qs, cliServer, selectedQuery);
903-
},
904-
),
905-
);
906-
907-
ctx.subscriptions.push(
908-
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
909-
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
910-
}),
911-
);
880+
registerQueryEditorCommands(ctx, {
881+
queryRunner: qs,
882+
cliServer,
883+
qhelpTmpDir: qhelpTmpDir.name,
884+
});
912885

913886
ctx.subscriptions.push(
914887
commandRunner("codeQL.copyVersion", async () => {
@@ -1015,51 +988,6 @@ async function showResultsForComparison(
1015988
}
1016989
}
1017990

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

0 commit comments

Comments
 (0)