Skip to content

Commit 0b27912

Browse files
committed
Make model editor open progress bar cancellable
1 parent 6a65094 commit 0b27912

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

extensions/ql-vscode/src/model-editor/model-editor-queries.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import {
77
import { CancellationToken } from "vscode";
88
import { CodeQLCliServer } from "../codeql-cli/cli";
99
import { DatabaseItem } from "../databases/local-databases";
10-
import { ProgressCallback } from "../common/vscode/progress";
10+
import {
11+
ProgressCallback,
12+
UserCancellationException,
13+
} from "../common/vscode/progress";
1114
import { redactableError } from "../common/errors";
1215
import { telemetryListener } from "../common/vscode/telemetry";
1316
import { join } from "path";
@@ -89,6 +92,13 @@ export async function runModelEditorQueries(
8992
// For a reference of what this should do in the future, see the previous implementation in
9093
// https://github.com/github/vscode-codeql/blob/089d3566ef0bc67d9b7cc66e8fd6740b31c1c0b0/extensions/ql-vscode/src/data-extensions-editor/external-api-usage-query.ts#L33-L72
9194

95+
if (token.isCancellationRequested) {
96+
throw new UserCancellationException(
97+
"Run model editor queries cancelled.",
98+
true,
99+
);
100+
}
101+
92102
progress({
93103
message: "Resolving QL packs",
94104
step: 1,
@@ -99,6 +109,13 @@ export async function runModelEditorQueries(
99109
await cliServer.resolveQlpacks(additionalPacks, true),
100110
);
101111

112+
if (token.isCancellationRequested) {
113+
throw new UserCancellationException(
114+
"Run model editor queries cancelled.",
115+
true,
116+
);
117+
}
118+
102119
progress({
103120
message: "Resolving query",
104121
step: 2,
@@ -143,6 +160,13 @@ export async function runModelEditorQueries(
143160
return;
144161
}
145162

163+
if (token.isCancellationRequested) {
164+
throw new UserCancellationException(
165+
"Run model editor queries cancelled.",
166+
true,
167+
);
168+
}
169+
146170
// Read the results and covert to internal representation
147171
progress({
148172
message: "Decoding results",
@@ -159,6 +183,13 @@ export async function runModelEditorQueries(
159183
return;
160184
}
161185

186+
if (token.isCancellationRequested) {
187+
throw new UserCancellationException(
188+
"Run model editor queries cancelled.",
189+
true,
190+
);
191+
}
192+
162193
progress({
163194
message: "Finalizing results",
164195
step: 1950,

extensions/ql-vscode/src/model-editor/model-editor-view.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
CancellationToken,
23
CancellationTokenSource,
34
Tab,
45
TabInputWebview,
@@ -14,7 +15,11 @@ import {
1415
FromModelEditorMessage,
1516
ToModelEditorMessage,
1617
} from "../common/interface-types";
17-
import { ProgressCallback, withProgress } from "../common/vscode/progress";
18+
import {
19+
ProgressCallback,
20+
UserCancellationException,
21+
withProgress,
22+
} from "../common/vscode/progress";
1823
import { QueryRunner } from "../query-server";
1924
import {
2025
showAndLogErrorMessage,
@@ -338,8 +343,8 @@ export class ModelEditorView extends AbstractWebview<
338343

339344
await Promise.all([
340345
this.setViewState(),
341-
withProgress((progress) => this.loadMethods(progress), {
342-
cancellable: false,
346+
withProgress((progress, token) => this.loadMethods(progress, token), {
347+
cancellable: true,
343348
}),
344349
this.loadExistingModeledMethods(),
345350
]);
@@ -423,11 +428,16 @@ export class ModelEditorView extends AbstractWebview<
423428
}
424429
}
425430

426-
protected async loadMethods(progress: ProgressCallback): Promise<void> {
431+
protected async loadMethods(
432+
progress: ProgressCallback,
433+
token?: CancellationToken,
434+
): Promise<void> {
427435
const mode = this.modelingStore.getMode(this.databaseItem);
428436

429437
try {
430-
const cancellationTokenSource = new CancellationTokenSource();
438+
if (!token) {
439+
token = new CancellationTokenSource().token;
440+
}
431441
const queryResult = await runModelEditorQueries(mode, {
432442
cliServer: this.cliServer,
433443
queryRunner: this.queryRunner,
@@ -441,12 +451,19 @@ export class ModelEditorView extends AbstractWebview<
441451
...update,
442452
message: `Loading models: ${update.message}`,
443453
}),
444-
token: cancellationTokenSource.token,
454+
token,
445455
});
446456
if (!queryResult) {
447457
return;
448458
}
449459

460+
if (token.isCancellationRequested) {
461+
throw new UserCancellationException(
462+
"Model editor: Load methods cancelled.",
463+
true,
464+
);
465+
}
466+
450467
this.modelingStore.setMethods(this.databaseItem, queryResult);
451468
} catch (err) {
452469
void showAndLogExceptionWithTelemetry(
@@ -578,6 +595,7 @@ export class ModelEditorView extends AbstractWebview<
578595
this.modelConfig,
579596
this.app.logger,
580597
progress,
598+
token,
581599
3,
582600
);
583601
if (!modelFile) {

0 commit comments

Comments
 (0)