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

Reopen new diagram widget on start for current active editor. #100

Merged
merged 4 commits into from
Jul 25, 2022
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
21 changes: 17 additions & 4 deletions applications/klighd-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<KLighDExtension | undefined>()
export const klighdExtensionCreated: vscode.Event<KLighDExtension | undefined> = klighdExtensionCreatedEmitter.event


// this method is called when your extension is activated
export function activate(context: vscode.ExtensionContext): void {
const extensionMap: Map<string, KLighDExtension> = new Map();
Expand All @@ -51,11 +57,18 @@ 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,
storageService,
});
klighdExtensionCreatedEmitter.fire(extension)
// Handle notifications that are KLighD specific extensions of the LSP for this LSClient.
new LspHandler(client);

Expand Down
3 changes: 2 additions & 1 deletion applications/klighd-vscode/src/klighd-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -219,6 +219,7 @@ export class KLighDExtension extends SprottyLspVscodeExtension {
this.singleton = webView;
}
}
this.storageService.setItem('diagramOpen', true);
}
})
);
Expand Down
41 changes: 41 additions & 0 deletions applications/klighd-vscode/src/klighd-webview-reopener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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";
import { StorageService } from "./storage/storage-service";

export class KlighdWebviewReopener {

private readonly storage: StorageService

constructor(storage: StorageService) {
this.storage = storage
}

onExtensionCreated(): void {
const diagramWasOpen = this.storage.getItem('diagramOpen')
if (diagramWasOpen == undefined || diagramWasOpen) {
const uri = window.activeTextEditor?.document.fileName
if (uri) {
commands.executeCommand(command.diagramOpen, Uri.file(uri))
}
}

}

}
10 changes: 9 additions & 1 deletion applications/klighd-vscode/src/klighd-webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
},
{
Expand All @@ -125,4 +126,11 @@ export class KLighDWebview extends SprottyLspWebview {
this.initializeWebview(diagramPanel.webview, title);
return diagramPanel;
}

protected async connect(): Promise<void> {
super.connect();
this.disposables.push(this.diagramPanel.onDidDispose(() => {
(this.extension as KLighDExtension).storageService.setItem('diagramOpen', false)
}));
}
}
12 changes: 12 additions & 0 deletions applications/klighd-vscode/src/storage/storage-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ export class StorageService {
return this.memento.get<Record<string, any>>(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<string, any>) {
this.memento.update(StorageService.key, data);
}
Expand Down