Make progress options optional

This will make the progress options passed to `withProgress` optional by
moving it to be the second argument and setting a default value for the
`location`. This will make it much easier to use from a variety of
commands.
This commit is contained in:
Koen Vlaswinkel
2023-03-13 15:13:55 +01:00
parent 2646716261
commit 4969a08531
4 changed files with 53 additions and 42 deletions

View File

@@ -1,6 +1,6 @@
import {
CancellationToken,
ProgressOptions,
ProgressOptions as VSCodeProgressOptions,
window as Window,
commands,
Disposable,
@@ -42,6 +42,11 @@ export interface ProgressUpdate {
export type ProgressCallback = (p: ProgressUpdate) => void;
// Make certain properties within a type optional
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
export type ProgressOptions = Optional<VSCodeProgressOptions, "location">;
/**
* A task that reports progress.
*
@@ -103,18 +108,29 @@ type NoProgressTask = (...args: any[]) => Promise<any>;
* request).
*/
export function withProgress<R>(
options: ProgressOptions,
task: ProgressTask<R>,
{
location = ProgressLocation.Notification,
title,
cancellable,
}: ProgressOptions = {},
): Thenable<R> {
let progressAchieved = 0;
return Window.withProgress(options, (progress, token) => {
return task((p) => {
const { message, step, maxStep } = p;
const increment = (100 * (step - progressAchieved)) / maxStep;
progressAchieved = step;
progress.report({ message, increment });
}, token);
});
return Window.withProgress(
{
location,
title,
cancellable,
},
(progress, token) => {
return task((p) => {
const { message, step, maxStep } = p;
const increment = (100 * (step - progressAchieved)) / maxStep;
progressAchieved = step;
progress.report({ message, increment });
}, token);
},
);
}
/**
@@ -186,19 +202,15 @@ export function commandRunner(
export function commandRunnerWithProgress<R>(
commandId: string,
task: ProgressTaskWithArgs<R>,
progressOptions: Partial<ProgressOptions>,
progressOptions: ProgressOptions,
outputLogger = extLogger,
): Disposable {
return commandRunner(
commandId,
async (...args: any[]) => {
const progressOptionsWithDefaults = {
location: ProgressLocation.Notification,
...progressOptions,
};
return withProgress(progressOptionsWithDefaults, (progress, token) =>
task(progress, token, ...args),
return withProgress(
(progress, token) => task(progress, token, ...args),
progressOptions,
);
},
outputLogger,

View File

@@ -73,11 +73,6 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
private async getDefinitions(uriString: string): Promise<LocationLink[]> {
return withProgress(
{
location: ProgressLocation.Notification,
cancellable: true,
title: "Finding definitions",
},
async (progress, token) => {
return getLocationsForUriString(
this.cli,
@@ -91,6 +86,11 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
(src, _dest) => src === uriString,
);
},
{
location: ProgressLocation.Notification,
cancellable: true,
title: "Finding definitions",
},
);
}
}
@@ -136,11 +136,6 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
private async getReferences(uriString: string): Promise<FullLocationLink[]> {
return withProgress(
{
location: ProgressLocation.Notification,
cancellable: true,
title: "Finding references",
},
async (progress, token) => {
return getLocationsForUriString(
this.cli,
@@ -154,6 +149,11 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
(src, _dest) => src === uriString,
);
},
{
location: ProgressLocation.Notification,
cancellable: true,
title: "Finding references",
},
);
}
}

View File

@@ -9,7 +9,6 @@ import {
extensions,
languages,
ProgressLocation,
ProgressOptions,
QuickPickItem,
Range,
Uri,
@@ -322,16 +321,16 @@ export async function activate(
await commands.executeCommand("workbench.action.reloadWindow");
}
} else {
const progressOptions: ProgressOptions = {
title: progressTitle,
location: ProgressLocation.Notification,
};
await withProgress(progressOptions, (progress) =>
distributionManager.installExtensionManagedDistributionRelease(
result.updatedRelease,
progress,
),
await withProgress(
(progress) =>
distributionManager.installExtensionManagedDistributionRelease(
result.updatedRelease,
progress,
),
{
title: progressTitle,
location: ProgressLocation.Notification,
},
);
await ctx.globalState.update(shouldUpdateOnNextActivationKey, false);

View File

@@ -795,9 +795,6 @@ export class DatabaseManager extends DisposableObject {
public async loadPersistedState(): Promise<void> {
return withProgress(
{
location: vscode.ProgressLocation.Notification,
},
async (progress, token) => {
const currentDatabaseUri =
this.ctx.workspaceState.get<string>(CURRENT_DB);
@@ -861,6 +858,9 @@ export class DatabaseManager extends DisposableObject {
void this.logger.log("Finished loading persisted databases.");
},
{
location: vscode.ProgressLocation.Notification,
},
);
}