Skip to content

Commit fe219e0

Browse files
committed
Refactor extension context subscriptions
Use DiposableObject more consistently and ensure all commands are added as a disposable to the ExtensionContext.
1 parent 2dcf3b3 commit fe219e0

File tree

5 files changed

+90
-68
lines changed

5 files changed

+90
-68
lines changed

extensions/ql-vscode/src/astViewer.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
window,
3-
ExtensionContext,
43
TreeDataProvider,
54
EventEmitter,
65
Event,
@@ -19,6 +18,7 @@ import { UrlValue, BqrsId } from './bqrs-cli-types';
1918
import { showLocation } from './interface-utils';
2019
import { isStringLoc, isWholeFileLoc, isLineColumnLoc } from './bqrs-utils';
2120
import { commandRunner } from './helpers';
21+
import { DisposableObject } from './vscode-utils/disposable-object';
2222

2323
export interface AstItem {
2424
id: BqrsId;
@@ -33,7 +33,7 @@ export interface ChildAstItem extends AstItem {
3333
parent: ChildAstItem | AstItem;
3434
}
3535

36-
class AstViewerDataProvider implements TreeDataProvider<AstItem> {
36+
class AstViewerDataProvider extends DisposableObject implements TreeDataProvider<AstItem> {
3737

3838
public roots: AstItem[] = [];
3939
public db: DatabaseItem | undefined;
@@ -44,10 +44,13 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem> {
4444
this._onDidChangeTreeData.event;
4545

4646
constructor() {
47-
commandRunner('codeQLAstViewer.gotoCode',
48-
async (item: AstItem) => {
49-
await showLocation(item.fileLocation);
50-
});
47+
super();
48+
this.push(
49+
commandRunner('codeQLAstViewer.gotoCode',
50+
async (item: AstItem) => {
51+
await showLocation(item.fileLocation);
52+
})
53+
);
5154
}
5255

5356
refresh(): void {
@@ -96,23 +99,28 @@ class AstViewerDataProvider implements TreeDataProvider<AstItem> {
9699
}
97100
}
98101

99-
export class AstViewer {
102+
export class AstViewer extends DisposableObject {
100103
private treeView: TreeView<AstItem>;
101104
private treeDataProvider: AstViewerDataProvider;
102105
private currentFile: string | undefined;
103106

104-
constructor(ctx: ExtensionContext) {
107+
constructor() {
108+
super();
109+
105110
this.treeDataProvider = new AstViewerDataProvider();
106111
this.treeView = window.createTreeView('codeQLAstViewer', {
107112
treeDataProvider: this.treeDataProvider,
108113
showCollapseAll: true
109114
});
110115

111-
commandRunner('codeQLAstViewer.clear', async () => {
112-
this.clear();
113-
});
114-
115-
ctx.subscriptions.push(window.onDidChangeTextEditorSelection(this.updateTreeSelection, this));
116+
this.push(this.treeView);
117+
this.push(this.treeDataProvider);
118+
this.push(
119+
commandRunner('codeQLAstViewer.clear', async () => {
120+
this.clear();
121+
})
122+
);
123+
this.push(window.onDidChangeTextEditorSelection(this.updateTreeSelection, this));
116124
}
117125

118126
updateRoots(roots: AstItem[], db: DatabaseItem, fileName: string) {

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { DisposableObject } from './vscode-utils/disposable-object';
33
import {
44
Event,
55
EventEmitter,
6-
ExtensionContext,
76
ProviderResult,
87
TreeDataProvider,
98
TreeItem,
@@ -83,8 +82,8 @@ class DatabaseTreeDataProvider extends DisposableObject
8382
private currentDatabaseItem: DatabaseItem | undefined;
8483

8584
constructor(
86-
private ctx: ExtensionContext,
87-
private databaseManager: DatabaseManager
85+
private databaseManager: DatabaseManager,
86+
private readonly extensionPath: string
8887
) {
8988
super();
9089

@@ -131,12 +130,12 @@ class DatabaseTreeDataProvider extends DisposableObject
131130
const item = new TreeItem(element.name);
132131
if (element === this.currentDatabaseItem) {
133132
item.iconPath = joinThemableIconPath(
134-
this.ctx.extensionPath,
133+
this.extensionPath,
135134
SELECTED_DATABASE_ICON
136135
);
137136
} else if (element.error !== undefined) {
138137
item.iconPath = joinThemableIconPath(
139-
this.ctx.extensionPath,
138+
this.extensionPath,
140139
INVALID_DATABASE_ICON
141140
);
142141
}
@@ -213,16 +212,16 @@ export class DatabaseUI extends DisposableObject {
213212
private treeDataProvider: DatabaseTreeDataProvider;
214213

215214
public constructor(
216-
ctx: ExtensionContext,
217215
private cliserver: cli.CodeQLCliServer,
218216
private databaseManager: DatabaseManager,
219217
private readonly queryServer: qsClient.QueryServerClient | undefined,
220-
private readonly storagePath: string
218+
private readonly storagePath: string,
219+
readonly extensionPath: string
221220
) {
222221
super();
223222

224223
this.treeDataProvider = this.push(
225-
new DatabaseTreeDataProvider(ctx, databaseManager)
224+
new DatabaseTreeDataProvider(databaseManager, extensionPath)
226225
);
227226
this.push(
228227
window.createTreeView('codeQLDatabases', {
@@ -232,7 +231,7 @@ export class DatabaseUI extends DisposableObject {
232231
);
233232

234233
logger.log('Registering database panel commands.');
235-
ctx.subscriptions.push(
234+
this.push(
236235
commandRunnerWithProgress(
237236
'codeQL.setCurrentDatabase',
238237
this.handleSetCurrentDatabase,
@@ -241,7 +240,7 @@ export class DatabaseUI extends DisposableObject {
241240
}
242241
)
243242
);
244-
ctx.subscriptions.push(
243+
this.push(
245244
commandRunnerWithProgress(
246245
'codeQL.upgradeCurrentDatabase',
247246
this.handleUpgradeCurrentDatabase,
@@ -251,7 +250,7 @@ export class DatabaseUI extends DisposableObject {
251250
}
252251
)
253252
);
254-
ctx.subscriptions.push(
253+
this.push(
255254
commandRunnerWithProgress(
256255
'codeQL.clearCache',
257256
this.handleClearCache,
@@ -260,7 +259,7 @@ export class DatabaseUI extends DisposableObject {
260259
})
261260
);
262261

263-
ctx.subscriptions.push(
262+
this.push(
264263
commandRunnerWithProgress(
265264
'codeQLDatabases.chooseDatabaseFolder',
266265
this.handleChooseDatabaseFolder,
@@ -269,7 +268,7 @@ export class DatabaseUI extends DisposableObject {
269268
}
270269
)
271270
);
272-
ctx.subscriptions.push(
271+
this.push(
273272
commandRunnerWithProgress(
274273
'codeQLDatabases.chooseDatabaseArchive',
275274
this.handleChooseDatabaseArchive,
@@ -278,7 +277,7 @@ export class DatabaseUI extends DisposableObject {
278277
}
279278
)
280279
);
281-
ctx.subscriptions.push(
280+
this.push(
282281
commandRunnerWithProgress(
283282
'codeQLDatabases.chooseDatabaseInternet',
284283
this.handleChooseDatabaseInternet,
@@ -287,39 +286,39 @@ export class DatabaseUI extends DisposableObject {
287286
}
288287
)
289288
);
290-
ctx.subscriptions.push(
289+
this.push(
291290
commandRunnerWithProgress(
292291
'codeQLDatabases.chooseDatabaseLgtm',
293292
this.handleChooseDatabaseLgtm,
294293
{
295294
title: 'Adding database from LGTM',
296295
})
297296
);
298-
ctx.subscriptions.push(
297+
this.push(
299298
commandRunner(
300299
'codeQLDatabases.setCurrentDatabase',
301300
this.handleMakeCurrentDatabase
302301
)
303302
);
304-
ctx.subscriptions.push(
303+
this.push(
305304
commandRunner(
306305
'codeQLDatabases.sortByName',
307306
this.handleSortByName
308307
)
309308
);
310-
ctx.subscriptions.push(
309+
this.push(
311310
commandRunner(
312311
'codeQLDatabases.sortByDateAdded',
313312
this.handleSortByDateAdded
314313
)
315314
);
316-
ctx.subscriptions.push(
315+
this.push(
317316
commandRunner(
318317
'codeQLDatabases.removeDatabase',
319318
this.handleRemoveDatabase
320319
)
321320
);
322-
ctx.subscriptions.push(
321+
this.push(
323322
commandRunnerWithProgress(
324323
'codeQLDatabases.upgradeDatabase',
325324
this.handleUpgradeDatabase,
@@ -329,13 +328,13 @@ export class DatabaseUI extends DisposableObject {
329328
}
330329
)
331330
);
332-
ctx.subscriptions.push(
331+
this.push(
333332
commandRunner(
334333
'codeQLDatabases.renameDatabase',
335334
this.handleRenameDatabase
336335
)
337336
);
338-
ctx.subscriptions.push(
337+
this.push(
339338
commandRunner(
340339
'codeQLDatabases.openDatabaseFolder',
341340
this.handleOpenFolder

extensions/ql-vscode/src/extension.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,11 +338,11 @@ async function activateWithInstalledDistribution(
338338
ctx.subscriptions.push(dbm);
339339
logger.log('Initializing database panel.');
340340
const databaseUI = new DatabaseUI(
341-
ctx,
342341
cliServer,
343342
dbm,
344343
qs,
345-
getContextStoragePath(ctx)
344+
getContextStoragePath(ctx),
345+
ctx.extensionPath
346346
);
347347
ctx.subscriptions.push(databaseUI);
348348

@@ -352,13 +352,14 @@ async function activateWithInstalledDistribution(
352352
showResultsForCompletedQuery(item, WebviewReveal.Forced);
353353

354354
const qhm = new QueryHistoryManager(
355-
ctx,
356355
qs,
356+
ctx.extensionPath,
357357
queryHistoryConfigurationListener,
358358
showResults,
359359
async (from: CompletedQuery, to: CompletedQuery) =>
360360
showResultsForComparison(from, to),
361361
);
362+
ctx.subscriptions.push(qhm);
362363
logger.log('Initializing results panel interface.');
363364
const intm = new InterfaceManager(ctx, dbm, cliServer, queryServerLogger);
364365
ctx.subscriptions.push(intm);
@@ -621,7 +622,8 @@ async function activateWithInstalledDistribution(
621622
new TemplateQueryReferenceProvider(cliServer, qs, dbm)
622623
);
623624

624-
const astViewer = new AstViewer(ctx);
625+
const astViewer = new AstViewer();
626+
ctx.subscriptions.push(astViewer);
625627
ctx.subscriptions.push(helpers.commandRunnerWithProgress('codeQL.viewAst', async (
626628
progress: helpers.ProgressCallback,
627629
token: CancellationToken

0 commit comments

Comments
 (0)