11import { prepareExecutable } from '../javaServerStarter' ;
2- import { getComputedJavaConfig , getExecutable } from '../extension' ;
2+ import { getComputedJavaConfig , getExecutable , getWorkspacePath } from '../extension' ;
33import * as fs from 'fs' ;
44import * as path from 'path' ;
55import * as vscode from 'vscode' ;
66import { getNonce , getUri } from '../webviewUtils' ;
7- import { InitializeMessage , JVM , SettingChangedMessage } from '../webviewProtocol/toDashboard' ;
8- import { isLombokSupportEnabled } from '../lombokSupport' ;
7+ import { DashboardState , DiagnosticInfo , JVM , UpdateMessage } from '../webviewProtocol/toDashboard' ;
8+ import { isLombokSupportEnabled , Lombok } from '../lombokSupport' ;
9+ import { Commands } from '../commands' ;
10+ import { apiManager } from '../apiManager' ;
911
12+ const currentState : DashboardState = {
13+ } ;
14+
15+ export namespace Dashboard {
16+ export function initialize ( context : vscode . ExtensionContext ) : void {
17+ console . log ( 'registering dashboard webview provider' ) ;
18+ let webview : vscode . Webview ;
19+
20+ context . subscriptions . push ( vscode . window . registerWebviewViewProvider ( 'java.dashboard' , {
21+ resolveWebviewView : async function ( webviewView : vscode . WebviewView , webviewContext : vscode . WebviewViewResolveContext , token : vscode . CancellationToken ) : Promise < void > {
22+ vscode . commands . executeCommand ( 'setContext' , 'java:dashboard' , true ) ;
23+ webview = webviewView . webview ;
24+ webviewView . webview . options = {
25+ enableScripts : true ,
26+ enableCommandUris : true ,
27+ localResourceRoots : [ context . extensionUri ]
28+ } ;
29+
30+ webviewView . webview . html = await getWebviewContent ( webviewView . webview , context ) ;
31+ webviewView . onDidDispose ( ( ) => {
32+ vscode . commands . executeCommand ( 'setContext' , 'java:dashboard' , false ) ;
33+ } ) ;
34+ }
35+ } ) ) ;
36+
37+ context . subscriptions . push ( vscode . commands . registerCommand ( 'java.dashboard.refresh' , async ( ) => {
38+ refreshLSInfo ( webview ) ;
39+ } ) ) ;
40+
41+ context . subscriptions . push ( vscode . commands . registerCommand ( 'java.dashboard.revealFileInOS' , async ( arg : { path : string } ) => {
42+ await vscode . commands . executeCommand ( 'revealFileInOS' , vscode . Uri . file ( arg . path ) ) ;
43+ } ) ) ;
44+
45+ context . subscriptions . push ( vscode . commands . registerCommand ( 'java.dashboard.dumpState' , async ( ) => {
46+ await vscode . workspace . openTextDocument ( {
47+ language : 'json' ,
48+ content : JSON . stringify ( currentState , null , 2 )
49+ } ) ;
50+ } ) ) ;
51+
52+ console . log ( 'registered dashboard webview provider' ) ;
53+ }
54+ }
1055
1156async function getJvms ( ) : Promise < JVM [ ] > {
1257 const config = await getComputedJavaConfig ( ) ;
@@ -19,10 +64,10 @@ async function getJvms(): Promise<JVM[]> {
1964
2065}
2166
22- export function getWebviewContent ( webview : vscode . Webview , extensionUri : vscode . Uri ) {
67+ function getWebviewContent ( webview : vscode . Webview , context : vscode . ExtensionContext ) {
2368 setWebviewMessageListener ( webview ) ;
2469
25- const scriptUri = getUri ( webview , extensionUri , [
70+ const scriptUri = getUri ( webview , context . extensionUri , [
2671 "dist" ,
2772 "dashboard.js" ,
2873 ] ) ;
@@ -45,30 +90,66 @@ export function getWebviewContent(webview: vscode.Webview, extensionUri: vscode.
4590 </html>
4691` ;
4792}
93+ async function refreshLSInfo ( webview : vscode . Webview ) : Promise < void > {
94+ try {
95+ vscode . commands . executeCommand < DiagnosticInfo > ( Commands . EXECUTE_WORKSPACE_COMMAND , Commands . GET_DIAGNOSTIC_INFO ) . then ( info => {
96+ currentState . diagnosticInfo = info ;
97+ const msg : UpdateMessage = {
98+ type : "update" ,
99+ diagnosticInfo : info
100+ } ;
101+ webview . postMessage ( msg ) ;
102+ } ) ;
103+ } catch ( e ) {
104+ console . error ( 'Failed to get diagnostic info' , e ) ;
105+ }
106+ }
107+
108+ function setWebviewMessageListener ( webview : vscode . Webview ) {
48109
49- function setWebviewMessageListener ( webview : vscode . Webview ) {
50110 vscode . workspace . onDidChangeConfiguration ( e => {
51111 if ( e . affectsConfiguration ( 'java.jdt.ls.lombokSupport.enabled' ) ) {
52- const msg : SettingChangedMessage = {
53- type : "settingsChanged" ,
112+ currentState . lombokEnabled = isLombokSupportEnabled ( ) ;
113+ const msg : UpdateMessage = {
114+ type : "update" ,
54115 lombokEnabled : isLombokSupportEnabled ( )
55- }
116+ } ;
56117 webview . postMessage ( msg ) ;
57118 }
119+ if ( e . affectsConfiguration ( 'java' ) ) {
120+ setTimeout ( ( ) => refreshLSInfo ( webview ) , 1000 ) ; // wait for LS to pick up the config change
121+ }
58122 } ) ;
59123
60124 webview . onDidReceiveMessage (
61125 async ( message : any ) => {
62126 const command = message . command ;
63127 switch ( command ) {
64- case "webviewReady" :
65- const message : InitializeMessage = {
66- type : "initialize" ,
67- jvms : await getJvms ( ) ,
68- lombokEnabled : isLombokSupportEnabled ( )
128+ case "webviewReady" : {
129+ await apiManager . getApiInstance ( ) . serverReady ;
130+ currentState . lombokEnabled = isLombokSupportEnabled ( ) ;
131+ currentState . activeLombokPath = Lombok . getActiveLombokPath ( ) ;
132+ currentState . workspacePath = getWorkspacePath ( ) ;
133+ const message : UpdateMessage = {
134+ type : "update" ,
135+ lombokEnabled : isLombokSupportEnabled ( ) ,
136+ activeLombokPath : Lombok . getActiveLombokPath ( ) ,
137+ workspacePath : getWorkspacePath ( ) ,
69138 } ;
70139 await webview . postMessage ( message ) ;
140+ getJvms ( ) . then ( jvms => {
141+ currentState . jvms = jvms ;
142+ const msg : UpdateMessage = {
143+ type : "update" ,
144+ jvms : jvms
145+ } ;
146+
147+ webview . postMessage ( msg ) ;
148+ } ) ;
149+
150+ refreshLSInfo ( webview ) ;
71151 break ;
152+ }
72153 }
73154 }
74155 ) ;
0 commit comments