|
| 1 | +import { |
| 2 | + CancellationToken, |
| 3 | + ProgressOptions, |
| 4 | + window as Window, |
| 5 | + commands, |
| 6 | + Disposable, |
| 7 | + ProgressLocation |
| 8 | +} from 'vscode'; |
| 9 | +import { showAndLogErrorMessage, showAndLogWarningMessage } from './helpers'; |
| 10 | +import { logger } from './logging'; |
| 11 | + |
| 12 | +export class UserCancellationException extends Error { |
| 13 | + /** |
| 14 | + * @param message The error message |
| 15 | + * @param silent If silent is true, then this exception will avoid showing a warning message to the user. |
| 16 | + */ |
| 17 | + constructor(message?: string, public readonly silent = false) { |
| 18 | + super(message); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +export interface ProgressUpdate { |
| 23 | + /** |
| 24 | + * The current step |
| 25 | + */ |
| 26 | + step: number; |
| 27 | + /** |
| 28 | + * The maximum step. This *should* be constant for a single job. |
| 29 | + */ |
| 30 | + maxStep: number; |
| 31 | + /** |
| 32 | + * The current progress message |
| 33 | + */ |
| 34 | + message: string; |
| 35 | +} |
| 36 | + |
| 37 | +export type ProgressCallback = (p: ProgressUpdate) => void; |
| 38 | + |
| 39 | +/** |
| 40 | + * A task that handles command invocations from `commandRunner` |
| 41 | + * and includes a progress monitor. |
| 42 | + * |
| 43 | + * |
| 44 | + * Arguments passed to the command handler are passed along, |
| 45 | + * untouched to this `ProgressTask` instance. |
| 46 | + * |
| 47 | + * @param progress a progress handler function. Call this |
| 48 | + * function with a `ProgressUpdate` instance in order to |
| 49 | + * denote some progress being achieved on this task. |
| 50 | + * @param token a cencellation token |
| 51 | + * @param args arguments passed to this task passed on from |
| 52 | + * `commands.registerCommand`. |
| 53 | + */ |
| 54 | +export type ProgressTask<R> = ( |
| 55 | + progress: ProgressCallback, |
| 56 | + token: CancellationToken, |
| 57 | + ...args: any[] |
| 58 | +) => Thenable<R>; |
| 59 | + |
| 60 | +/** |
| 61 | + * A task that handles command invocations from `commandRunner`. |
| 62 | + * Arguments passed to the command handler are passed along, |
| 63 | + * untouched to this `NoProgressTask` instance. |
| 64 | + * |
| 65 | + * @param args arguments passed to this task passed on from |
| 66 | + * `commands.registerCommand`. |
| 67 | + */ |
| 68 | +type NoProgressTask = ((...args: any[]) => Promise<any>); |
| 69 | + |
| 70 | +/** |
| 71 | + * This mediates between the kind of progress callbacks we want to |
| 72 | + * write (where we *set* current progress position and give |
| 73 | + * `maxSteps`) and the kind vscode progress api expects us to write |
| 74 | + * (which increment progress by a certain amount out of 100%). |
| 75 | + * |
| 76 | + * Where possible, the `commandRunner` function below should be used |
| 77 | + * instead of this function. The commandRunner is meant for wrapping |
| 78 | + * top-level commands and provides error handling and other support |
| 79 | + * automatically. |
| 80 | + * |
| 81 | + * Only use this function if you need a progress monitor and the |
| 82 | + * control flow does not always come from a command (eg- during |
| 83 | + * extension activation, or from an internal language server |
| 84 | + * request). |
| 85 | + */ |
| 86 | +export function withProgress<R>( |
| 87 | + options: ProgressOptions, |
| 88 | + task: ProgressTask<R>, |
| 89 | + ...args: any[] |
| 90 | +): Thenable<R> { |
| 91 | + let progressAchieved = 0; |
| 92 | + return Window.withProgress(options, |
| 93 | + (progress, token) => { |
| 94 | + return task(p => { |
| 95 | + const { message, step, maxStep } = p; |
| 96 | + const increment = 100 * (step - progressAchieved) / maxStep; |
| 97 | + progressAchieved = step; |
| 98 | + progress.report({ message, increment }); |
| 99 | + }, token, ...args); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * A generic wrapper for command registration. This wrapper adds uniform error handling for commands. |
| 105 | + * |
| 106 | + * In this variant of the command runner, no progress monitor is used. |
| 107 | + * |
| 108 | + * @param commandId The ID of the command to register. |
| 109 | + * @param task The task to run. It is passed directly to `commands.registerCommand`. Any |
| 110 | + * arguments to the command handler are passed on to the task. |
| 111 | + */ |
| 112 | +export function commandRunner( |
| 113 | + commandId: string, |
| 114 | + task: NoProgressTask, |
| 115 | +): Disposable { |
| 116 | + return commands.registerCommand(commandId, async (...args: any[]) => { |
| 117 | + try { |
| 118 | + return await task(...args); |
| 119 | + } catch (e) { |
| 120 | + const errorMessage = `${e.message || e} (${commandId})`; |
| 121 | + if (e instanceof UserCancellationException) { |
| 122 | + // User has cancelled this action manually |
| 123 | + if (e.silent) { |
| 124 | + logger.log(errorMessage); |
| 125 | + } else { |
| 126 | + showAndLogWarningMessage(errorMessage); |
| 127 | + } |
| 128 | + } else { |
| 129 | + showAndLogErrorMessage(errorMessage); |
| 130 | + } |
| 131 | + return undefined; |
| 132 | + } |
| 133 | + }); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * A generic wrapper for command registration. This wrapper adds uniform error handling, |
| 138 | + * progress monitoring, and cancellation for commands. |
| 139 | + * |
| 140 | + * @param commandId The ID of the command to register. |
| 141 | + * @param task The task to run. It is passed directly to `commands.registerCommand`. Any |
| 142 | + * arguments to the command handler are passed on to the task after the progress callback |
| 143 | + * and cancellation token. |
| 144 | + * @param progressOptions Progress options to be sent to the progress monitor. |
| 145 | + */ |
| 146 | +export function commandRunnerWithProgress<R>( |
| 147 | + commandId: string, |
| 148 | + task: ProgressTask<R>, |
| 149 | + progressOptions: Partial<ProgressOptions> |
| 150 | +): Disposable { |
| 151 | + return commands.registerCommand(commandId, async (...args: any[]) => { |
| 152 | + const progressOptionsWithDefaults = { |
| 153 | + location: ProgressLocation.Notification, |
| 154 | + ...progressOptions |
| 155 | + }; |
| 156 | + try { |
| 157 | + return await withProgress(progressOptionsWithDefaults, task, ...args); |
| 158 | + } catch (e) { |
| 159 | + const errorMessage = `${e.message || e} (${commandId})`; |
| 160 | + if (e instanceof UserCancellationException) { |
| 161 | + // User has cancelled this action manually |
| 162 | + if (e.silent) { |
| 163 | + logger.log(errorMessage); |
| 164 | + } else { |
| 165 | + showAndLogWarningMessage(errorMessage); |
| 166 | + } |
| 167 | + } else { |
| 168 | + showAndLogErrorMessage(errorMessage); |
| 169 | + } |
| 170 | + return undefined; |
| 171 | + } |
| 172 | + }); |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * Displays a progress monitor that indicates how much progess has been made |
| 177 | + * reading from a stream. |
| 178 | + * |
| 179 | + * @param readable The stream to read progress from |
| 180 | + * @param messagePrefix A prefix for displaying the message |
| 181 | + * @param totalNumBytes Total number of bytes in this stream |
| 182 | + * @param progress The progress callback used to set messages |
| 183 | + */ |
| 184 | +export function reportStreamProgress( |
| 185 | + readable: NodeJS.ReadableStream, |
| 186 | + messagePrefix: string, |
| 187 | + totalNumBytes?: number, |
| 188 | + progress?: ProgressCallback |
| 189 | +) { |
| 190 | + if (progress && totalNumBytes) { |
| 191 | + let numBytesDownloaded = 0; |
| 192 | + const bytesToDisplayMB = (numBytes: number): string => `${(numBytes / (1024 * 1024)).toFixed(1)} MB`; |
| 193 | + const updateProgress = () => { |
| 194 | + progress({ |
| 195 | + step: numBytesDownloaded, |
| 196 | + maxStep: totalNumBytes, |
| 197 | + message: `${messagePrefix} [${bytesToDisplayMB(numBytesDownloaded)} of ${bytesToDisplayMB(totalNumBytes)}]`, |
| 198 | + }); |
| 199 | + }; |
| 200 | + |
| 201 | + // Display the progress straight away rather than waiting for the first chunk. |
| 202 | + updateProgress(); |
| 203 | + |
| 204 | + readable.on('data', data => { |
| 205 | + numBytesDownloaded += data.length; |
| 206 | + updateProgress(); |
| 207 | + }); |
| 208 | + } else if (progress) { |
| 209 | + progress({ |
| 210 | + step: 1, |
| 211 | + maxStep: 2, |
| 212 | + message: `${messagePrefix} (Size unknown)`, |
| 213 | + }); |
| 214 | + } |
| 215 | +} |
0 commit comments