From be14fa75ae50c7debaf264b3974ca065bb13fcba Mon Sep 17 00:00:00 2001 From: Soeren Domroes Date: Wed, 20 Jul 2022 15:51:41 +0200 Subject: [PATCH 1/4] Reopen new diagram widget on start for current active editor. --- applications/klighd-vscode/src/extension.ts | 23 +++++++++++--- .../src/klighd-webview-reopener.ts | 31 +++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 applications/klighd-vscode/src/klighd-webview-reopener.ts diff --git a/applications/klighd-vscode/src/extension.ts b/applications/klighd-vscode/src/extension.ts index 0c75b089..4ad53b5b 100644 --- a/applications/klighd-vscode/src/extension.ts +++ b/applications/klighd-vscode/src/extension.ts @@ -3,7 +3,7 @@ * * http://rtsys.informatik.uni-kiel.de/kieler * - * Copyright 2021 by + * Copyright 2021-2022 by * + Kiel University * + Department of Computer Science * + Real-Time and Embedded Systems Group @@ -14,21 +14,27 @@ * * SPDX-License-Identifier: EPL-2.0 */ -import "reflect-metadata"; import { nanoid } from "nanoid/non-secure"; +import "reflect-metadata"; +import { Action, isAction } from "sprotty-protocol"; import * as vscode from "vscode"; import { CommonLanguageClient } from "vscode-languageclient"; import { command } from "./constants"; import { ActionHandlerCallback, KLighDExtension } from "./klighd-extension"; +import { KlighdWebviewReopener } from "./klighd-webview-reopener"; import { LspHandler } from "./lsp-handler"; -import { StorageService } from "./storage/storage-service"; import { ReportChangeMessage } from "./storage/messages"; -import { Action, isAction } from "sprotty-protocol"; +import { StorageService } from "./storage/storage-service"; // potential exports for other extensions to improve their dev experience // Currently, this only includes our command string. Requires this extension to be published as a package. export { command }; + +export const klighdExtensionCreatedEmitter = new vscode.EventEmitter() +export const klighdExtensionCreated: vscode.Event = klighdExtensionCreatedEmitter.event + + // this method is called when your extension is activated export function activate(context: vscode.ExtensionContext): void { const extensionMap: Map = new Map(); @@ -56,6 +62,7 @@ export function activate(context: vscode.ExtensionContext): void { supportedFileEnding: fileEndings, storageService, }); + klighdExtensionCreatedEmitter.fire(extension) // Handle notifications that are KLighD specific extensions of the LSP for this LSClient. new LspHandler(client); @@ -136,7 +143,13 @@ export function activate(context: vscode.ExtensionContext): void { extension.webviews.forEach((webview) => webview.dispatch(action)); }) - ); + ); + + // And make sure we register a serializer for our webview type + const reopener = new KlighdWebviewReopener() + context.subscriptions.push( + klighdExtensionCreated(() => reopener.onExtensionCreated()) + ) } function isLanguageClient(client: unknown): client is CommonLanguageClient { diff --git a/applications/klighd-vscode/src/klighd-webview-reopener.ts b/applications/klighd-vscode/src/klighd-webview-reopener.ts new file mode 100644 index 00000000..666a27b2 --- /dev/null +++ b/applications/klighd-vscode/src/klighd-webview-reopener.ts @@ -0,0 +1,31 @@ +/* + * KIELER - Kiel Integrated Environment for Layout Eclipse RichClient + * + * http://rtsys.informatik.uni-kiel.de/kieler + * + * Copyright 2022 by + * + Kiel University + * + Department of Computer Science + * + Real-Time and Embedded Systems Group + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + */ + +import { commands, Uri, window } from "vscode"; +import { command } from "./constants"; + + +export class KlighdWebviewReopener { + + onExtensionCreated(): void { + const uri = window.activeTextEditor?.document.fileName + if (uri) { + commands.executeCommand(command.diagramOpen, Uri.file(uri)) + } + } + +} \ No newline at end of file From 101b047e58d87c9e2419d8d100022e76ed6fd1b8 Mon Sep 17 00:00:00 2001 From: Soeren Domroes Date: Fri, 22 Jul 2022 15:20:22 +0200 Subject: [PATCH 2/4] Diagram is persited and reopens in correct editor group. I assume that the second editor group is the one of the diagram, which allows it to be placed below or beside. --- applications/klighd-vscode/src/extension.ts | 15 ++++++++------- .../klighd-vscode/src/klighd-extension.ts | 3 ++- .../src/klighd-webview-reopener.ts | 18 ++++++++++++++---- .../klighd-vscode/src/klighd-webview.ts | 10 +++++++++- .../src/storage/storage-service.ts | 12 ++++++++++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/applications/klighd-vscode/src/extension.ts b/applications/klighd-vscode/src/extension.ts index 4ad53b5b..cf5880e1 100644 --- a/applications/klighd-vscode/src/extension.ts +++ b/applications/klighd-vscode/src/extension.ts @@ -42,6 +42,7 @@ export function activate(context: vscode.ExtensionContext): void { // This extension should persist data in workspace state, so it is different for // each project a user opens. To change this, assign another Memento to this constant. const mementoForPersistence = context.workspaceState; + // const storage = new LocalStorage() // Command provided for other extensions to register the LS used to generate diagrams with KLighD. context.subscriptions.push( @@ -57,6 +58,12 @@ export function activate(context: vscode.ExtensionContext): void { try { const storageService = new StorageService(mementoForPersistence, client); + + // And make sure we register a serializer for our webview type + const reopener = new KlighdWebviewReopener(storageService) + context.subscriptions.push( + klighdExtensionCreated(() => reopener.onExtensionCreated()) + ) const extension = new KLighDExtension(context, { lsClient: client, supportedFileEnding: fileEndings, @@ -143,13 +150,7 @@ export function activate(context: vscode.ExtensionContext): void { extension.webviews.forEach((webview) => webview.dispatch(action)); }) - ); - - // And make sure we register a serializer for our webview type - const reopener = new KlighdWebviewReopener() - context.subscriptions.push( - klighdExtensionCreated(() => reopener.onExtensionCreated()) - ) + ); } function isLanguageClient(client: unknown): client is CommonLanguageClient { diff --git a/applications/klighd-vscode/src/klighd-extension.ts b/applications/klighd-vscode/src/klighd-extension.ts index 3c5fc250..0d3bcfa3 100644 --- a/applications/klighd-vscode/src/klighd-extension.ts +++ b/applications/klighd-vscode/src/klighd-extension.ts @@ -73,7 +73,7 @@ export class KLighDExtension extends SprottyLspVscodeExtension { private supportedFileEndings: string[]; // This service is required here, so it can be hooked into created webviews. - private storageService: StorageService; + readonly storageService: StorageService; /** * Stored action handlers that where registered by another extension. @@ -219,6 +219,7 @@ export class KLighDExtension extends SprottyLspVscodeExtension { this.singleton = webView; } } + this.storageService.setItem('diagramOpen', true); } }) ); diff --git a/applications/klighd-vscode/src/klighd-webview-reopener.ts b/applications/klighd-vscode/src/klighd-webview-reopener.ts index 666a27b2..01610bc1 100644 --- a/applications/klighd-vscode/src/klighd-webview-reopener.ts +++ b/applications/klighd-vscode/src/klighd-webview-reopener.ts @@ -17,15 +17,25 @@ import { commands, Uri, window } from "vscode"; import { command } from "./constants"; - +import { StorageService } from "./storage/storage-service"; export class KlighdWebviewReopener { + private readonly storage: StorageService + + constructor(storage: StorageService) { + this.storage = storage + } + onExtensionCreated(): void { - const uri = window.activeTextEditor?.document.fileName - if (uri) { - commands.executeCommand(command.diagramOpen, Uri.file(uri)) + const diagramWasOpen = this.storage.getItem('diagramOpen') + if (diagramWasOpen) { + const uri = window.activeTextEditor?.document.fileName + if (uri) { + commands.executeCommand(command.diagramOpen, Uri.file(uri)) + } } + } } \ No newline at end of file diff --git a/applications/klighd-vscode/src/klighd-webview.ts b/applications/klighd-vscode/src/klighd-webview.ts index defb000f..abda0ee8 100644 --- a/applications/klighd-vscode/src/klighd-webview.ts +++ b/applications/klighd-vscode/src/klighd-webview.ts @@ -19,6 +19,7 @@ import { SprottyDiagramIdentifier, SprottyWebviewOptions } from "sprotty-vscode" import { SprottyLspWebview } from "sprotty-vscode/lib/lsp"; import { commands, ViewColumn, WebviewPanel, window } from "vscode"; import { contextKeys } from "./constants"; +import { KLighDExtension } from "./klighd-extension"; /** * Extends the SprottyLspWebview to communicate user preferences to the container, @@ -114,7 +115,7 @@ export class KLighDWebview extends SprottyLspWebview { this.diagramIdentifier.diagramType || 'diagram', title, { - viewColumn: ViewColumn.Beside, + viewColumn: ViewColumn.Two, preserveFocus: true // The original editor remains focused. }, { @@ -125,4 +126,11 @@ export class KLighDWebview extends SprottyLspWebview { this.initializeWebview(diagramPanel.webview, title); return diagramPanel; } + + protected async connect(): Promise { + super.connect(); + this.disposables.push(this.diagramPanel.onDidDispose(() => { + (this.extension as KLighDExtension).storageService.setItem('diagramOpen', false) + })); + } } diff --git a/applications/klighd-vscode/src/storage/storage-service.ts b/applications/klighd-vscode/src/storage/storage-service.ts index 779148a6..df67508f 100644 --- a/applications/klighd-vscode/src/storage/storage-service.ts +++ b/applications/klighd-vscode/src/storage/storage-service.ts @@ -61,6 +61,18 @@ export class StorageService { return this.memento.get>(StorageService.key) ?? {}; } + setItem(key: string, value: any): void { + const data = this.getData(); + data[key] = value; + + this.updateData(data); + } + + getItem(key: string): any { + const data = this.getData(); + return data[key] + } + private updateData(data: Record) { this.memento.update(StorageService.key, data); } From 147aecad013e0f3c41ffb5ada427e12b9f42c005 Mon Sep 17 00:00:00 2001 From: Soeren Domroes Date: Fri, 22 Jul 2022 15:26:06 +0200 Subject: [PATCH 3/4] Open diagram per default if it was not explizitly closed. --- applications/klighd-vscode/src/klighd-webview-reopener.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applications/klighd-vscode/src/klighd-webview-reopener.ts b/applications/klighd-vscode/src/klighd-webview-reopener.ts index 01610bc1..60a27305 100644 --- a/applications/klighd-vscode/src/klighd-webview-reopener.ts +++ b/applications/klighd-vscode/src/klighd-webview-reopener.ts @@ -29,7 +29,7 @@ export class KlighdWebviewReopener { onExtensionCreated(): void { const diagramWasOpen = this.storage.getItem('diagramOpen') - if (diagramWasOpen) { + if (diagramWasOpen == undefined || diagramWasOpen) { const uri = window.activeTextEditor?.document.fileName if (uri) { commands.executeCommand(command.diagramOpen, Uri.file(uri)) From f3350baeed989e9893b991b126e7dcdc1f2b4884 Mon Sep 17 00:00:00 2001 From: Soeren Domroes Date: Mon, 25 Jul 2022 16:32:25 +0200 Subject: [PATCH 4/4] Removed old debug code. --- applications/klighd-vscode/src/extension.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/applications/klighd-vscode/src/extension.ts b/applications/klighd-vscode/src/extension.ts index cf5880e1..96281cc5 100644 --- a/applications/klighd-vscode/src/extension.ts +++ b/applications/klighd-vscode/src/extension.ts @@ -42,7 +42,6 @@ export function activate(context: vscode.ExtensionContext): void { // This extension should persist data in workspace state, so it is different for // each project a user opens. To change this, assign another Memento to this constant. const mementoForPersistence = context.workspaceState; - // const storage = new LocalStorage() // Command provided for other extensions to register the LS used to generate diagrams with KLighD. context.subscriptions.push(