Skip to content

Commit 9044d11

Browse files
authored
Merge pull request #2190 from github/koesie10/typed-new-database-panel-commands
Convert new database panel to typed commands
2 parents 5b2093d + 2e9a22e commit 9044d11

File tree

4 files changed

+61
-57
lines changed

4 files changed

+61
-57
lines changed

extensions/ql-vscode/src/common/commands.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CommandManager } from "../packages/commands";
22
import type { Uri } from "vscode";
3+
import type { DbTreeViewItem } from "../databases/ui/db-tree-view-item";
34
import type { QueryHistoryInfo } from "../query-history/query-history-info";
45

56
// A command function matching the signature that VS Code calls when
@@ -9,6 +10,12 @@ export type SelectionCommandFunction<Item> = (
910
multiSelect: Item[],
1011
) => Promise<void>;
1112

13+
// A command function matching the signature that VS Code calls when
14+
// a command on a selection is invoked when canSelectMany is false.
15+
export type SingleSelectionCommandFunction<Item> = (
16+
singleItem: Item,
17+
) => Promise<void>;
18+
1219
/**
1320
* Contains type definitions for all commands used by the extension.
1421
*
@@ -62,8 +69,22 @@ export type VariantAnalysisCommands = {
6269
"codeQL.runVariantAnalysisContextEditor": (uri?: Uri) => Promise<void>;
6370
};
6471

72+
export type DatabasePanelCommands = {
73+
"codeQLVariantAnalysisRepositories.openConfigFile": () => Promise<void>;
74+
"codeQLVariantAnalysisRepositories.addNewDatabase": () => Promise<void>;
75+
"codeQLVariantAnalysisRepositories.addNewList": () => Promise<void>;
76+
"codeQLVariantAnalysisRepositories.setupControllerRepository": () => Promise<void>;
77+
78+
"codeQLVariantAnalysisRepositories.setSelectedItem": SingleSelectionCommandFunction<DbTreeViewItem>;
79+
"codeQLVariantAnalysisRepositories.setSelectedItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
80+
"codeQLVariantAnalysisRepositories.openOnGitHubContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
81+
"codeQLVariantAnalysisRepositories.renameItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
82+
"codeQLVariantAnalysisRepositories.removeItemContextMenu": SingleSelectionCommandFunction<DbTreeViewItem>;
83+
};
84+
6585
export type AllCommands = BaseCommands &
6686
QueryHistoryCommands &
67-
VariantAnalysisCommands;
87+
VariantAnalysisCommands &
88+
DatabasePanelCommands;
6889

6990
export type AppCommandManager = CommandManager<AllCommands>;

extensions/ql-vscode/src/databases/db-module.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { DbConfigStore } from "./config/db-config-store";
66
import { DbManager } from "./db-manager";
77
import { DbPanel } from "./ui/db-panel";
88
import { DbSelectionDecorationProvider } from "./ui/db-selection-decoration-provider";
9+
import { DatabasePanelCommands } from "../common/commands";
910

1011
export class DbModule extends DisposableObject {
1112
public readonly dbManager: DbManager;
1213
private readonly dbConfigStore: DbConfigStore;
14+
private dbPanel: DbPanel | undefined;
1315

1416
private constructor(app: App) {
1517
super();
@@ -26,15 +28,24 @@ export class DbModule extends DisposableObject {
2628
return dbModule;
2729
}
2830

31+
public getCommands(): DatabasePanelCommands {
32+
if (!this.dbPanel) {
33+
throw new Error("Database panel not initialized");
34+
}
35+
36+
return {
37+
...this.dbPanel.getCommands(),
38+
};
39+
}
40+
2941
private async initialize(app: App): Promise<void> {
3042
void extLogger.log("Initializing database module");
3143

3244
await this.dbConfigStore.initialize();
3345

34-
const dbPanel = new DbPanel(this.dbManager, app.credentials);
35-
await dbPanel.initialize();
46+
this.dbPanel = new DbPanel(this.dbManager, app.credentials);
3647

37-
this.push(dbPanel);
48+
this.push(this.dbPanel);
3849
this.push(this.dbConfigStore);
3950

4051
const dbSelectionDecorationProvider = new DbSelectionDecorationProvider();

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

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
window,
88
workspace,
99
} from "vscode";
10-
import { commandRunner, UserCancellationException } from "../../commandRunner";
10+
import { UserCancellationException } from "../../commandRunner";
1111
import {
1212
getNwoFromGitHubUrl,
1313
isValidGitHubNwo,
@@ -32,6 +32,7 @@ import { getGitHubUrl } from "./db-tree-view-item-action";
3232
import { getControllerRepo } from "../../variant-analysis/run-remote-query";
3333
import { getErrorMessage } from "../../pure/helpers-pure";
3434
import { Credentials } from "../../common/authentication";
35+
import { DatabasePanelCommands } from "../../common/commands";
3536

3637
export interface RemoteDatabaseQuickPickItem extends QuickPickItem {
3738
kind: string;
@@ -72,58 +73,28 @@ export class DbPanel extends DisposableObject {
7273
this.push(this.treeView);
7374
}
7475

75-
public async initialize(): Promise<void> {
76-
this.push(
77-
commandRunner("codeQLVariantAnalysisRepositories.openConfigFile", () =>
78-
this.openConfigFile(),
79-
),
80-
);
81-
this.push(
82-
commandRunner("codeQLVariantAnalysisRepositories.addNewDatabase", () =>
83-
this.addNewRemoteDatabase(),
84-
),
85-
);
86-
this.push(
87-
commandRunner("codeQLVariantAnalysisRepositories.addNewList", () =>
88-
this.addNewList(),
89-
),
90-
);
91-
this.push(
92-
commandRunner(
93-
"codeQLVariantAnalysisRepositories.setSelectedItem",
94-
(treeViewItem: DbTreeViewItem) => this.setSelectedItem(treeViewItem),
95-
),
96-
);
97-
this.push(
98-
commandRunner(
99-
"codeQLVariantAnalysisRepositories.setSelectedItemContextMenu",
100-
(treeViewItem: DbTreeViewItem) => this.setSelectedItem(treeViewItem),
101-
),
102-
);
103-
this.push(
104-
commandRunner(
105-
"codeQLVariantAnalysisRepositories.openOnGitHubContextMenu",
106-
(treeViewItem: DbTreeViewItem) => this.openOnGitHub(treeViewItem),
107-
),
108-
);
109-
this.push(
110-
commandRunner(
111-
"codeQLVariantAnalysisRepositories.renameItemContextMenu",
112-
(treeViewItem: DbTreeViewItem) => this.renameItem(treeViewItem),
113-
),
114-
);
115-
this.push(
116-
commandRunner(
117-
"codeQLVariantAnalysisRepositories.removeItemContextMenu",
118-
(treeViewItem: DbTreeViewItem) => this.removeItem(treeViewItem),
119-
),
120-
);
121-
this.push(
122-
commandRunner(
123-
"codeQLVariantAnalysisRepositories.setupControllerRepository",
124-
() => this.setupControllerRepository(),
125-
),
126-
);
76+
public getCommands(): DatabasePanelCommands {
77+
return {
78+
"codeQLVariantAnalysisRepositories.openConfigFile":
79+
this.openConfigFile.bind(this),
80+
"codeQLVariantAnalysisRepositories.addNewDatabase":
81+
this.addNewRemoteDatabase.bind(this),
82+
"codeQLVariantAnalysisRepositories.addNewList":
83+
this.addNewList.bind(this),
84+
"codeQLVariantAnalysisRepositories.setupControllerRepository":
85+
this.setupControllerRepository.bind(this),
86+
87+
"codeQLVariantAnalysisRepositories.setSelectedItem":
88+
this.setSelectedItem.bind(this),
89+
"codeQLVariantAnalysisRepositories.setSelectedItemContextMenu":
90+
this.setSelectedItem.bind(this),
91+
"codeQLVariantAnalysisRepositories.openOnGitHubContextMenu":
92+
this.openOnGitHub.bind(this),
93+
"codeQLVariantAnalysisRepositories.renameItemContextMenu":
94+
this.renameItem.bind(this),
95+
"codeQLVariantAnalysisRepositories.removeItemContextMenu":
96+
this.removeItem.bind(this),
97+
};
12798
}
12899

129100
private async openConfigFile(): Promise<void> {

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ async function activateWithInstalledDistribution(
10961096
...getCommands(),
10971097
...qhm.getCommands(),
10981098
...variantAnalysisManager.getCommands(),
1099+
...dbModule.getCommands(),
10991100
};
11001101

11011102
for (const [commandName, command] of Object.entries(allCommands)) {

0 commit comments

Comments
 (0)