Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reveal in editor button to method modeling panel #2896

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions extensions/ql-vscode/src/common/interface-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,18 @@ interface SetModeledMethodMessage {
method: ModeledMethod;
}

interface RevealMethodMessage {
t: "revealMethod";
method: Method;
}

export type ToModelEditorMessage =
| SetExtensionPackStateMessage
| SetMethodsMessage
| SetModeledMethodsMessage
| SetModifiedMethodsMessage
| SetInProgressMethodsMessage;
| SetInProgressMethodsMessage
| RevealMethodMessage;

export type FromModelEditorMessage =
| ViewLoadedMsg
Expand All @@ -597,9 +603,15 @@ export type FromModelEditorMessage =
| HideModeledMethodsMessage
| SetModeledMethodMessage;

interface RevealInEditorMessage {
t: "revealInModelEditor";
method: Method;
}

export type FromMethodModelingMessage =
| CommonFromViewMessages
| SetModeledMethodMessage;
| SetModeledMethodMessage
| RevealInEditorMessage;

interface SetMethodMessage {
t: "setMethod";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ import { DisposableObject } from "../../common/disposable-object";
import { MethodModelingViewProvider } from "./method-modeling-view-provider";
import { Method } from "../method";
import { ModelingStore } from "../modeling-store";
import { ModelEditorViewTracker } from "../model-editor-view-tracker";

export class MethodModelingPanel extends DisposableObject {
private readonly provider: MethodModelingViewProvider;

constructor(app: App, modelingStore: ModelingStore) {
constructor(
app: App,
modelingStore: ModelingStore,
editorViewTracker: ModelEditorViewTracker,
) {
super();

this.provider = new MethodModelingViewProvider(app, modelingStore);
this.provider = new MethodModelingViewProvider(
app,
modelingStore,
editorViewTracker,
);
this.push(
window.registerWebviewViewProvider(
MethodModelingViewProvider.viewType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { extLogger } from "../../common/logging/vscode/loggers";
import { App } from "../../common/app";
import { redactableError } from "../../common/errors";
import { Method } from "../method";
import { ModelingStore } from "../modeling-store";
import { DbModelingState, ModelingStore } from "../modeling-store";
import { AbstractWebviewViewProvider } from "../../common/vscode/abstract-webview-view-provider";
import { assertNever } from "../../common/helpers-pure";
import { ModelEditorViewTracker } from "../model-editor-view-tracker";

export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
ToMethodModelingMessage,
Expand All @@ -22,6 +24,7 @@ export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
constructor(
app: App,
private readonly modelingStore: ModelingStore,
private readonly editorViewTracker: ModelEditorViewTracker,
) {
super(app, "method-modeling");
}
Expand Down Expand Up @@ -77,17 +80,43 @@ export class MethodModelingViewProvider extends AbstractWebviewViewProvider<
break;

case "setModeledMethod": {
const activeState = this.modelingStore.getStateForActiveDb();
if (!activeState) {
throw new Error("No active state found in modeling store");
}
const activeState = this.ensureActiveState();

this.modelingStore.updateModeledMethod(
activeState.databaseItem,
msg.method,
);
break;
}
case "revealInModelEditor":
await this.revealInModelEditor(msg.method);

break;
default:
assertNever(msg);
}
}

private async revealInModelEditor(method: Method): Promise<void> {
const activeState = this.ensureActiveState();

const views = this.editorViewTracker.getViews(
activeState.databaseItem.databaseUri.toString(),
);
if (views.length === 0) {
return;
}

await Promise.all(views.map((view) => view.revealMethod(method)));
}

private ensureActiveState(): DbModelingState {
const activeState = this.modelingStore.getStateForActiveDb();
if (!activeState) {
throw new Error("No active state found in modeling store");
}

return activeState;
}

private registerToModelingStoreEvents(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import { setUpPack } from "./model-editor-queries";
import { MethodModelingPanel } from "./method-modeling/method-modeling-panel";
import { ModelingStore } from "./modeling-store";
import { showResolvableLocation } from "../databases/local-databases/locations";
import { ModelEditorViewTracker } from "./model-editor-view-tracker";

const SUPPORTED_LANGUAGES: string[] = ["java", "csharp"];

export class ModelEditorModule extends DisposableObject {
private readonly queryStorageDir: string;
private readonly modelingStore: ModelingStore;
private readonly editorViewTracker: ModelEditorViewTracker<ModelEditorView>;
private readonly methodsUsagePanel: MethodsUsagePanel;
private readonly methodModelingPanel: MethodModelingPanel;

Expand All @@ -39,11 +41,12 @@ export class ModelEditorModule extends DisposableObject {
super();
this.queryStorageDir = join(baseQueryStorageDir, "model-editor-results");
this.modelingStore = new ModelingStore(app);
this.editorViewTracker = new ModelEditorViewTracker();
this.methodsUsagePanel = this.push(
new MethodsUsagePanel(this.modelingStore, cliServer),
);
this.methodModelingPanel = this.push(
new MethodModelingPanel(app, this.modelingStore),
new MethodModelingPanel(app, this.modelingStore, this.editorViewTracker),
);

this.registerToModelingStoreEvents();
Expand Down Expand Up @@ -148,6 +151,7 @@ export class ModelEditorModule extends DisposableObject {
const view = new ModelEditorView(
this.app,
this.modelingStore,
this.editorViewTracker,
this.databaseManager,
this.cliServer,
this.queryRunner,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Method } from "./method";

interface ModelEditorViewInterface {
databaseUri: string;

revealMethod(method: Method): Promise<void>;
}

export class ModelEditorViewTracker<
T extends ModelEditorViewInterface = ModelEditorViewInterface,
> {
private readonly views = new Map<string, T[]>();

constructor() {}

public registerView(view: T): void {
const databaseUri = view.databaseUri;

if (!this.views.has(databaseUri)) {
this.views.set(databaseUri, []);
}

this.views.get(databaseUri)?.push(view);
}

public unregisterView(view: T): void {
const views = this.views.get(view.databaseUri);
if (!views) {
return;
}

const index = views.indexOf(view);
if (index !== -1) {
views.splice(index, 1);
}
}

public getViews(databaseUri: string): T[] {
return this.views.get(databaseUri) ?? [];
}
}
20 changes: 19 additions & 1 deletion extensions/ql-vscode/src/model-editor/model-editor-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { getLanguageDisplayName } from "../common/query-language";
import { AutoModeler } from "./auto-modeler";
import { telemetryListener } from "../common/vscode/telemetry";
import { ModelingStore } from "./modeling-store";
import { ModelEditorViewTracker } from "./model-editor-view-tracker";

export class ModelEditorView extends AbstractWebview<
ToModelEditorMessage,
Expand All @@ -52,6 +53,7 @@ export class ModelEditorView extends AbstractWebview<
public constructor(
protected readonly app: App,
private readonly modelingStore: ModelingStore,
private readonly viewTracker: ModelEditorViewTracker<ModelEditorView>,
private readonly databaseManager: DatabaseManager,
private readonly cliServer: CodeQLCliServer,
private readonly queryRunner: QueryRunner,
Expand All @@ -66,6 +68,8 @@ export class ModelEditorView extends AbstractWebview<
this.modelingStore.initializeStateForDb(databaseItem);
this.registerToModelingStoreEvents();

this.viewTracker.registerView(this);

this.autoModeler = new AutoModeler(
app,
cliServer,
Expand Down Expand Up @@ -181,7 +185,7 @@ export class ModelEditorView extends AbstractWebview<
}

protected onPanelDispose(): void {
// Nothing to do here
this.viewTracker.unregisterView(this);
}

protected async onMessage(msg: FromModelEditorMessage): Promise<void> {
Expand Down Expand Up @@ -338,6 +342,19 @@ export class ModelEditorView extends AbstractWebview<
]);
}

public get databaseUri(): string {
return this.databaseItem.databaseUri.toString();
}

public async revealMethod(method: Method): Promise<void> {
this.panel?.reveal();

await this.postMessage({
t: "revealMethod",
method,
});
}

private async setViewState(): Promise<void> {
const showLlmButton =
this.databaseItem.language === "java" && showLlmGeneration();
Expand Down Expand Up @@ -497,6 +514,7 @@ export class ModelEditorView extends AbstractWebview<
const view = new ModelEditorView(
this.app,
this.modelingStore,
this.viewTracker,
this.databaseManager,
this.cliServer,
this.queryRunner,
Expand Down
2 changes: 1 addition & 1 deletion extensions/ql-vscode/src/model-editor/modeling-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Method, Usage } from "./method";
import { ModeledMethod } from "./modeled-method";
import { INITIAL_HIDE_MODELED_METHODS_VALUE } from "./shared/hide-modeled-methods";

interface DbModelingState {
export interface DbModelingState {
databaseItem: DatabaseItem;
methods: Method[];
hideModeledMethods: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MethodName } from "../model-editor/MethodName";
import { ModeledMethod } from "../../model-editor/modeled-method";
import { MethodModelingInputs } from "./MethodModelingInputs";
import { VSCodeTag } from "@vscode/webview-ui-toolkit/react";
import { ReviewInEditorButton } from "./ReviewInEditorButton";

const Container = styled.div`
padding: 0.3rem;
Expand Down Expand Up @@ -64,6 +65,7 @@ export const MethodModeling = ({
modeledMethod={modeledMethod}
onChange={onChange}
/>
<ReviewInEditorButton method={method} />
</Container>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from "react";
import { useCallback } from "react";
import { styled } from "styled-components";
import { vscode } from "../vscode-api";
import TextButton from "../common/TextButton";
import { Method } from "../../model-editor/method";

const Button = styled(TextButton)`
margin-top: 0.5rem;
`;

type Props = {
method: Method;
};

export const ReviewInEditorButton = ({ method }: Props) => {
const handleClick = useCallback(() => {
vscode.postMessage({
t: "revealInModelEditor",
method,
});
}, [method]);

return <Button onClick={handleClick}>Review in editor</Button>;
};
13 changes: 12 additions & 1 deletion extensions/ql-vscode/src/view/model-editor/LibraryRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from "react";
import { useCallback, useMemo, useState } from "react";
import { useCallback, useEffect, useMemo, useState } from "react";
import { styled } from "styled-components";
import { Method } from "../../model-editor/method";
import { ModeledMethod } from "../../model-editor/modeled-method";
Expand Down Expand Up @@ -76,6 +76,7 @@ export type LibraryRowProps = {
inProgressMethods: InProgressMethods;
viewState: ModelEditorViewState;
hideModeledMethods: boolean;
revealedMethodSignature: string | null;
onChange: (modeledMethod: ModeledMethod) => void;
onSaveModelClick: (
methods: Method[],
Expand All @@ -100,6 +101,7 @@ export const LibraryRow = ({
inProgressMethods,
viewState,
hideModeledMethods,
revealedMethodSignature,
onChange,
onSaveModelClick,
onGenerateFromLlmClick,
Expand All @@ -117,6 +119,14 @@ export const LibraryRow = ({
setExpanded((oldIsExpanded) => !oldIsExpanded);
}, []);

useEffect(() => {
// If any of the methods in this group is the one that should be revealed, we should expand
// this group so the method can highlight itself.
if (methods.some((m) => m.signature === revealedMethodSignature)) {
setExpanded(true);
}
}, [methods, revealedMethodSignature]);

const handleModelWithAI = useCallback(
async (e: React.MouseEvent) => {
onGenerateFromLlmClick(title, methods, modeledMethods);
Expand Down Expand Up @@ -227,6 +237,7 @@ export const LibraryRow = ({
inProgressMethods={inProgressMethods}
mode={viewState.mode}
hideModeledMethods={hideModeledMethods}
revealedMethodSignature={revealedMethodSignature}
onChange={onChange}
/>
<SectionDivider />
Expand Down
Loading