diff --git a/packages/langium-vscode/package.json b/packages/langium-vscode/package.json index 77465c07f..3c81903c2 100644 --- a/packages/langium-vscode/package.json +++ b/packages/langium-vscode/package.json @@ -73,7 +73,8 @@ } }, "activationEvents": [ - "onLanguage:langium" + "onLanguage:langium", + "onWebviewPanel:railroadDiagram" ], "vsce": { "dependencies": false diff --git a/packages/langium-vscode/src/language-server/railroad-handler.ts b/packages/langium-vscode/src/language-server/railroad-handler.ts index c01b83f63..931ba74e5 100644 --- a/packages/langium-vscode/src/language-server/railroad-handler.ts +++ b/packages/langium-vscode/src/language-server/railroad-handler.ts @@ -29,7 +29,11 @@ export function registerRailroadConnectionHandler(connection: Connection, servic return undefined; } const generatedRailroadHtml = createGrammarDiagramHtml(document.parseResult.value as Grammar, { - javascript: `const vscode = acquireVsCodeApi(); vscode.setState(${JSON.stringify(uri)});` + // Setting the state to the current uri allows us to open the webview on vscode restart + javascript: ` + const vscode = acquireVsCodeApi(); + vscode.setState(${JSON.stringify(uri)}); + ` }); return generatedRailroadHtml; } catch { diff --git a/packages/langium-vscode/src/railroad-webview.ts b/packages/langium-vscode/src/railroad-webview.ts index b5a243e9f..ff2ae2692 100644 --- a/packages/langium-vscode/src/railroad-webview.ts +++ b/packages/langium-vscode/src/railroad-webview.ts @@ -35,6 +35,9 @@ class RailroadDiagramSerializer implements vscode.WebviewPanelSerializer { if (typeof state === 'string') { const uri = vscode.Uri.parse(state); RailroadDiagramPanel.revive(webviewPanel, this.client, uri); + } else { + // Invalid state, just close the webview + webviewPanel.dispose(); } return Promise.resolve(); } @@ -75,6 +78,9 @@ export class RailroadDiagramPanel implements vscode.Disposable { this.uri = uri.toString(); this.client = client; this.panel = panel; + this.panel.webview.options = { + enableScripts: true + }; vscode.window.onDidChangeActiveTextEditor(e => { if (e?.document.uri.path.endsWith('.langium')) { this.update(e.document.uri.toString());