Skip to content

Commit

Permalink
adds codelens to view/edit diagram (finos#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
sameersaini authored Sep 11, 2024
1 parent 281adbb commit e49fc7e
Showing 1 changed file with 46 additions and 19 deletions.
65 changes: 46 additions & 19 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
WorkspaceEdit,
EndOfLine,
ProgressLocation,
type WebviewPanel,
} from 'vscode';
import {
type LanguageClient,
Expand Down Expand Up @@ -83,6 +84,7 @@ import {
import { renderDiagramRendererWebView } from './webviews/DiagramWebView';

let client: LegendLanguageClient;
const openedWebViews: Record<string, WebviewPanel> = {};

export function createClient(context: ExtensionContext): LanguageClient {
languages.setLanguageConfiguration(LEGEND_LANGUAGE_ID, {
Expand Down Expand Up @@ -197,6 +199,41 @@ export function createClient(context: ExtensionContext): LanguageClient {
return client;
}

const showDiagramWebView = async (
diagramId: string,
context: ExtensionContext,
): Promise<void> => {
let diagramRendererWebView: WebviewPanel;
if (openedWebViews[diagramId]) {
diagramRendererWebView = openedWebViews[diagramId] as WebviewPanel;
diagramRendererWebView.reveal();
} else {
diagramRendererWebView = window.createWebviewPanel(
DIAGRAM_RENDERER,
`Diagram Editor (${diagramId.split('::').pop()})`,
ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
},
);
diagramRendererWebView.onDidDispose(() => {
delete openedWebViews[diagramId];
});
openedWebViews[diagramId] = diagramRendererWebView;

const entities = await client.entities(new LegendEntitiesRequest([]));
renderDiagramRendererWebView(
diagramRendererWebView,
context,
diagramId,
entities,
workspace.getConfiguration('legend').get('studio.forms.file', ''),
client,
);
}
};

export function registerCommands(context: ExtensionContext): void {
const functionCommand = commands.registerCommand(
LEGEND_CLIENT_COMMAND_ID,
Expand All @@ -213,9 +250,14 @@ export function registerCommands(context: ExtensionContext): void {
break;
}

default: {
case LEGEND_SHOW_DIAGRAM: {
showDiagramWebView(args[2] as string, context);
break;
}

default: {
window.showWarningMessage(`${commandId} command is not supported`);
}
}
},
);
Expand Down Expand Up @@ -261,25 +303,10 @@ export function registerCommands(context: ExtensionContext): void {

const showDiagram = commands.registerCommand(
LEGEND_SHOW_DIAGRAM,
async (...args: unknown[]) => {
const diagramRendererWebView = window.createWebviewPanel(
DIAGRAM_RENDERER,
`Diagram Editor`,
ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
},
);

const entities = await client.entities(new LegendEntitiesRequest([]));
renderDiagramRendererWebView(
diagramRendererWebView,
context,
(...args: unknown[]) => {
showDiagramWebView(
(args[0] as LegendConceptTreeItem).id as string,
entities,
workspace.getConfiguration('legend').get('studio.forms.file', ''),
client,
context,
);
},
);
Expand Down

0 comments on commit e49fc7e

Please sign in to comment.