Skip to content

Commit c77a300

Browse files
committed
Move methods, hideModeledMethods and active editor state to the modeling store
1 parent d33c267 commit c77a300

File tree

7 files changed

+191
-86
lines changed

7 files changed

+191
-86
lines changed

extensions/ql-vscode/src/model-editor/methods-usage/methods-usage-panel.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ import {
77
import { Method, Usage } from "../method";
88
import { DatabaseItem } from "../../databases/local-databases";
99
import { CodeQLCliServer } from "../../codeql-cli/cli";
10+
import { ModelingStore } from "../modeling-store";
1011

1112
export class MethodsUsagePanel extends DisposableObject {
1213
private readonly dataProvider: MethodsUsageDataProvider;
1314
private readonly treeView: TreeView<MethodsUsageTreeViewItem>;
1415

15-
public constructor(cliServer: CodeQLCliServer) {
16+
public constructor(
17+
private readonly modelingStore: ModelingStore,
18+
cliServer: CodeQLCliServer,
19+
) {
1620
super();
1721

1822
this.dataProvider = new MethodsUsageDataProvider(cliServer);
@@ -21,6 +25,8 @@ export class MethodsUsagePanel extends DisposableObject {
2125
treeDataProvider: this.dataProvider,
2226
});
2327
this.push(this.treeView);
28+
29+
this.registerToModelingStoreEvents();
2430
}
2531

2632
public async setState(
@@ -44,4 +50,39 @@ export class MethodsUsagePanel extends DisposableObject {
4450
await this.treeView.reveal(canonicalUsage);
4551
}
4652
}
53+
54+
private registerToModelingStoreEvents(): void {
55+
this.push(
56+
this.modelingStore.onActiveDbChanged(async () => {
57+
await this.handleStateChangeEvent();
58+
}),
59+
);
60+
61+
this.push(
62+
this.modelingStore.onMethodsChanged(async (event) => {
63+
if (event.isActiveDb) {
64+
await this.handleStateChangeEvent();
65+
}
66+
}),
67+
);
68+
69+
this.push(
70+
this.modelingStore.onHideModeledMethodsChanged(async (event) => {
71+
if (event.isActiveDb) {
72+
await this.handleStateChangeEvent();
73+
}
74+
}),
75+
);
76+
}
77+
78+
private async handleStateChangeEvent(): Promise<void> {
79+
const activeState = this.modelingStore.getStateForActiveDb();
80+
if (activeState !== undefined) {
81+
await this.setState(
82+
activeState.methods,
83+
activeState.databaseItem,
84+
activeState.hideModeledMethods,
85+
);
86+
}
87+
}
4788
}

extensions/ql-vscode/src/model-editor/model-editor-module.ts

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ export class ModelEditorModule extends DisposableObject {
2929
private readonly methodsUsagePanel: MethodsUsagePanel;
3030
private readonly methodModelingPanel: MethodModelingPanel;
3131

32-
private mostRecentlyActiveView: ModelEditorView | undefined = undefined;
33-
3432
private constructor(
3533
private readonly app: App,
3634
private readonly databaseManager: DatabaseManager,
@@ -41,24 +39,12 @@ export class ModelEditorModule extends DisposableObject {
4139
super();
4240
this.queryStorageDir = join(baseQueryStorageDir, "model-editor-results");
4341
this.modelingStore = new ModelingStore(app);
44-
this.methodsUsagePanel = this.push(new MethodsUsagePanel(cliServer));
42+
this.methodsUsagePanel = this.push(
43+
new MethodsUsagePanel(this.modelingStore, cliServer),
44+
);
4545
this.methodModelingPanel = this.push(new MethodModelingPanel(app));
4646
}
4747

48-
private handleViewBecameActive(view: ModelEditorView): void {
49-
this.mostRecentlyActiveView = view;
50-
}
51-
52-
private handleViewWasDisposed(view: ModelEditorView): void {
53-
if (this.mostRecentlyActiveView === view) {
54-
this.mostRecentlyActiveView = undefined;
55-
}
56-
}
57-
58-
private isMostRecentlyActiveView(view: ModelEditorView): boolean {
59-
return this.mostRecentlyActiveView === view;
60-
}
61-
6248
public static async initialize(
6349
app: App,
6450
databaseManager: DatabaseManager,
@@ -165,16 +151,15 @@ export class ModelEditorModule extends DisposableObject {
165151
db,
166152
modelFile,
167153
Mode.Application,
168-
this.methodsUsagePanel.setState.bind(this.methodsUsagePanel),
169154
this.showMethod.bind(this),
170-
this.handleViewBecameActive.bind(this),
171-
(view) => {
172-
this.handleViewWasDisposed(view);
173-
void cleanupQueryDir();
174-
},
175-
this.isMostRecentlyActiveView.bind(this),
176155
);
177156

157+
this.modelingStore.onDbClosed(async (dbUri) => {
158+
if (dbUri === db.databaseUri.toString()) {
159+
await cleanupQueryDir();
160+
}
161+
});
162+
178163
this.push(view);
179164
this.push({
180165
dispose(): void {

extensions/ql-vscode/src/model-editor/model-editor-view.ts

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ import { loadModeledMethods, saveModeledMethods } from "./modeled-method-fs";
4141
import { pickExtensionPack } from "./extension-pack-picker";
4242
import { getLanguageDisplayName } from "../common/query-language";
4343
import { AutoModeler } from "./auto-modeler";
44-
import { INITIAL_HIDE_MODELED_METHODS_VALUE } from "./shared/hide-modeled-methods";
4544
import { telemetryListener } from "../common/vscode/telemetry";
4645
import { ModelingStore } from "./modeling-store";
4746

@@ -51,9 +50,6 @@ export class ModelEditorView extends AbstractWebview<
5150
> {
5251
private readonly autoModeler: AutoModeler;
5352

54-
private methods: Method[];
55-
private hideModeledMethods: boolean;
56-
5753
public constructor(
5854
protected readonly app: App,
5955
private readonly modelingStore: ModelingStore,
@@ -65,24 +61,15 @@ export class ModelEditorView extends AbstractWebview<
6561
private readonly databaseItem: DatabaseItem,
6662
private readonly extensionPack: ExtensionPack,
6763
private mode: Mode,
68-
private readonly updateMethodsUsagePanelState: (
69-
methods: Method[],
70-
databaseItem: DatabaseItem,
71-
hideModeledMethods: boolean,
72-
) => Promise<void>,
7364
private readonly showMethod: (
7465
method: Method,
7566
usage: Usage,
7667
) => Promise<void>,
77-
private readonly handleViewBecameActive: (view: ModelEditorView) => void,
78-
private readonly handleViewWasDisposed: (view: ModelEditorView) => void,
79-
private readonly isMostRecentlyActiveView: (
80-
view: ModelEditorView,
81-
) => boolean,
8268
) {
8369
super(app);
8470

8571
this.modelingStore.initializeStateForDb(databaseItem);
72+
this.registerToModelingStoreEvents();
8673

8774
this.autoModeler = new AutoModeler(
8875
app,
@@ -101,8 +88,6 @@ export class ModelEditorView extends AbstractWebview<
10188
await this.postMessage({ t: "addModeledMethods", modeledMethods });
10289
},
10390
);
104-
this.methods = [];
105-
this.hideModeledMethods = INITIAL_HIDE_MODELED_METHODS_VALUE;
10691
}
10792

10893
public async openView() {
@@ -111,20 +96,15 @@ export class ModelEditorView extends AbstractWebview<
11196

11297
panel.onDidChangeViewState(async () => {
11398
if (panel.active) {
114-
this.handleViewBecameActive(this);
115-
await this.updateMethodsUsagePanelState(
116-
this.methods,
117-
this.databaseItem,
118-
this.hideModeledMethods,
119-
);
99+
this.modelingStore.setActiveDb(this.databaseItem);
120100
await this.markModelEditorAsActive();
121101
} else {
122102
await this.updateModelEditorActiveContext();
123103
}
124104
});
125105

126106
panel.onDidDispose(() => {
127-
this.handleViewWasDisposed(this);
107+
this.modelingStore.removeDb(this.databaseItem);
128108
// onDidDispose is called after the tab has been closed,
129109
// so we want to check if there are any others still open.
130110
void this.app.commands.execute(
@@ -313,11 +293,11 @@ export class ModelEditorView extends AbstractWebview<
313293
break;
314294
case "switchMode":
315295
this.mode = msg.mode;
316-
this.methods = [];
296+
this.modelingStore.setMethods(this.databaseItem, []);
317297
await Promise.all([
318298
this.postMessage({
319299
t: "setMethods",
320-
methods: this.methods,
300+
methods: [],
321301
}),
322302
this.setViewState(),
323303
withProgress((progress) => this.loadMethods(progress), {
@@ -328,11 +308,9 @@ export class ModelEditorView extends AbstractWebview<
328308

329309
break;
330310
case "hideModeledMethods":
331-
this.hideModeledMethods = msg.hideModeledMethods;
332-
await this.updateMethodsUsagePanelState(
333-
this.methods,
311+
this.modelingStore.setHideModeledMethods(
334312
this.databaseItem,
335-
this.hideModeledMethods,
313+
msg.hideModeledMethods,
336314
);
337315
void telemetryListener?.sendUIInteraction(
338316
"model-editor-hide-modeled-methods",
@@ -413,19 +391,8 @@ export class ModelEditorView extends AbstractWebview<
413391
if (!queryResult) {
414392
return;
415393
}
416-
this.methods = queryResult;
417394

418-
await this.postMessage({
419-
t: "setMethods",
420-
methods: this.methods,
421-
});
422-
if (this.isMostRecentlyActiveView(this)) {
423-
await this.updateMethodsUsagePanelState(
424-
this.methods,
425-
this.databaseItem,
426-
this.hideModeledMethods,
427-
);
428-
}
395+
this.modelingStore.setMethods(this.databaseItem, queryResult);
429396
} catch (err) {
430397
void showAndLogExceptionWithTelemetry(
431398
this.app.logger,
@@ -540,11 +507,7 @@ export class ModelEditorView extends AbstractWebview<
540507
addedDatabase,
541508
modelFile,
542509
Mode.Framework,
543-
this.updateMethodsUsagePanelState,
544510
this.showMethod,
545-
this.handleViewBecameActive,
546-
this.handleViewWasDisposed,
547-
this.isMostRecentlyActiveView,
548511
);
549512
await view.openView();
550513
});
@@ -622,4 +585,17 @@ export class ModelEditorView extends AbstractWebview<
622585

623586
return addedDatabase;
624587
}
588+
589+
private registerToModelingStoreEvents() {
590+
this.push(
591+
this.modelingStore.onMethodsChanged(async (event) => {
592+
if (event.dbUri === this.databaseItem.databaseUri.toString()) {
593+
await this.postMessage({
594+
t: "setMethods",
595+
methods: event.methods,
596+
});
597+
}
598+
}),
599+
);
600+
}
625601
}

0 commit comments

Comments
 (0)