Skip to content

Commit d56a986

Browse files
Merge pull request #2222 from github/robertbrignull/use_app_commands_2
Convert call sites to use typed commands (part 2): QueryHistoryManager
2 parents 16b9abe + 180b84b commit d56a986

File tree

8 files changed

+172
-131
lines changed

8 files changed

+172
-131
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type BuiltInVsCodeCommands = {
3838
// The codeQLDatabases.focus command is provided by VS Code because we've registered the custom view
3939
"codeQLDatabases.focus": () => Promise<void>;
4040
"markdown.showPreviewToSide": (uri: Uri) => Promise<void>;
41+
revealFileInOS: (uri: Uri) => Promise<void>;
4142
setContext: (
4243
key: `${"codeql" | "codeQL"}${string}`,
4344
value: unknown,

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,7 @@ async function activateWithInstalledDistribution(
777777
};
778778

779779
const qhm = new QueryHistoryManager(
780+
app,
780781
qs,
781782
dbm,
782783
localQueryResultsView,

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

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { join, dirname } from "path";
22
import {
3-
commands,
43
Disposable,
54
env,
65
EventEmitter,
@@ -26,12 +25,7 @@ import { extLogger } from "../common";
2625
import { URLSearchParams } from "url";
2726
import { DisposableObject } from "../pure/disposable-object";
2827
import { ONE_HOUR_IN_MS, TWO_HOURS_IN_MS } from "../pure/time";
29-
import {
30-
asError,
31-
assertNever,
32-
getErrorMessage,
33-
getErrorStack,
34-
} from "../pure/helpers-pure";
28+
import { asError, assertNever, getErrorMessage } from "../pure/helpers-pure";
3529
import { CompletedLocalQueryInfo, LocalQueryInfo } from "../query-results";
3630
import {
3731
getActionsWorkflowRunUrl,
@@ -66,6 +60,8 @@ import { HistoryTreeDataProvider } from "./history-tree-data-provider";
6660
import { redactableError } from "../pure/errors";
6761
import { QueryHistoryDirs } from "./query-history-dirs";
6862
import { QueryHistoryCommands } from "../common/commands";
63+
import { App } from "../common/app";
64+
import { tryOpenExternalFile } from "../vscode-utils/external-files";
6965

7066
/**
7167
* query-history-manager.ts
@@ -135,6 +131,7 @@ export class QueryHistoryManager extends DisposableObject {
135131
readonly onDidCompleteQuery = this._onDidCompleteQuery.event;
136132

137133
constructor(
134+
private readonly app: App,
138135
private readonly qs: QueryRunner,
139136
private readonly dbm: DatabaseManager,
140137
private readonly localQueriesResultsView: ResultsView,
@@ -683,7 +680,10 @@ export class QueryHistoryManager extends DisposableObject {
683680
}
684681

685682
if (singleItem.completedQuery.logFileLocation) {
686-
await this.tryOpenExternalFile(singleItem.completedQuery.logFileLocation);
683+
await tryOpenExternalFile(
684+
this.app.commands,
685+
singleItem.completedQuery.logFileLocation,
686+
);
687687
} else {
688688
void showAndLogWarningMessage("No log file available");
689689
}
@@ -751,7 +751,7 @@ export class QueryHistoryManager extends DisposableObject {
751751
}
752752
}
753753
try {
754-
await commands.executeCommand(
754+
await this.app.commands.execute(
755755
"revealFileInOS",
756756
Uri.file(externalFilePath),
757757
);
@@ -800,7 +800,10 @@ export class QueryHistoryManager extends DisposableObject {
800800
}
801801

802802
if (finalSingleItem.evalLogLocation) {
803-
await this.tryOpenExternalFile(finalSingleItem.evalLogLocation);
803+
await tryOpenExternalFile(
804+
this.app.commands,
805+
finalSingleItem.evalLogLocation,
806+
);
804807
} else {
805808
this.warnNoEvalLogs();
806809
}
@@ -825,7 +828,10 @@ export class QueryHistoryManager extends DisposableObject {
825828
}
826829

827830
if (finalSingleItem.evalLogSummaryLocation) {
828-
await this.tryOpenExternalFile(finalSingleItem.evalLogSummaryLocation);
831+
await tryOpenExternalFile(
832+
this.app.commands,
833+
finalSingleItem.evalLogSummaryLocation,
834+
);
829835
return;
830836
}
831837

@@ -968,7 +974,10 @@ export class QueryHistoryManager extends DisposableObject {
968974
const query = finalSingleItem.completedQuery.query;
969975
const hasInterpretedResults = query.canHaveInterpretedResults();
970976
if (hasInterpretedResults) {
971-
await this.tryOpenExternalFile(query.resultsPaths.interpretedResultsPath);
977+
await tryOpenExternalFile(
978+
this.app.commands,
979+
query.resultsPaths.interpretedResultsPath,
980+
);
972981
} else {
973982
const label = this.labelProvider.getLabel(finalSingleItem);
974983
void showAndLogInformationMessage(
@@ -997,11 +1006,11 @@ export class QueryHistoryManager extends DisposableObject {
9971006
}
9981007
const query = finalSingleItem.completedQuery.query;
9991008
if (await query.hasCsv()) {
1000-
void this.tryOpenExternalFile(query.csvPath);
1009+
void tryOpenExternalFile(this.app.commands, query.csvPath);
10011010
return;
10021011
}
10031012
if (await query.exportCsvResults(this.qs.cliServer, query.csvPath)) {
1004-
void this.tryOpenExternalFile(query.csvPath);
1013+
void tryOpenExternalFile(this.app.commands, query.csvPath);
10051014
}
10061015
}
10071016

@@ -1024,7 +1033,8 @@ export class QueryHistoryManager extends DisposableObject {
10241033
return;
10251034
}
10261035

1027-
await this.tryOpenExternalFile(
1036+
await tryOpenExternalFile(
1037+
this.app.commands,
10281038
await finalSingleItem.completedQuery.query.ensureCsvAlerts(
10291039
this.qs.cliServer,
10301040
this.dbm,
@@ -1051,7 +1061,8 @@ export class QueryHistoryManager extends DisposableObject {
10511061
return;
10521062
}
10531063

1054-
await this.tryOpenExternalFile(
1064+
await tryOpenExternalFile(
1065+
this.app.commands,
10551066
await finalSingleItem.completedQuery.query.ensureDilPath(
10561067
this.qs.cliServer,
10571068
),
@@ -1077,7 +1088,7 @@ export class QueryHistoryManager extends DisposableObject {
10771088

10781089
const actionsWorkflowRunUrl = getActionsWorkflowRunUrl(finalSingleItem);
10791090

1080-
await commands.executeCommand(
1091+
await this.app.commands.execute(
10811092
"vscode.open",
10821093
Uri.parse(actionsWorkflowRunUrl),
10831094
);
@@ -1101,7 +1112,7 @@ export class QueryHistoryManager extends DisposableObject {
11011112
return;
11021113
}
11031114

1104-
await commands.executeCommand(
1115+
await this.app.commands.execute(
11051116
"codeQL.copyVariantAnalysisRepoList",
11061117
finalSingleItem.variantAnalysis.id,
11071118
);
@@ -1171,47 +1182,6 @@ export class QueryHistoryManager extends DisposableObject {
11711182
}
11721183
}
11731184

1174-
private async tryOpenExternalFile(fileLocation: string) {
1175-
const uri = Uri.file(fileLocation);
1176-
try {
1177-
await window.showTextDocument(uri, { preview: false });
1178-
} catch (e) {
1179-
const msg = getErrorMessage(e);
1180-
if (
1181-
msg.includes(
1182-
"Files above 50MB cannot be synchronized with extensions",
1183-
) ||
1184-
msg.includes("too large to open")
1185-
) {
1186-
const res = await showBinaryChoiceDialog(
1187-
`VS Code does not allow extensions to open files >50MB. This file
1188-
exceeds that limit. Do you want to open it outside of VS Code?
1189-
1190-
You can also try manually opening it inside VS Code by selecting
1191-
the file in the file explorer and dragging it into the workspace.`,
1192-
);
1193-
if (res) {
1194-
try {
1195-
await commands.executeCommand("revealFileInOS", uri);
1196-
} catch (e) {
1197-
void showAndLogExceptionWithTelemetry(
1198-
redactableError(
1199-
asError(e),
1200-
)`Failed to reveal file in OS: ${getErrorMessage(e)}`,
1201-
);
1202-
}
1203-
}
1204-
} else {
1205-
void showAndLogExceptionWithTelemetry(
1206-
redactableError(asError(e))`Could not open file ${fileLocation}`,
1207-
{
1208-
fullMessage: `${getErrorMessage(e)}\n${getErrorStack(e)}`,
1209-
},
1210-
);
1211-
}
1212-
}
1213-
}
1214-
12151185
private async findOtherQueryToCompare(
12161186
singleItem: QueryHistoryInfo,
12171187
multiSelect: QueryHistoryInfo[],
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Uri, window } from "vscode";
2+
import { AppCommandManager } from "../common/commands";
3+
import {
4+
showAndLogExceptionWithTelemetry,
5+
showBinaryChoiceDialog,
6+
} from "../helpers";
7+
import { redactableError } from "../pure/errors";
8+
import { asError, getErrorMessage, getErrorStack } from "../pure/helpers-pure";
9+
10+
export async function tryOpenExternalFile(
11+
commandManager: AppCommandManager,
12+
fileLocation: string,
13+
) {
14+
const uri = Uri.file(fileLocation);
15+
try {
16+
await window.showTextDocument(uri, { preview: false });
17+
} catch (e) {
18+
const msg = getErrorMessage(e);
19+
if (
20+
msg.includes("Files above 50MB cannot be synchronized with extensions") ||
21+
msg.includes("too large to open")
22+
) {
23+
const res = await showBinaryChoiceDialog(
24+
`VS Code does not allow extensions to open files >50MB. This file
25+
exceeds that limit. Do you want to open it outside of VS Code?
26+
27+
You can also try manually opening it inside VS Code by selecting
28+
the file in the file explorer and dragging it into the workspace.`,
29+
);
30+
if (res) {
31+
try {
32+
await commandManager.execute("revealFileInOS", uri);
33+
} catch (e) {
34+
void showAndLogExceptionWithTelemetry(
35+
redactableError(
36+
asError(e),
37+
)`Failed to reveal file in OS: ${getErrorMessage(e)}`,
38+
);
39+
}
40+
}
41+
} else {
42+
void showAndLogExceptionWithTelemetry(
43+
redactableError(asError(e))`Could not open file ${fileLocation}`,
44+
{
45+
fullMessage: `${getErrorMessage(e)}\n${getErrorStack(e)}`,
46+
},
47+
);
48+
}
49+
}
50+
}

extensions/ql-vscode/test/vscode-tests/no-workspace/query-history/history-tree-data-provider.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from "../../../../src/query-history/history-tree-data-provider";
2828
import { QueryHistoryManager } from "../../../../src/query-history/query-history-manager";
2929
import { createMockQueryHistoryDirs } from "../../../factories/query-history/query-history-dirs";
30+
import { createMockApp } from "../../../__mocks__/appMock";
3031

3132
describe("HistoryTreeDataProvider", () => {
3233
const mockExtensionLocation = join(tmpDir.name, "mock-extension-location");
@@ -421,6 +422,7 @@ describe("HistoryTreeDataProvider", () => {
421422

422423
async function createMockQueryHistory(allHistory: QueryHistoryInfo[]) {
423424
const qhm = new QueryHistoryManager(
425+
createMockApp({}),
424426
{} as QueryRunner,
425427
{} as DatabaseManager,
426428
localQueriesResultsViewStub,

0 commit comments

Comments
 (0)