Skip to content

Commit 0e744fd

Browse files
authored
Merge pull request #1822 from github/koesie10/consistent-query-text-file-commands
Make query text and file commands consistent
2 parents 39a0ef0 + 32f3d32 commit 0e744fd

6 files changed

Lines changed: 200 additions & 96 deletions

File tree

extensions/ql-vscode/src/extension.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,24 @@ async function activateWithInstalledDistribution(
12811281
),
12821282
);
12831283

1284+
ctx.subscriptions.push(
1285+
commandRunner(
1286+
"codeQL.openVariantAnalysisQueryText",
1287+
async (variantAnalysisId: number) => {
1288+
await variantAnalysisManager.openQueryText(variantAnalysisId);
1289+
},
1290+
),
1291+
);
1292+
1293+
ctx.subscriptions.push(
1294+
commandRunner(
1295+
"codeQL.openVariantAnalysisQueryFile",
1296+
async (variantAnalysisId: number) => {
1297+
await variantAnalysisManager.openQueryFile(variantAnalysisId);
1298+
},
1299+
),
1300+
);
1301+
12841302
ctx.subscriptions.push(
12851303
commandRunner("codeQL.openReferencedFile", openReferencedFile),
12861304
);

extensions/ql-vscode/src/query-history.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,14 @@ export class QueryHistoryManager extends DisposableObject {
826826
return;
827827
}
828828

829+
if (finalSingleItem.t === "variant-analysis") {
830+
await commands.executeCommand(
831+
"codeQL.openVariantAnalysisQueryFile",
832+
finalSingleItem.variantAnalysis.id,
833+
);
834+
return;
835+
}
836+
829837
let queryPath: string;
830838
switch (finalSingleItem.t) {
831839
case "local":
@@ -834,9 +842,6 @@ export class QueryHistoryManager extends DisposableObject {
834842
case "remote":
835843
queryPath = finalSingleItem.remoteQuery.queryFilePath;
836844
break;
837-
case "variant-analysis":
838-
queryPath = finalSingleItem.variantAnalysis.query.filePath;
839-
break;
840845
default:
841846
assertNever(finalSingleItem);
842847
}
@@ -1340,6 +1345,14 @@ export class QueryHistoryManager extends DisposableObject {
13401345
return;
13411346
}
13421347

1348+
if (finalSingleItem.t === "variant-analysis") {
1349+
await commands.executeCommand(
1350+
"codeQL.openVariantAnalysisQueryText",
1351+
finalSingleItem.variantAnalysis.id,
1352+
);
1353+
return;
1354+
}
1355+
13431356
const params = new URLSearchParams({
13441357
isQuickEval: String(
13451358
!!(

extensions/ql-vscode/src/remote-queries/variant-analysis-manager.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import {
1111
EventEmitter,
1212
ExtensionContext,
1313
Uri,
14+
ViewColumn,
15+
window as Window,
16+
workspace,
1417
} from "vscode";
1518
import { DisposableObject } from "../pure/disposable-object";
1619
import { Credentials } from "../authentication";
@@ -43,6 +46,7 @@ import {
4346
createTimestampFile,
4447
showAndLogErrorMessage,
4548
showAndLogInformationMessage,
49+
showAndLogWarningMessage,
4650
} from "../helpers";
4751
import { readFile, readJson, remove, pathExists, outputJson } from "fs-extra";
4852
import { EOL } from "os";
@@ -54,6 +58,7 @@ import {
5458
filterAndSortRepositoriesWithResults,
5559
RepositoriesFilterSortStateWithIds,
5660
} from "../pure/variant-analysis-filter-sort";
61+
import { URLSearchParams } from "url";
5762

5863
export class VariantAnalysisManager
5964
extends DisposableObject
@@ -267,6 +272,57 @@ export class VariantAnalysisManager
267272
return;
268273
}
269274

275+
public async openQueryText(variantAnalysisId: number): Promise<void> {
276+
const variantAnalysis = await this.getVariantAnalysis(variantAnalysisId);
277+
if (!variantAnalysis) {
278+
void showAndLogWarningMessage(
279+
"Could not open variant analysis query text. Variant analysis not found.",
280+
);
281+
return;
282+
}
283+
284+
const filename = variantAnalysis.query.filePath;
285+
286+
try {
287+
const params = new URLSearchParams({
288+
variantAnalysisId: variantAnalysis.id.toString(),
289+
});
290+
const uri = Uri.from({
291+
scheme: "codeql-variant-analysis",
292+
path: filename,
293+
query: params.toString(),
294+
});
295+
const doc = await workspace.openTextDocument(uri);
296+
await Window.showTextDocument(doc, { preview: false });
297+
} catch (error) {
298+
void showAndLogWarningMessage(
299+
"Could not open variant analysis query text. Failed to open text document.",
300+
);
301+
}
302+
}
303+
304+
public async openQueryFile(variantAnalysisId: number): Promise<void> {
305+
const variantAnalysis = await this.getVariantAnalysis(variantAnalysisId);
306+
307+
if (!variantAnalysis) {
308+
void showAndLogWarningMessage(
309+
"Could not open variant analysis query file",
310+
);
311+
return;
312+
}
313+
314+
try {
315+
const textDocument = await workspace.openTextDocument(
316+
variantAnalysis.query.filePath,
317+
);
318+
await Window.showTextDocument(textDocument, ViewColumn.One);
319+
} catch (error) {
320+
void showAndLogWarningMessage(
321+
`Could not open file: ${variantAnalysis.query.filePath}`,
322+
);
323+
}
324+
}
325+
270326
public registerView(view: VariantAnalysisView): void {
271327
if (this.views.has(view.variantAnalysisId)) {
272328
throw new Error(

extensions/ql-vscode/src/remote-queries/variant-analysis-view.ts

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
commands,
3-
ExtensionContext,
4-
Uri,
5-
ViewColumn,
6-
window as Window,
7-
workspace,
8-
} from "vscode";
9-
import { URLSearchParams } from "url";
1+
import { commands, ExtensionContext, Uri, ViewColumn } from "vscode";
102
import { AbstractWebview, WebviewPanelConfig } from "../abstract-webview";
113
import { extLogger } from "../common";
124
import {
@@ -129,10 +121,16 @@ export class VariantAnalysisView
129121
);
130122
break;
131123
case "openQueryFile":
132-
await this.openQueryFile();
124+
void commands.executeCommand(
125+
"codeQL.openVariantAnalysisQueryFile",
126+
this.variantAnalysisId,
127+
);
133128
break;
134129
case "openQueryText":
135-
await this.openQueryText();
130+
void commands.executeCommand(
131+
"codeQL.openVariantAnalysisQueryText",
132+
this.variantAnalysisId,
133+
);
136134
break;
137135
case "copyRepositoryList":
138136
void commands.executeCommand(
@@ -186,61 +184,6 @@ export class VariantAnalysisView
186184
});
187185
}
188186

189-
private async openQueryFile(): Promise<void> {
190-
const variantAnalysis = await this.manager.getVariantAnalysis(
191-
this.variantAnalysisId,
192-
);
193-
194-
if (!variantAnalysis) {
195-
void showAndLogWarningMessage(
196-
"Could not open variant analysis query file",
197-
);
198-
return;
199-
}
200-
201-
try {
202-
const textDocument = await workspace.openTextDocument(
203-
variantAnalysis.query.filePath,
204-
);
205-
await Window.showTextDocument(textDocument, ViewColumn.One);
206-
} catch (error) {
207-
void showAndLogWarningMessage(
208-
`Could not open file: ${variantAnalysis.query.filePath}`,
209-
);
210-
}
211-
}
212-
213-
private async openQueryText(): Promise<void> {
214-
const variantAnalysis = await this.manager.getVariantAnalysis(
215-
this.variantAnalysisId,
216-
);
217-
if (!variantAnalysis) {
218-
void showAndLogWarningMessage(
219-
"Could not open variant analysis query text. Variant analysis not found.",
220-
);
221-
return;
222-
}
223-
224-
const filename = variantAnalysis.query.filePath;
225-
226-
try {
227-
const params = new URLSearchParams({
228-
variantAnalysisId: variantAnalysis.id.toString(),
229-
});
230-
const uri = Uri.from({
231-
scheme: "codeql-variant-analysis",
232-
path: filename,
233-
query: params.toString(),
234-
});
235-
const doc = await workspace.openTextDocument(uri);
236-
await Window.showTextDocument(doc, { preview: false });
237-
} catch (error) {
238-
void showAndLogWarningMessage(
239-
"Could not open variant analysis query text. Failed to open text document.",
240-
);
241-
}
242-
}
243-
244187
private async openLogs(): Promise<void> {
245188
const variantAnalysis = await this.manager.getVariantAnalysis(
246189
this.variantAnalysisId,

extensions/ql-vscode/src/vscode-tests/cli-integration/remote-queries/variant-analysis-manager.test.ts

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import {
44
env,
55
extensions,
66
QuickPickItem,
7+
TextDocument,
8+
TextEditor,
79
Uri,
810
window,
11+
workspace,
912
} from "vscode";
1013
import { CodeQLExtensionInterface } from "../../../extension";
1114
import { extLogger } from "../../../common";
@@ -1121,4 +1124,94 @@ describe("Variant Analysis Manager", () => {
11211124
});
11221125
});
11231126
});
1127+
1128+
describe("openQueryText", () => {
1129+
let variantAnalysis: VariantAnalysis;
1130+
let variantAnalysisStorageLocation: string;
1131+
1132+
let showTextDocumentSpy: jest.SpiedFunction<typeof window.showTextDocument>;
1133+
let openTextDocumentSpy: jest.SpiedFunction<
1134+
typeof workspace.openTextDocument
1135+
>;
1136+
1137+
beforeEach(async () => {
1138+
variantAnalysis = createMockVariantAnalysis({});
1139+
1140+
variantAnalysisStorageLocation =
1141+
variantAnalysisManager.getVariantAnalysisStorageLocation(
1142+
variantAnalysis.id,
1143+
);
1144+
await createTimestampFile(variantAnalysisStorageLocation);
1145+
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
1146+
1147+
showTextDocumentSpy = jest
1148+
.spyOn(window, "showTextDocument")
1149+
.mockResolvedValue(undefined as unknown as TextEditor);
1150+
openTextDocumentSpy = jest
1151+
.spyOn(workspace, "openTextDocument")
1152+
.mockResolvedValue(undefined as unknown as TextDocument);
1153+
});
1154+
1155+
afterEach(() => {
1156+
fs.rmSync(variantAnalysisStorageLocation, { recursive: true });
1157+
});
1158+
1159+
it("opens the query text", async () => {
1160+
await variantAnalysisManager.openQueryText(variantAnalysis.id);
1161+
1162+
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
1163+
expect(openTextDocumentSpy).toHaveBeenCalledTimes(1);
1164+
1165+
const uri: Uri = openTextDocumentSpy.mock.calls[0][0] as Uri;
1166+
expect(uri.scheme).toEqual("codeql-variant-analysis");
1167+
expect(uri.path).toEqual(variantAnalysis.query.filePath);
1168+
const params = new URLSearchParams(uri.query);
1169+
expect(Array.from(params.keys())).toEqual(["variantAnalysisId"]);
1170+
expect(params.get("variantAnalysisId")).toEqual(
1171+
variantAnalysis.id.toString(),
1172+
);
1173+
});
1174+
});
1175+
1176+
describe("openQueryFile", () => {
1177+
let variantAnalysis: VariantAnalysis;
1178+
let variantAnalysisStorageLocation: string;
1179+
1180+
let showTextDocumentSpy: jest.SpiedFunction<typeof window.showTextDocument>;
1181+
let openTextDocumentSpy: jest.SpiedFunction<
1182+
typeof workspace.openTextDocument
1183+
>;
1184+
1185+
beforeEach(async () => {
1186+
variantAnalysis = createMockVariantAnalysis({});
1187+
1188+
variantAnalysisStorageLocation =
1189+
variantAnalysisManager.getVariantAnalysisStorageLocation(
1190+
variantAnalysis.id,
1191+
);
1192+
await createTimestampFile(variantAnalysisStorageLocation);
1193+
await variantAnalysisManager.rehydrateVariantAnalysis(variantAnalysis);
1194+
1195+
showTextDocumentSpy = jest
1196+
.spyOn(window, "showTextDocument")
1197+
.mockResolvedValue(undefined as unknown as TextEditor);
1198+
openTextDocumentSpy = jest
1199+
.spyOn(workspace, "openTextDocument")
1200+
.mockResolvedValue(undefined as unknown as TextDocument);
1201+
});
1202+
1203+
afterEach(() => {
1204+
fs.rmSync(variantAnalysisStorageLocation, { recursive: true });
1205+
});
1206+
1207+
it("opens the query file", async () => {
1208+
await variantAnalysisManager.openQueryFile(variantAnalysis.id);
1209+
1210+
expect(showTextDocumentSpy).toHaveBeenCalledTimes(1);
1211+
expect(openTextDocumentSpy).toHaveBeenCalledTimes(1);
1212+
1213+
const filename: string = openTextDocumentSpy.mock.calls[0][0] as string;
1214+
expect(filename).toEqual(variantAnalysis.query.filePath);
1215+
});
1216+
});
11241217
});

0 commit comments

Comments
 (0)