Skip to content

Commit e1d5a4d

Browse files
committed
Merge remote-tracking branch 'origin/main' into koesie10/base-typed-commands
2 parents fe6ff68 + 7059f46 commit e1d5a4d

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
@@ -42,6 +42,18 @@ export type BaseCommands = {
4242
"codeQL.restartQueryServer": () => Promise<void>;
4343
};
4444

45+
// Commands used when working with queries in the editor
46+
export type QueryEditorCommands = {
47+
"codeQL.openReferencedFile": (selectedQuery: Uri) => Promise<void>;
48+
"codeQL.openReferencedFileContextEditor": (
49+
selectedQuery: Uri,
50+
) => Promise<void>;
51+
"codeQL.openReferencedFileContextExplorer": (
52+
selectedQuery: Uri,
53+
) => Promise<void>;
54+
"codeQL.previewQueryHelp": (selectedQuery: Uri) => Promise<void>;
55+
};
56+
4557
// Commands used for running local queries
4658
export type LocalQueryCommands = {
4759
"codeQL.runQuery": (uri?: Uri) => Promise<void>;
@@ -222,6 +234,7 @@ export type MockGitHubApiServerCommands = {
222234
};
223235

224236
export type AllCommands = BaseCommands &
237+
QueryEditorCommands &
225238
ResultsViewCommands &
226239
QueryHistoryCommands &
227240
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
import { App } from "./common/app";
127126

128127
/**
@@ -865,6 +864,11 @@ async function activateWithInstalledDistribution(
865864

866865
const allCommands: AllCommands = {
867866
...getCommands(app, cliServer, qs),
867+
...getQueryEditorCommands({
868+
queryRunner: qs,
869+
cliServer,
870+
qhelpTmpDir: qhelpTmpDir.name,
871+
}),
868872
...localQueryResultsView.getCommands(),
869873
...qhm.getCommands(),
870874
...variantAnalysisManager.getCommands(),
@@ -914,38 +918,6 @@ async function activateWithInstalledDistribution(
914918
);
915919
}
916920

917-
ctx.subscriptions.push(
918-
commandRunner("codeQL.openReferencedFile", async (selectedQuery: Uri) => {
919-
await openReferencedFile(qs, cliServer, selectedQuery);
920-
}),
921-
);
922-
923-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
924-
ctx.subscriptions.push(
925-
commandRunner(
926-
"codeQL.openReferencedFileContextEditor",
927-
async (selectedQuery: Uri) => {
928-
await openReferencedFile(qs, cliServer, selectedQuery);
929-
},
930-
),
931-
);
932-
933-
// Since we are tracking extension usage through commands, this command mirrors the "codeQL.openReferencedFile" command
934-
ctx.subscriptions.push(
935-
commandRunner(
936-
"codeQL.openReferencedFileContextExplorer",
937-
async (selectedQuery: Uri) => {
938-
await openReferencedFile(qs, cliServer, selectedQuery);
939-
},
940-
),
941-
);
942-
943-
ctx.subscriptions.push(
944-
commandRunner("codeQL.previewQueryHelp", async (selectedQuery: Uri) => {
945-
await previewQueryHelp(cliServer, qhelpTmpDir, selectedQuery);
946-
}),
947-
);
948-
949921
void extLogger.log("Starting language server.");
950922
await client.start();
951923
ctx.subscriptions.push({
@@ -1013,51 +985,6 @@ async function showResultsForComparison(
1013985
}
1014986
}
1015987

1016-
async function previewQueryHelp(
1017-
cliServer: CodeQLCliServer,
1018-
qhelpTmpDir: DirResult,
1019-
selectedQuery: Uri,
1020-
): Promise<void> {
1021-
// selectedQuery is unpopulated when executing through the command palette
1022-
const pathToQhelp = selectedQuery
1023-
? selectedQuery.fsPath
1024-
: window.activeTextEditor?.document.uri.fsPath;
1025-
if (pathToQhelp) {
1026-
// Create temporary directory
1027-
const relativePathToMd = `${basename(pathToQhelp, ".qhelp")}.md`;
1028-
const absolutePathToMd = join(qhelpTmpDir.name, relativePathToMd);
1029-
const uri = Uri.file(absolutePathToMd);
1030-
try {
1031-
await cliServer.generateQueryHelp(pathToQhelp, absolutePathToMd);
1032-
await commands.executeCommand("markdown.showPreviewToSide", uri);
1033-
} catch (e) {
1034-
const errorMessage = getErrorMessage(e).includes(
1035-
"Generating qhelp in markdown",
1036-
)
1037-
? redactableError`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
1038-
: redactableError`Could not open a preview of the generated file (${absolutePathToMd}).`;
1039-
void showAndLogExceptionWithTelemetry(errorMessage, {
1040-
fullMessage: `${errorMessage}\n${getErrorMessage(e)}`,
1041-
});
1042-
}
1043-
}
1044-
}
1045-
1046-
async function openReferencedFile(
1047-
qs: QueryRunner,
1048-
cliServer: CodeQLCliServer,
1049-
selectedQuery: Uri,
1050-
): Promise<void> {
1051-
// If no file is selected, the path of the file in the editor is selected
1052-
const path =
1053-
selectedQuery?.fsPath || window.activeTextEditor?.document.uri.fsPath;
1054-
if (qs !== undefined && path) {
1055-
const resolved = await cliServer.resolveQlref(path);
1056-
const uri = Uri.file(resolved.resolvedPath);
1057-
await window.showTextDocument(uri, { preview: false });
1058-
}
1059-
}
1060-
1061988
function addUnhandledRejectionListener() {
1062989
const handler = (error: unknown) => {
1063990
// 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)