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