Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Drakae committed May 8, 2024
1 parent 65580fe commit c7d4a77
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 41 deletions.
47 changes: 26 additions & 21 deletions extension/src-language-server/diagram-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,13 @@
* SPDX-License-Identifier: EPL-2.0
*/

import {
Action,
DiagramServices,
JsonMap,
RequestAction,
RequestModelAction,
ResponseAction
} from "sprotty-protocol";
import { Action, DiagramServices, JsonMap, RequestAction, RequestModelAction, ResponseAction } from "sprotty-protocol";
import { Connection } from "vscode-languageserver";
import { SetSynthesisOptionsAction, UpdateOptionsAction } from "./options/actions";
import { DropDownOption } from "./options/option-models";
import { SnippetDiagramServer } from './snippets/snippet-diagram-server';
import { LanguageSnippet } from './snippets/snippet-model';
import { StpaDiagramSnippets } from './snippets/stpa-snippets';
import { SnippetDiagramServer } from "./snippets/snippet-diagram-server";
import { LanguageSnippet } from "./snippets/snippet-model";
import { StpaDiagramSnippets } from "./snippets/stpa-snippets";
import { GenerateSVGsAction, RequestSvgAction, SvgAction } from "./stpa/actions";
import { StpaSynthesisOptions, filteringUCAsID } from "./stpa/diagram/stpa-synthesis-options";
import {
Expand Down Expand Up @@ -56,8 +49,8 @@ import {
setSystemConstraintGraphOptions,
} from "./stpa/result-report/svg-generator";
import { SynthesisOptions } from "./synthesis-options";
import { StpaServices } from './stpa/stpa-module';
import { FtaServices } from './fta/fta-module';
import { StpaServices } from "./stpa/stpa-module";
import { FtaServices } from "./fta/fta-module";

export class PastaDiagramServer extends SnippetDiagramServer {
protected synthesisOptions: SynthesisOptions | undefined;
Expand All @@ -67,13 +60,25 @@ export class PastaDiagramServer extends SnippetDiagramServer {
constructor(
dispatch: <A extends Action>(action: A) => Promise<void>,
services: DiagramServices,
clientId: string,
clientId: string,
options: JsonMap | undefined,
connection: Connection | undefined,
language: StpaServices | FtaServices,
language: StpaServices | FtaServices
) {
super(dispatch, services, clientId, language.snippets.StpaDiagramSnippets.getSnippets() ?? [], options, connection);
this.stpaSnippets = language.snippets.StpaDiagramSnippets;
super(
dispatch,
services,
clientId,
language.hasOwnProperty("snippets") && (language as StpaServices).snippets.StpaDiagramSnippets.getSnippets()
? (language as StpaServices).snippets.StpaDiagramSnippets.getSnippets()
: [],
options,
connection
);
// only STPAService has snippets
if (language.hasOwnProperty("snippets")) {
this.stpaSnippets = (language as StpaServices).snippets.StpaDiagramSnippets;
}
this.synthesisOptions = language.options.SynthesisOptions;
this.clientId = clientId;
this.connection = connection;
Expand Down Expand Up @@ -105,7 +110,7 @@ export class PastaDiagramServer extends SnippetDiagramServer {
* @returns a snippet for the given {@code text}
*/
protected createSnippetFromString(text: string): LanguageSnippet {
return this.stpaSnippets?.createSnippet(text) ?? {} as LanguageSnippet;
return this.stpaSnippets?.createSnippet(text) ?? ({} as LanguageSnippet);
}

/**
Expand All @@ -120,7 +125,7 @@ export class PastaDiagramServer extends SnippetDiagramServer {
diagramSizes = {};
const setSynthesisOption = {
kind: SetSynthesisOptionsAction.KIND,
options: this.synthesisOptions.getSynthesisOptions().map((option) => option.synthesisOption),
options: this.synthesisOptions.getSynthesisOptions().map(option => option.synthesisOption),
} as SetSynthesisOptionsAction;
// save current option values
saveOptions(this.synthesisOptions);
Expand All @@ -140,7 +145,7 @@ export class PastaDiagramServer extends SnippetDiagramServer {
// filtered uca graph svg
const filteringUcaOption = this.synthesisOptions
.getSynthesisOptions()
.find((option) => option.synthesisOption.id === filteringUCAsID);
.find(option => option.synthesisOption.id === filteringUCAsID);
for (const value of (filteringUcaOption?.synthesisOption as DropDownOption).availableValues) {
setFilteredUcaGraphOptions(this.synthesisOptions, value.id);
await this.createSVG(setSynthesisOption, action.uri, FILTERED_UCA_PATH(value.id));
Expand Down Expand Up @@ -200,7 +205,7 @@ export class PastaDiagramServer extends SnippetDiagramServer {
for (const option of action.options) {
const opt = this.synthesisOptions
.getSynthesisOptions()
.find((synOpt) => synOpt.synthesisOption.id === option.id);
.find(synOpt => synOpt.synthesisOption.id === option.id);
if (opt) {
opt.currentValue = option.currentValue;
opt.synthesisOption.currentValue = option.currentValue;
Expand Down
7 changes: 0 additions & 7 deletions extension/src-language-server/fta/fta-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { Module, PartialLangiumServices } from "langium";
import { LangiumSprottyServices, SprottyDiagramServices } from "langium-sprotty";
import { DefaultElementFilter, ElkFactory, IElementFilter, ILayoutConfigurator } from "sprotty-elk/lib/elk-layout";
import { LayoutEngine } from "../layout-engine";
import { StpaDiagramSnippets } from "../snippets/stpa-snippets";
import { FtaDiagramGenerator } from "./diagram/fta-diagram-generator";
import { FtaLayoutConfigurator } from "./diagram/fta-layout-config";
import { FtaSynthesisOptions } from "./diagram/fta-synthesis-options";
Expand All @@ -45,9 +44,6 @@ export type FtaAddedServices = {
options: {
SynthesisOptions: FtaSynthesisOptions;
};
snippets: {
StpaDiagramSnippets: StpaDiagramSnippets;
};
};

/**
Expand Down Expand Up @@ -87,7 +83,4 @@ export const FtaModule: Module<FtaServices, PartialLangiumServices & SprottyDiag
options: {
SynthesisOptions: () => new FtaSynthesisOptions(),
},
snippets: {
StpaDiagramSnippets: services => new StpaDiagramSnippets(services),
},
};
2 changes: 1 addition & 1 deletion extension/src-webview/snippets/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ModelRenderer } from "sprotty";
import { Action, Bounds, RequestAction, ResponseAction, generateRequestId } from "sprotty-protocol";
import { WebviewSnippet } from "./snippet-models";

/** Sent from the view. */
/** Sent from the view to set the renderer and canvas bounds used for the STPA diagram. */
export interface SendModelRendererAction extends Action {
kind: typeof SendModelRendererAction.KIND;
renderer: ModelRenderer;
Expand Down
10 changes: 9 additions & 1 deletion extension/src-webview/snippets/snippet-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ export class SnippetRegistry extends Registry implements IActionHandlerInitializ
}
}

/**
* Renders the given snippets and sends back.
* @param action The action containing the snippets to render.
*/
private handleRequestWebviewSnippets(action: RequestWebviewSnippetsAction): void {
const snippets = this.snippetRenderer.renderSnippets(action.snippets);
const response: SendWebviewSnippetsAction = {
Expand All @@ -64,8 +68,12 @@ export class SnippetRegistry extends Registry implements IActionHandlerInitializ
});
}

/**
* Sets the renderer and canvas bounds needed to render the snippets properly.
* @param action The action containing the renderer and bounds.
*/
private handleSendModelRenderer(action: SendModelRendererAction): void {
this.snippetRenderer.setRenderer((action as SendModelRendererAction).renderer);
this.snippetRenderer.setModelRenderer((action as SendModelRendererAction).renderer);
this.snippetRenderer.setBounds((action as SendModelRendererAction).bounds);
}
}
13 changes: 8 additions & 5 deletions extension/src-webview/snippets/snippet-renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import { WebviewSnippet } from "./snippet-models";
@injectable()
export class SnippetRenderer {
@inject(TYPES.IModelFactory) protected modelFactory: IModelFactory;
protected renderer: ModelRenderer;
/** Needed to render the snippet graph. */
protected modelRenderer: ModelRenderer;
/** Needed to show labels and edges. */
protected bounds: Bounds;

setRenderer(renderer: ModelRenderer): void {
this.renderer = renderer;
setModelRenderer(renderer: ModelRenderer): void {
this.modelRenderer = renderer;
}

setBounds(bounds: Bounds): void {
Expand All @@ -42,15 +44,16 @@ export class SnippetRenderer {
* Renders all snippets provided by the server.
*/
renderSnippets(snippets: WebviewSnippet[]): VNode[] {
if (snippets.length === 0) return <div></div>;
if (snippets.length === 0) {return <div></div>;}

// labels and edges are only visible if they are within the canvas bounds
for (const snippet of snippets) {
(snippet.graph as SGraph).canvasBounds = { width: this.bounds.width + 20, height: this.bounds.height, x: this.bounds.x, y: this.bounds.y };
}

const res = snippets.map(snippet => {
const graph = this.renderer?.renderElement(this.modelFactory.createRoot(snippet.graph));
// render the snippet graph
const graph = this.modelRenderer?.renderElement(this.modelFactory.createRoot(snippet.graph));
// padding of sidebar content is 16px
const width = ((snippet.graph as SGraph).children[0] as SNode).size.width + 30;
const height = ((snippet.graph as SGraph).children[0] as SNode).size.height + 30;
Expand Down
13 changes: 7 additions & 6 deletions extension/src/diagram-snippets-webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,28 +96,29 @@ export class DiagramSnippetWebview {
* @param message The message received from the webview.
*/
protected async receiveFromWebview(message: any): Promise<void> {
console.log("Received from diagram snippet webview");
if (message.readyMessage) {
this.resolveWebviewReady();
this.sendDiagramIdentifier();

// TODO: guarantee that sprotty webview exist
if (this.extension.clientId) {
// send the snippets saved in the config file to the language server
// send the snippets saved in the config file to the language server,
// which will send the rendered snippets back to the diagram snippets webview
const snippets = vscode.workspace.getConfiguration('pasta.stpa').get('snippets');
const action = { kind: SendDefaultSnippetsAction.KIND, snippets: snippets } as SendDefaultSnippetsAction;
const mes2: ActionMessage = {
const sendSnippetsActionMessage: ActionMessage = {
clientId: this.extension.clientId,
action: action
};
this.extension.languageClient.sendNotification(acceptMessageType, mes2);
this.extension.languageClient.sendNotification(acceptMessageType, sendSnippetsActionMessage);
}
} else if (message.action && this.extension.clientId) {
const mes: ActionMessage = {
// forward the action to the language server
const actionMessage: ActionMessage = {
clientId: this.extension.clientId,
action: message.action
};
this.extension.languageClient.sendNotification(acceptMessageType, mes);
this.extension.languageClient.sendNotification(acceptMessageType, actionMessage);
}
}

Expand Down

0 comments on commit c7d4a77

Please sign in to comment.