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" ;
92import {
103 showAndLogExceptionWithTelemetry ,
114 showAndLogWarningMessage ,
@@ -14,51 +7,22 @@ import { extLogger } from "./common";
147import { asError , getErrorMessage , getErrorStack } from "./pure/helpers-pure" ;
158import { telemetryListener } from "./telemetry" ;
169import { 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- }
0 commit comments