Skip to content

Commit 6afdf63

Browse files
Merge pull request #2202 from github/robertbrignull/extract_progress
Move withProgress and associated code to a separate file
2 parents b2fceb9 + 408c042 commit 6afdf63

34 files changed

+175
-183
lines changed

extensions/ql-vscode/src/ast-cfg-commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Uri, window } from "vscode";
2-
import { withProgress } from "./commandRunner";
2+
import { withProgress } from "./progress";
33
import { AstViewer } from "./astViewer";
44
import {
55
TemplatePrintAstProvider,
Lines changed: 14 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import {
2-
CancellationToken,
3-
ProgressOptions as VSCodeProgressOptions,
4-
window as Window,
5-
commands,
6-
Disposable,
7-
ProgressLocation,
8-
} from "vscode";
1+
import { CancellationToken, commands, Disposable } from "vscode";
92
import {
103
showAndLogExceptionWithTelemetry,
114
showAndLogWarningMessage,
@@ -14,51 +7,22 @@ import { extLogger } from "./common";
147
import { asError, getErrorMessage, getErrorStack } from "./pure/helpers-pure";
158
import { telemetryListener } from "./telemetry";
169
import { redactableError } from "./pure/errors";
17-
18-
export class UserCancellationException extends Error {
19-
/**
20-
* @param message The error message
21-
* @param silent If silent is true, then this exception will avoid showing a warning message to the user.
22-
*/
23-
constructor(message?: string, public readonly silent = false) {
24-
super(message);
25-
}
26-
}
27-
28-
export interface ProgressUpdate {
29-
/**
30-
* The current step
31-
*/
32-
step: number;
33-
/**
34-
* The maximum step. This *should* be constant for a single job.
35-
*/
36-
maxStep: number;
37-
/**
38-
* The current progress message
39-
*/
40-
message: string;
41-
}
42-
43-
export type ProgressCallback = (p: ProgressUpdate) => void;
44-
45-
// Make certain properties within a type optional
46-
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
47-
48-
export type ProgressOptions = Optional<VSCodeProgressOptions, "location">;
10+
import {
11+
UserCancellationException,
12+
withProgress,
13+
ProgressOptions,
14+
ProgressCallback,
15+
} from "./progress";
4916

5017
/**
51-
* A task that reports progress.
18+
* A task that handles command invocations from `commandRunner`.
19+
* Arguments passed to the command handler are passed along,
20+
* untouched to this `NoProgressTask` instance.
5221
*
53-
* @param progress a progress handler function. Call this
54-
* function with a `ProgressUpdate` instance in order to
55-
* denote some progress being achieved on this task.
56-
* @param token a cancellation token
22+
* @param args arguments passed to this task passed on from
23+
* `commands.registerCommand`.
5724
*/
58-
export type ProgressTask<R> = (
59-
progress: ProgressCallback,
60-
token: CancellationToken,
61-
) => Thenable<R>;
25+
export type NoProgressTask = (...args: any[]) => Promise<any>;
6226

6327
/**
6428
* A task that handles command invocations from `commandRunner`
@@ -75,64 +39,12 @@ export type ProgressTask<R> = (
7539
* @param args arguments passed to this task passed on from
7640
* `commands.registerCommand`.
7741
*/
78-
export type ProgressTaskWithArgs<R> = (
42+
type ProgressTaskWithArgs<R> = (
7943
progress: ProgressCallback,
8044
token: CancellationToken,
8145
...args: any[]
8246
) => Thenable<R>;
8347

84-
/**
85-
* A task that handles command invocations from `commandRunner`.
86-
* Arguments passed to the command handler are passed along,
87-
* untouched to this `NoProgressTask` instance.
88-
*
89-
* @param args arguments passed to this task passed on from
90-
* `commands.registerCommand`.
91-
*/
92-
export type NoProgressTask = (...args: any[]) => Promise<any>;
93-
94-
/**
95-
* This mediates between the kind of progress callbacks we want to
96-
* write (where we *set* current progress position and give
97-
* `maxSteps`) and the kind vscode progress api expects us to write
98-
* (which increment progress by a certain amount out of 100%).
99-
*
100-
* Where possible, the `commandRunner` function below should be used
101-
* instead of this function. The commandRunner is meant for wrapping
102-
* top-level commands and provides error handling and other support
103-
* automatically.
104-
*
105-
* Only use this function if you need a progress monitor and the
106-
* control flow does not always come from a command (eg- during
107-
* extension activation, or from an internal language server
108-
* request).
109-
*/
110-
export function withProgress<R>(
111-
task: ProgressTask<R>,
112-
{
113-
location = ProgressLocation.Notification,
114-
title,
115-
cancellable,
116-
}: ProgressOptions = {},
117-
): Thenable<R> {
118-
let progressAchieved = 0;
119-
return Window.withProgress(
120-
{
121-
location,
122-
title,
123-
cancellable,
124-
},
125-
(progress, token) => {
126-
return task((p) => {
127-
const { message, step, maxStep } = p;
128-
const increment = (100 * (step - progressAchieved)) / maxStep;
129-
progressAchieved = step;
130-
progress.report({ message, increment });
131-
}, token);
132-
},
133-
);
134-
}
135-
13648
/**
13749
* A generic wrapper for command registration. This wrapper adds uniform error handling for commands.
13850
*
@@ -216,48 +128,3 @@ export function commandRunnerWithProgress<R>(
216128
outputLogger,
217129
);
218130
}
219-
220-
/**
221-
* Displays a progress monitor that indicates how much progess has been made
222-
* reading from a stream.
223-
*
224-
* @param readable The stream to read progress from
225-
* @param messagePrefix A prefix for displaying the message
226-
* @param totalNumBytes Total number of bytes in this stream
227-
* @param progress The progress callback used to set messages
228-
*/
229-
export function reportStreamProgress(
230-
readable: NodeJS.ReadableStream,
231-
messagePrefix: string,
232-
totalNumBytes?: number,
233-
progress?: ProgressCallback,
234-
) {
235-
if (progress && totalNumBytes) {
236-
let numBytesDownloaded = 0;
237-
const bytesToDisplayMB = (numBytes: number): string =>
238-
`${(numBytes / (1024 * 1024)).toFixed(1)} MB`;
239-
const updateProgress = () => {
240-
progress({
241-
step: numBytesDownloaded,
242-
maxStep: totalNumBytes,
243-
message: `${messagePrefix} [${bytesToDisplayMB(
244-
numBytesDownloaded,
245-
)} of ${bytesToDisplayMB(totalNumBytes)}]`,
246-
});
247-
};
248-
249-
// Display the progress straight away rather than waiting for the first chunk.
250-
updateProgress();
251-
252-
readable.on("data", (data) => {
253-
numBytesDownloaded += data.length;
254-
updateProgress();
255-
});
256-
} else if (progress) {
257-
progress({
258-
step: 1,
259-
maxStep: 2,
260-
message: `${messagePrefix} (Size unknown)`,
261-
});
262-
}
263-
}

extensions/ql-vscode/src/contextual/locationFinder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { CodeQLCliServer } from "../cli";
1212
import { DatabaseManager, DatabaseItem } from "../local-databases";
1313
import fileRangeFromURI from "./fileRangeFromURI";
14-
import { ProgressCallback } from "../commandRunner";
14+
import { ProgressCallback } from "../progress";
1515
import { KeyType } from "./keyType";
1616
import {
1717
qlpackOfDatabase,

extensions/ql-vscode/src/contextual/queryResolver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DatabaseItem } from "../local-databases";
1616
import { extLogger } from "../common";
1717
import { createInitialQueryInfo } from "../run-queries-shared";
1818
import { CancellationToken, Uri } from "vscode";
19-
import { ProgressCallback } from "../commandRunner";
19+
import { ProgressCallback } from "../progress";
2020
import { QueryRunner } from "../queryRunner";
2121
import { redactableError } from "../pure/errors";
2222
import { QLPACK_FILENAMES } from "../pure/ql";

extensions/ql-vscode/src/contextual/templateProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { CodeQLCliServer } from "../cli";
1919
import { DatabaseManager } from "../local-databases";
2020
import { CachedOperation } from "../helpers";
21-
import { ProgressCallback, withProgress } from "../commandRunner";
21+
import { ProgressCallback, withProgress } from "../progress";
2222
import AstBuilder from "./astBuilder";
2323
import { KeyType } from "./keyType";
2424
import {

extensions/ql-vscode/src/databaseFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { retry } from "@octokit/plugin-retry";
1818

1919
import { DatabaseManager, DatabaseItem } from "./local-databases";
2020
import { showAndLogInformationMessage, tmpDir } from "./helpers";
21-
import { reportStreamProgress, ProgressCallback } from "./commandRunner";
21+
import { reportStreamProgress, ProgressCallback } from "./progress";
2222
import { extLogger } from "./common";
2323
import { getErrorMessage } from "./pure/helpers-pure";
2424
import {

extensions/ql-vscode/src/databases/ui/db-panel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
window,
88
workspace,
99
} from "vscode";
10-
import { UserCancellationException } from "../../commandRunner";
10+
import { UserCancellationException } from "../../progress";
1111
import {
1212
getNwoFromGitHubUrl,
1313
isValidGitHubNwo,

extensions/ql-vscode/src/distribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from "./helpers";
1515
import { extLogger } from "./common";
1616
import { getCodeQlCliVersion } from "./cli-version";
17-
import { ProgressCallback, reportStreamProgress } from "./commandRunner";
17+
import { ProgressCallback, reportStreamProgress } from "./progress";
1818
import {
1919
codeQlLauncherName,
2020
deprecatedCodeQlLauncherName,

extensions/ql-vscode/src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ import { QLTestAdapterFactory } from "./test-adapter";
8989
import { TestUIService } from "./test-ui";
9090
import { CompareView } from "./compare/compare-view";
9191
import { initializeTelemetry } from "./telemetry";
92-
import { commandRunner, ProgressCallback, withProgress } from "./commandRunner";
92+
import { commandRunner } from "./commandRunner";
93+
import { ProgressCallback, withProgress } from "./progress";
9394
import { CodeQlStatusBarHandler } from "./status-bar";
9495
import { getPackagingCommands } from "./packaging";
9596
import { HistoryItemLabelProvider } from "./query-history/history-item-label-provider";

extensions/ql-vscode/src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
commands,
2020
} from "vscode";
2121
import { CodeQLCliServer, QlpacksInfo } from "./cli";
22-
import { UserCancellationException } from "./commandRunner";
22+
import { UserCancellationException } from "./progress";
2323
import { extLogger, OutputChannelLogger } from "./common";
2424
import { QueryMetadata } from "./pure/interface-types";
2525
import { telemetryListener } from "./telemetry";

0 commit comments

Comments
 (0)