Creating a VS Code plugin to preview ETRICE ROOM files as behavior/structure views involves several components:
🧩 Overview
You want to:
- Parse
.room files (ETRICE's ROOM modeling language).
- Visualize the structure and behavior of actors using diagrams.
- Use a webview in VS Code for rendering.
📁 Project Structure
code
etrice-room-viewer/
├── package.json
├── src/
│ ├── extension.ts // Main entry point
│ ├── parser.ts // Parses .room files into JSON
│ ├── webview/
│ │ ├── index.html // Webview UI
│ │ ├── style.css // Styling
│ │ └── main.js // D3.js or similar logic for visualization
│ └── models/
│ └── roomModel.ts // TypeScript interfaces for parsed data
├── out/ // Compiled JS goes here
├── media/
│ └── icons/
└── README.md
✅ Step-by-Step Implementation
1. package.json
json
{
"name": "etrice-room-viewer",
"displayName": "ETRICE ROOM Viewer",
"description": "Preview ETRICE ROOM files as behavior and structure diagrams.",
"version": "0.0.1",
"publisher": "your-publisher-name",
"engines": {
"vscode": "^1.70.0"
},
"categories": ["Other", "Visualization"],
"activationEvents": [
"onLanguage:room",
"onCommand:etrice.showRoomStructureView",
"onCommand:etrice.showRoomBehaviorView"
],
"main": "./out/extension",
"contributes": {
"languages": [{
"id": "room",
"extensions": [".room"],
"aliases": ["ROOM", "room"],
"mimetypes": ["text/x-room"]
}],
"commands": [
{
"command": "etrice.showRoomStructureView",
"title": "Show ROOM Structure View"
},
{
"command": "etrice.showRoomBehaviorView",
"title": "Show ROOM Behavior View"
}
]
},
"scripts": {
"vscode:prepublish": "npm run build",
"build": "tsc -p ./",
"watch": "tsc -watch -p ./",
"package": "vsce package"
},
"devDependencies": {
"@types/vscode": "^1.74.0",
"@types/node": "^18.x.x",
"typescript": "^5.0.2",
"vsce": "^2.16.1"
},
"dependencies": {}
}
2. src/models/roomModel.ts
Define TypeScript interfaces that mirror the ROOM structure:
ts
export interface RoomActorClass {
name: string;
base?: string;
interfaces: RoomInterface[];
structure: RoomStructure;
behavior?: RoomBehavior;
}
export interface RoomInterface {
name: string;
protocol: string;
conjugated: boolean;
}
export interface RoomStructure {
actors: RoomActorRef[];
bindings: RoomBinding[];
}
export interface RoomActorRef {
name: string;
type: string;
}
export interface RoomBinding {
from: string;
to: string;
}
export interface RoomBehavior {
stateMachine: RoomStateMachine;
}
export interface RoomStateMachine {
states: RoomState[];
transitions: RoomTransition[];
initialState: string;
}
export interface RoomState {
name: string;
type: 'simple' | 'composite';
}
export interface RoomTransition {
from: string;
to: string;
trigger?: string;
}
3. src/parser.ts
A basic parser to extract ROOM model info from .room text:
ts
import * as vscode from 'vscode';
import { RoomActorClass, RoomStructure, RoomInterface, RoomStateMachine, RoomState, RoomTransition } from './models/roomModel';
export class RoomParser {
static parse(text: string): RoomActorClass[] {
const lines = text.split('\n').map(line => line.trim());
const actors: RoomActorClass[] = [];
let currentActor: Partial<RoomActorClass> | null = null;
for (const line of lines) {
if (line.startsWith('ActorClass')) {
const match = /ActorClass\s+(\w+)(?:\s+extends\s+(\w+))?/.exec(line);
if (match) {
currentActor = {
name: match[1],
base: match[2],
interfaces: [],
structure: { actors: [], bindings: [] },
behavior: undefined
};
}
}
else if (currentActor && line.includes('Interface')) {
const match = /Interface\s+(\w+)\s*:\s*(\w+)(\s+conjugated)?/.exec(line);
if (match) {
currentActor.interfaces?.push({
name: match[1],
protocol: match[2],
conjugated: !!match[3]
});
}
}
else if (currentActor && line.includes('ActorRef')) {
const match = /ActorRef\s+(\w+)\s*:\s*(\w+)/.exec(line);
if (match && currentActor.structure) {
currentActor.structure.actors.push({
name: match[1],
type: match[2]
});
}
}
else if (currentActor && line.includes('Binding')) {
const match = /Binding\s+(\w+\.\w+)\s+and\s+(\w+\.\w+)/.exec(line);
if (match && currentActor.structure) {
currentActor.structure.bindings.push({
from: match[1],
to: match[2]
});
}
}
else if (currentActor && line.includes('Behavior')) {
const sm: RoomStateMachine = {
states: [],
transitions: [],
initialState: ''
};
// This is a placeholder; real parsing would need more depth
sm.states.push({ name: 'Initial', type: 'simple' });
sm.states.push({ name: 'Active', type: 'simple' });
sm.transitions.push({ from: 'Initial', to: 'Active', trigger: 'start' });
sm.initialState = 'Initial';
currentActor.behavior = { stateMachine: sm };
}
else if (line === '}') {
if (currentActor) {
actors.push(currentActor as RoomActorClass);
currentActor = null;
}
}
}
return actors;
}
}
4. src/webview/index.html
Basic HTML layout with placeholders for visualizations:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ROOM Preview</title>
<style>
body { font-family: sans-serif; padding: 1em; background: #f5f5f5; }
svg { width: 100%; height: auto; border: 1px solid #ccc; margin-top: 1em; }
</style>
</head>
<body>
<h2>ROOM Diagram</h2>
<div id="diagram"></div>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="${vscode.Uri.file('${webview.asWebviewUri(webviewResourceRoot)}')}/main.js"></script>
</body>
</html>
5. src/webview/main.js
Simple D3-based visualization example:
js
const vscode = acquireVsCodeApi();
window.addEventListener('message', event => {
const message = event.data;
if (message.command === 'updateData') {
renderStructure(message.data);
}
});
function renderStructure(data) {
d3.select('#diagram').selectAll('*').remove();
const svg = d3.select('#diagram')
.append('svg')
.attr('width', 600)
.attr('height', 400);
// Example: draw nodes for actors
const nodes = data.map((actor, i) => ({ id: actor.name, x: 100 + i * 150, y: 100 }));
const links = [];
data.forEach(actor => {
actor.structure.actors.forEach(subActor => {
links.push({ source: actor.name, target: subActor.name });
});
});
const link = svg.selectAll('.link')
.data(links)
.enter()
.append('line')
.attr('class', 'link')
.attr('stroke', '#333');
const node = svg.selectAll('.node')
.data(nodes)
.enter()
.append('circle')
.attr('class', 'node')
.attr('r', 20)
.attr('cx', d => d.x)
.attr('cy', d => d.y)
.append('title')
.text(d => d.id);
});
6. src/extension.ts
Main extension code that opens a webview and communicates with it:
ts
import * as vscode from 'vscode';
import { RoomParser } from './parser';
import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand('etrice.showRoomStructureView', () => {
showRoomView(context.extensionPath, 'structure');
})
);
context.subscriptions.push(
vscode.commands.registerCommand('etrice.showRoomBehaviorView', () => {
showRoomView(context.extensionPath, 'behavior');
})
);
}
function showRoomView(extensionPath: string, viewType: 'structure' | 'behavior') {
const panel = vscode.window.createWebviewPanel(
'roomViewer',
`ROOM ${viewType === 'structure' ? 'Structure' : 'Behavior'} View`,
vscode.ViewColumn.Beside,
{
enableScripts: true,
localResourceRoots: [vscode.Uri.file(path.join(extensionPath, 'src', 'webview'))]
}
);
const editor = vscode.window.activeTextEditor;
if (!editor || !editor.document.fileName.endsWith('.room')) {
vscode.window.showErrorMessage('Open a .room file first.');
return;
}
const doc = editor.document.getText();
const parsed = RoomParser.parse(doc);
const htmlPath = vscode.Uri.file(path.join(extensionPath, 'src', 'webview', 'index.html'));
const htmlContent = fs.readFileSync(htmlPath.fsPath, 'utf-8');
panel.webview.html = htmlContent.replace('${webviewResourceRoot}', panel.webview.asWebviewUri(
vscode.Uri.file(path.join(extensionPath, 'src', 'webview'))
).toString());
panel.webview.postMessage({
command: 'updateData',
data: parsed
});
}
🛠️ Build & Run
- Install dependencies:
- Compile:
bash
npm run build
-
Press F5 in VS Code to launch the extension in a new window.
-
Open a .room file and use the commands:
code
Ctrl+Shift+P → Show ROOM Structure View
🎯 Future Improvements
- Full ROOM grammar parsing (ANTLR/antlr4ts).
- Interactive diagram editing.
- Integration with ETRICE Eclipse project.
- Add export options (PNG, SVG).
📦 Publishing
When ready:
bash
npm install -g vsce
vsce package
Then upload your .vsix to Visual Studio Marketplace.
Would you like me to generate this entire project as a downloadable ZIP or GitHub repo template?