I'll help you create a VS Code extension to preview ETRICE ROOM files. ETRICE (Eclipse Training, Research, and Industrial Cooperation Environment) uses ROOM (Real-Time Object-Oriented Modeling) for actor-based modeling.
Here's a complete implementation:
Project Structure
etrice-room-viewer/
├── package.json
├── src/
│ ├── extension.ts
│ ├── roomParser.ts
│ ├── webview/
│ │ ├── index.html
│ │ ├── script.js
│ │ └── style.css
│ └── types/
│ └── room.ts
├── media/
│ └── icons/
└── README.md
1. package.json
{
"name": "etrice-room-viewer",
"displayName": "ETRICE ROOM Viewer",
"description": "Preview ETRICE ROOM files as behavior and structure diagrams",
"version": "0.1.0",
"engines": {
"vscode": "^1.74.0"
},
"categories": ["Visualization", "Other"],
"activationEvents": [
"onLanguage:room"
],
"main": "./out/extension.js",
"contributes": {
"languages": [
{
"id": "room",
"aliases": ["ROOM", "room"],
"extensions": [".room"],
"configuration": "./language-configuration.json"
}
],
"commands": [
{
"command": "etrice.openPreview",
"title": "Open ROOM Preview",
"icon": "$(preview)"
},
{
"command": "etrice.showStructure",
"title": "Show Structure View"
},
{
"command": "etrice.showBehavior",
"title": "Show Behavior View"
}
],
"menus": {
"editor/title": [
{
"command": "etrice.openPreview",
"when": "resourceExtname == .room",
"group": "navigation"
}
],
"explorer/context": [
{
"command": "etrice.openPreview",
"when": "resourceExtname == .room"
}
]
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./"
},
"devDependencies": {
"@types/vscode": "^1.74.0",
"@types/node": "16.x",
"typescript": "^4.9.4"
},
"dependencies": {
"d3": "^7.8.5"
}
}
2. src/types/room.ts
export interface RoomModel {
modelName: string;
imports: string[];
actorClasses: ActorClass[];
protocolClasses: ProtocolClass[];
dataClasses: DataClass[];
}
export interface ActorClass {
name: string;
base?: string;
interfaces: Interface[];
structure: Structure;
behavior?: Behavior;
}
export interface Interface {
name: string;
protocol: string;
conjugated: boolean;
multiplicity?: number;
}
export interface Structure {
actors: ActorRef[];
bindings: Binding[];
layerConnections: LayerConnection[];
}
export interface ActorRef {
name: string;
type: string;
multiplicity?: number;
}
export interface Binding {
endpoint1: string;
endpoint2: string;
}
export interface LayerConnection {
from: string;
to: string;
satisfied: string;
}
export interface Behavior {
stateMachine: StateMachine;
}
export interface StateMachine {
states: State[];
transitions: Transition[];
initialState: string;
}
export interface State {
name: string;
type: 'simple' | 'composite';
entry?: string;
exit?: string;
substates?: State[];
}
export interface Transition {
from: string;
to: string;
trigger?: string;
guard?: string;
action?: string;
}
export interface ProtocolClass {
name: string;
incoming: Message[];
outgoing: Message[];
}
export interface Message {
name: string;
data?: string;
}
export interface DataClass {
name: string;
attributes: Attribute[];
operations: Operation[];
}
export interface Attribute {
name: string;
type: string;
}
export interface Operation {
name: string;
returnType: string;
parameters: Parameter[];
}
export interface Parameter {
name: string;
type: string;
}
3. src/roomParser.ts
import { RoomModel, ActorClass, ProtocolClass, DataClass, Structure, Behavior, StateMachine, State, Transition } from './types/room';
export class RoomParser {
parse(content: string): RoomModel {
const lines = content.split('\n').map(line => line.trim()).filter(line => line.length > 0);
const model: RoomModel = {
modelName: '',
imports: [],
actorClasses: [],
protocolClasses: [],
dataClasses: []
};
let currentSection = '';
let braceLevel = 0;
let currentActor: Partial<ActorClass> | null = null;
let currentProtocol: Partial<ProtocolClass> | null = null;
let currentStateMachine: Partial<StateMachine> | null = null;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Count braces for nesting
braceLevel += (line.match(/{/g) || []).length;
braceLevel -= (line.match(/}/g) || []).length;
// Parse model name
if (line.startsWith('RoomModel')) {
const match = line.match(/RoomModel\s+(\w+)/);
if (match) {
model.modelName = match[1];
}
}
// Parse imports
else if (line.startsWith('import')) {
const match = line.match(/import\s+(.+?)(?:\s+from\s+".+")?/);
if (match) {
model.imports.push(match[1]);
}
}
// Parse ActorClass
else if (line.includes('ActorClass')) {
const match = line.match(/ActorClass\s+(\w+)(?:\s+extends\s+(\w+))?/);
if (match) {
currentActor = {
name: match[1],
base: match[2],
interfaces: [],
structure: { actors: [], bindings: [], layerConnections: [] }
};
currentSection = 'actor';
}
}
// Parse ProtocolClass
else if (line.includes('ProtocolClass')) {
const match = line.match(/ProtocolClass\s+(\w+)/);
if (match) {
currentProtocol = {
name: match[1],
incoming: [],
outgoing: []
};
currentSection = 'protocol';
}
}
// Parse interfaces within actor
else if (currentSection === 'actor' && line.includes('Interface')) {
const match = line.match(/Interface\s+(\w+)\s*:\s*(\w+)(?:\s+(conjugated))?(?:\s*\[(\d+)\])?/);
if (match && currentActor) {
currentActor.interfaces = currentActor.interfaces || [];
currentActor.interfaces.push({
name: match[1],
protocol: match[2],
conjugated: !!match[3],
multiplicity: match[4] ? parseInt(match[4]) : undefined
});
}
}
// Parse structure section
else if (line.includes('Structure')) {
currentSection = 'structure';
}
// Parse actor references in structure
else if (currentSection === 'structure' && line.includes('ActorRef')) {
const match = line.match(/ActorRef\s+(\w+)\s*:\s*(\w+)(?:\s*\[(\d+)\])?/);
if (match && currentActor) {
currentActor.structure = currentActor.structure || { actors: [], bindings: [], layerConnections: [] };
currentActor.structure.actors.push({
name: match[1],
type: match[2],
multiplicity: match[3] ? parseInt(match[3]) : undefined
});
}
}
// Parse bindings
else if (currentSection === 'structure' && line.includes('Binding')) {
const match = line.match(/Binding\s+(.+?)\s+and\s+(.+)/);
if (match && currentActor) {
currentActor.structure = currentActor.structure || { actors: [], bindings: [], layerConnections: [] };
currentActor.structure.bindings.push({
endpoint1: match[1].trim(),
endpoint2: match[2].trim()
});
}
}
// Parse behavior section
else if (line.includes('Behavior')) {
currentSection = 'behavior';
currentStateMachine = {
states: [],
transitions: [],
initialState: ''
};
}
// Parse states
else if (currentSection === 'behavior' && line.includes('State')) {
const match = line.match(/State\s+(\w+)/);
if (match && currentStateMachine) {
const state: State = {
name: match[1],
type: 'simple'
};
currentStateMachine.states = currentStateMachine.states || [];
currentStateMachine.states.push(state);
if (line.includes('initial')) {
currentStateMachine.initialState = match[1];
}
}
}
// Parse transitions
else if (currentSection === 'behavior' && line.includes('Transition')) {
const match = line.match(/Transition\s+(.+?)\s*->\s*(.+?)(?:\s*\{\s*triggers\s*\{\s*(.+?)\s*\})?/);
if (match && currentStateMachine) {
currentStateMachine.transitions = currentStateMachine.transitions || [];
currentStateMachine.transitions.push({
from: match[1].trim(),
to: match[2].trim(),
trigger: match[3]?.trim()
});
}
}
// End of actor class
if (braceLevel === 0 && currentActor && currentSection === 'actor') {
if (currentStateMachine && currentStateMachine.states && currentStateMachine.states.length > 0) {
currentActor.behavior = { stateMachine: currentStateMachine as StateMachine };
}
model.actorClasses.push(currentActor as ActorClass);
currentActor = null;
currentStateMachine = null;
currentSection = '';
}
// End of protocol class
if (braceLevel === 0 && currentProtocol && currentSection === 'protocol') {
model.protocolClasses.push(currentProtocol as ProtocolClass);
currentProtocol = null;
currentSection = '';
}
}
return model;
}
}
4. src/extension.ts
import * as vscode from 'vscode';
import { RoomParser } from './roomParser';
import { RoomModel } from './types/room';
import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
const provider = new RoomPreviewProvider(context.extensionUri);
context.subscriptions.push(
vscode.window.registerWebviewPanelSerializer('roomPreview', provider)
);
context.subscriptions.push(
vscode.commands.registerCommand('etrice.openPreview', (uri?: vscode.Uri) => {
const resource = uri || vscode.window.activeTextEditor?.document.uri;
if (resource) {
provider.openPreview(resource);
}
})
);
context.subscriptions.push(
vscode.commands.registerCommand('etrice.showStructure', () => {
vscode.commands.executeCommand('etrice.openPreview');
})
);
context.subscriptions.push(
vscode.commands.registerCommand('etrice.showBehavior', () => {
vscode.commands.executeCommand('etrice.openPreview');
})
);
}
class RoomPreviewProvider implements vscode.WebviewPanelSerializer {
private panels = new Map<string, vscode.WebviewPanel>();
private parser = new RoomParser();
constructor(private readonly extensionUri: vscode.Uri) {}
async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: any): Promise<void> {
// Restore the webview panel
this.setupWebviewPanel(webviewPanel, state.resource);
}
openPreview(resource: vscode.Uri) {
const column = vscode.window.activeTextEditor
? vscode.window.activeTextEditor.viewColumn
: undefined;
const panelKey = resource.toString();
if (this.panels.has(panelKey)) {
this.panels.get(panelKey)!.reveal(column);
return;
}
const panel = vscode.window.createWebviewPanel(
'roomPreview',
`ROOM Preview: ${path.basename(resource.fsPath)}`,
column || vscode.ViewColumn.One,
{
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [this.extensionUri]
}
);
this.panels.set(panelKey, panel);
this.setupWebviewPanel(panel, resource);
panel.onDidDispose(() => {
this.panels.delete(panelKey);
});
}
private async setupWebviewPanel(panel: vscode.WebviewPanel, resource: vscode.Uri) {
panel.webview.html = await this.getWebviewContent(panel.webview, resource);
// Update content when the file changes
const changeDocumentSubscription = vscode.workspace.onDidChangeTextDocument(e => {
if (e.document.uri.toString() === resource.toString()) {
this.updateWebview(panel.webview, resource);
}
});
panel.onDidDispose(() => {
changeDocumentSubscription.dispose();
});
}
private async getWebviewContent(webview: vscode.Webview, resource: vscode.Uri): Promise<string> {
const document = await vscode.workspace.openTextDocument(resource);
const model = this.parser.parse(document.getText());
const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this.extensionUri, 'src', 'webview', 'script.js'));
const styleUri = webview.asWebviewUri(vscode.Uri.joinPath(this.extensionUri, 'src', 'webview', 'style.css'));
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ROOM Preview</title>
<link href="${styleUri}" rel="stylesheet">
<script src="https://d3js.org/d3.v7.min.js"></script>
</head>
<body>