Skip to content

Commit 4969a08

Browse files
committed
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.
1 parent 2646716 commit 4969a08

4 files changed

Lines changed: 53 additions & 42 deletions

File tree

extensions/ql-vscode/src/commandRunner.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
CancellationToken,
3-
ProgressOptions,
3+
ProgressOptions as VSCodeProgressOptions,
44
window as Window,
55
commands,
66
Disposable,
@@ -42,6 +42,11 @@ export interface ProgressUpdate {
4242

4343
export type ProgressCallback = (p: ProgressUpdate) => void;
4444

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">;
49+
4550
/**
4651
* A task that reports progress.
4752
*
@@ -103,18 +108,29 @@ type NoProgressTask = (...args: any[]) => Promise<any>;
103108
* request).
104109
*/
105110
export function withProgress<R>(
106-
options: ProgressOptions,
107111
task: ProgressTask<R>,
112+
{
113+
location = ProgressLocation.Notification,
114+
title,
115+
cancellable,
116+
}: ProgressOptions = {},
108117
): Thenable<R> {
109118
let progressAchieved = 0;
110-
return Window.withProgress(options, (progress, token) => {
111-
return task((p) => {
112-
const { message, step, maxStep } = p;
113-
const increment = (100 * (step - progressAchieved)) / maxStep;
114-
progressAchieved = step;
115-
progress.report({ message, increment });
116-
}, token);
117-
});
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+
);
118134
}
119135

120136
/**
@@ -186,19 +202,15 @@ export function commandRunner(
186202
export function commandRunnerWithProgress<R>(
187203
commandId: string,
188204
task: ProgressTaskWithArgs<R>,
189-
progressOptions: Partial<ProgressOptions>,
205+
progressOptions: ProgressOptions,
190206
outputLogger = extLogger,
191207
): Disposable {
192208
return commandRunner(
193209
commandId,
194210
async (...args: any[]) => {
195-
const progressOptionsWithDefaults = {
196-
location: ProgressLocation.Notification,
197-
...progressOptions,
198-
};
199-
200-
return withProgress(progressOptionsWithDefaults, (progress, token) =>
201-
task(progress, token, ...args),
211+
return withProgress(
212+
(progress, token) => task(progress, token, ...args),
213+
progressOptions,
202214
);
203215
},
204216
outputLogger,

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,6 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
7373

7474
private async getDefinitions(uriString: string): Promise<LocationLink[]> {
7575
return withProgress(
76-
{
77-
location: ProgressLocation.Notification,
78-
cancellable: true,
79-
title: "Finding definitions",
80-
},
8176
async (progress, token) => {
8277
return getLocationsForUriString(
8378
this.cli,
@@ -91,6 +86,11 @@ export class TemplateQueryDefinitionProvider implements DefinitionProvider {
9186
(src, _dest) => src === uriString,
9287
);
9388
},
89+
{
90+
location: ProgressLocation.Notification,
91+
cancellable: true,
92+
title: "Finding definitions",
93+
},
9494
);
9595
}
9696
}
@@ -136,11 +136,6 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
136136

137137
private async getReferences(uriString: string): Promise<FullLocationLink[]> {
138138
return withProgress(
139-
{
140-
location: ProgressLocation.Notification,
141-
cancellable: true,
142-
title: "Finding references",
143-
},
144139
async (progress, token) => {
145140
return getLocationsForUriString(
146141
this.cli,
@@ -154,6 +149,11 @@ export class TemplateQueryReferenceProvider implements ReferenceProvider {
154149
(src, _dest) => src === uriString,
155150
);
156151
},
152+
{
153+
location: ProgressLocation.Notification,
154+
cancellable: true,
155+
title: "Finding references",
156+
},
157157
);
158158
}
159159
}

extensions/ql-vscode/src/extension.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
extensions,
1010
languages,
1111
ProgressLocation,
12-
ProgressOptions,
1312
QuickPickItem,
1413
Range,
1514
Uri,
@@ -322,16 +321,16 @@ export async function activate(
322321
await commands.executeCommand("workbench.action.reloadWindow");
323322
}
324323
} else {
325-
const progressOptions: ProgressOptions = {
326-
title: progressTitle,
327-
location: ProgressLocation.Notification,
328-
};
329-
330-
await withProgress(progressOptions, (progress) =>
331-
distributionManager.installExtensionManagedDistributionRelease(
332-
result.updatedRelease,
333-
progress,
334-
),
324+
await withProgress(
325+
(progress) =>
326+
distributionManager.installExtensionManagedDistributionRelease(
327+
result.updatedRelease,
328+
progress,
329+
),
330+
{
331+
title: progressTitle,
332+
location: ProgressLocation.Notification,
333+
},
335334
);
336335

337336
await ctx.globalState.update(shouldUpdateOnNextActivationKey, false);

extensions/ql-vscode/src/local-databases.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,6 @@ export class DatabaseManager extends DisposableObject {
795795

796796
public async loadPersistedState(): Promise<void> {
797797
return withProgress(
798-
{
799-
location: vscode.ProgressLocation.Notification,
800-
},
801798
async (progress, token) => {
802799
const currentDatabaseUri =
803800
this.ctx.workspaceState.get<string>(CURRENT_DB);
@@ -861,6 +858,9 @@ export class DatabaseManager extends DisposableObject {
861858

862859
void this.logger.log("Finished loading persisted databases.");
863860
},
861+
{
862+
location: vscode.ProgressLocation.Notification,
863+
},
864864
);
865865
}
866866

0 commit comments

Comments
 (0)