Skip to content

Commit dd5da2b

Browse files
committed
Make query text and file commands consistent
We were using two different implementations for opening the query file and query text between the query history and the results view. This moves the better implementation in the view to a command and uses these commands for opening the query text/file in the query history and view. This results in consistent error messages and behaviour between the two different views.
1 parent bf84dbe commit dd5da2b

4 files changed

Lines changed: 99 additions & 69 deletions

File tree

extensions/ql-vscode/src/extension.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,24 @@ async function activateWithInstalledDistribution(
12731273
),
12741274
);
12751275

1276+
ctx.subscriptions.push(
1277+
commandRunner(
1278+
"codeQL.openVariantAnalysisQueryText",
1279+
async (variantAnalysisId: number) => {
1280+
await variantAnalysisManager.openQueryText(variantAnalysisId);
1281+
},
1282+
),
1283+
);
1284+
1285+
ctx.subscriptions.push(
1286+
commandRunner(
1287+
"codeQL.openVariantAnalysisQueryFile",
1288+
async (variantAnalysisId: number) => {
1289+
await variantAnalysisManager.openQueryFile(variantAnalysisId);
1290+
},
1291+
),
1292+
);
1293+
12761294
ctx.subscriptions.push(
12771295
commandRunner("codeQL.openReferencedFile", openReferencedFile),
12781296
);

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,

0 commit comments

Comments
 (0)