@@ -9,6 +9,8 @@ import { connectToPort, findJavaExecutable, getAvailablePort, killProcess } from
99import { SERVER_JAR , DEBUG_MODE , DEBUG_PORT } from "./constants" ;
1010import { LiquidJavaWebviewProvider } from "./webview/provider" ;
1111import { LJDiagnostic } from "./types" ;
12+ import { createMermaidDiagram } from "./webview/fsm" ;
13+ import { StateMachine } from "./types/fsm" ;
1214
1315let serverProcess : child_process . ChildProcess ;
1416let client : LanguageClient ;
@@ -19,6 +21,7 @@ let statusBarItem: vscode.StatusBarItem;
1921let currentDiagnostics : LJDiagnostic [ ] ;
2022let webviewProvider : LiquidJavaWebviewProvider ;
2123let currentFile : string | undefined ;
24+ let currentStateMachine : StateMachine | undefined ;
2225
2326/**
2427 * Activates the LiquidJava extension
@@ -29,15 +32,11 @@ export async function activate(context: vscode.ExtensionContext) {
2932 initStatusBar ( context ) ;
3033 initCommandPalette ( context ) ;
3134 initWebview ( context ) ;
35+ initFileEvents ( context ) ;
3236 initHover ( ) ;
3337
3438 logger . client . info ( "Activating LiquidJava extension..." ) ;
3539
36- const activeEditor = vscode . window . activeTextEditor ;
37- if ( activeEditor && activeEditor . document . languageId === "java" ) {
38- currentFile = activeEditor . document . uri . fsPath ;
39- webviewProvider ?. sendMessage ( { type : "file" , file : currentFile } ) ;
40- }
4140 await applyItalicOverlay ( ) ;
4241
4342 // find java executable path
@@ -135,15 +134,7 @@ function initWebview(context: vscode.ExtensionContext) {
135134 if ( message . type === "ready" ) {
136135 webviewProvider . sendMessage ( { type : "file" , file : currentFile } ) ;
137136 webviewProvider . sendMessage ( { type : "diagnostics" , diagnostics : currentDiagnostics } ) ;
138- }
139- } )
140- ) ;
141- // listen for active text editor changes
142- context . subscriptions . push (
143- vscode . window . onDidChangeActiveTextEditor ( editor => {
144- if ( editor && editor . document . languageId === "java" ) {
145- currentFile = editor . document . uri . fsPath ;
146- webviewProvider ?. sendMessage ( { type : "file" , file : currentFile } ) ;
137+ if ( currentStateMachine ) webviewProvider . sendMessage ( { type : "fsm" , sm : currentStateMachine } ) ;
147138 }
148139 } )
149140 ) ;
@@ -172,9 +163,39 @@ function initHover() {
172163 } ) ;
173164}
174165
166+
167+ /**
168+ * Initializes file system event listeners
169+ * @param context The extension context
170+ */
171+ function initFileEvents ( context : vscode . ExtensionContext ) {
172+ // listen for active text editor changes
173+ context . subscriptions . push (
174+ vscode . window . onDidChangeActiveTextEditor ( editor => {
175+ if ( ! editor || editor . document . languageId !== "java" ) return ;
176+ handleActiveFileChange ( editor ) ;
177+
178+ } ) ,
179+ vscode . workspace . onDidSaveTextDocument ( document => {
180+ if ( document . uri . scheme !== 'file' || document . languageId !== "java" ) return ;
181+ requestStateMachine ( document )
182+ } )
183+ ) ;
184+ }
185+
186+ /**
187+ * Requests the state machine for the given document from the language server
188+ * @param document The text document
189+ */
190+ async function requestStateMachine ( document : vscode . TextDocument ) {
191+ const sm : StateMachine = await client ?. sendRequest ( "liquidjava/fsm" , { uri : document . uri . toString ( ) } ) ;
192+ webviewProvider ?. sendMessage ( { type : "fsm" , sm } ) ;
193+ currentStateMachine = sm ;
194+ }
195+
175196/**
176197 * Updates the status bar with the current state
177- * @param state The state of the status bar: "loading", "stopped", "passed" or "failed"
198+ * @param state The current state ( "loading", "stopped", "passed", "failed")
178199 */
179200function updateStatusBar ( state : "loading" | "stopped" | "passed" | "failed" ) {
180201 const icons = {
@@ -231,7 +252,7 @@ async function runLanguageServer(context: vscode.ExtensionContext, javaExecutabl
231252/**
232253 * Starts the client and connects it to the language server
233254 * @param context The extension context
234- * @param port The port the server is running on
255+ * @param port The port number the server is running on
235256 */
236257async function runClient ( context : vscode . ExtensionContext , port : number ) {
237258 const serverOptions : ServerOptions = ( ) => {
@@ -270,6 +291,11 @@ async function runClient(context: vscode.ExtensionContext, port: number) {
270291 client . onNotification ( "liquidjava/diagnostics" , ( diagnostics : LJDiagnostic [ ] ) => {
271292 handleLJDiagnostics ( diagnostics ) ;
272293 } ) ;
294+
295+ const editor = vscode . window . activeTextEditor ;
296+ if ( editor && editor . document . languageId === "java" ) {
297+ handleActiveFileChange ( editor ) ;
298+ }
273299 } catch ( e ) {
274300 vscode . window . showErrorMessage ( "LiquidJava failed to initialize: " + e . toString ( ) ) ;
275301 logger . client . error ( "Failed to initialize: " + e . toString ( ) ) ;
@@ -323,7 +349,7 @@ async function stopExtension(reason: string) {
323349
324350/**
325351 * Handles LiquidJava diagnostics received from the language server
326- * @param diagnostics The LiquidJava diagnostics
352+ * @param diagnostics The array of diagnostics received
327353 */
328354function handleLJDiagnostics ( diagnostics : LJDiagnostic [ ] ) {
329355 const containsError = diagnostics . some ( d => d . category === "error" ) ;
@@ -335,3 +361,13 @@ function handleLJDiagnostics(diagnostics: LJDiagnostic[]) {
335361 webviewProvider ?. sendMessage ( { type : "diagnostics" , diagnostics } ) ;
336362 currentDiagnostics = diagnostics ;
337363}
364+
365+ /**
366+ * Handles active file change events
367+ * @param editor The active text editor
368+ */
369+ function handleActiveFileChange ( editor : vscode . TextEditor ) {
370+ currentFile = editor . document . uri . fsPath ;
371+ webviewProvider ?. sendMessage ( { type : "file" , file : currentFile } ) ;
372+ requestStateMachine ( editor . document ) ;
373+ }
0 commit comments