-
Notifications
You must be signed in to change notification settings - Fork 528
Expand file tree
/
Copy pathdashboard.ts
More file actions
192 lines (168 loc) · 6.3 KB
/
dashboard.ts
File metadata and controls
192 lines (168 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as vscode from 'vscode';
import { apiManager } from '../apiManager';
import { Commands } from '../commands';
import { getComputedJavaConfig, getWorkspacePath } from '../extension';
import { isLombokSupportEnabled, Lombok } from '../lombokSupport';
import { DashboardState, DiagnosticInfo, JVM, UpdateMessage } from '../webviewProtocol/toDashboard';
import { getNonce, getUri } from '../webviewUtils';
import { Uri } from 'vscode';
import * as path from 'path';
const currentState: DashboardState = {
};
class DashboardPanel {
private disposables: vscode.Disposable[] = [];
constructor(private webView: vscode.Webview, private readonly context: vscode.ExtensionContext) {
this.init();
}
private init(): void {
this.disposables.push(vscode.workspace.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('java.jdt.ls.lombokSupport.enabled')) {
currentState.lombokEnabled = isLombokSupportEnabled();
const msg: UpdateMessage = {
type: "update",
lombokEnabled: isLombokSupportEnabled()
};
this.postMessage(msg);
}
if (e.affectsConfiguration('java')) {
setTimeout(() => this.refreshLSInfo(), 1000); // wait for LS to pick up the config change
}
}));
this.setWebviewMessageListener();
this.webView.html = this.getWebviewContent();
this.disposables.push(vscode.commands.registerCommand('java.dashboard.revealFileInOS', async (arg: { path: string }) => {
await vscode.commands.executeCommand('revealFileInOS', vscode.Uri.file(arg.path));
}));
}
private postMessage(message: UpdateMessage) {
if (this.webView) {
this.webView.postMessage(message);
}
}
private setWebviewMessageListener() {
this.webView.onDidReceiveMessage(
async (message: any) => {
const command = message.command;
switch (command) {
case "webviewReady": {
await apiManager.getApiInstance().serverReady();
currentState.lombokEnabled = isLombokSupportEnabled();
currentState.activeLombokPath = Lombok.getActiveLombokPath();
currentState.workspacePath = getWorkspacePath();
const message: UpdateMessage = {
type: "update",
lombokEnabled: isLombokSupportEnabled(),
activeLombokPath: Lombok.getActiveLombokPath(),
workspacePath: getWorkspacePath(),
};
await this.postMessage(message);
this.getJvms().then(jvms => {
currentState.jvms = jvms;
const msg: UpdateMessage = {
type: "update",
jvms: jvms
};
this.postMessage(msg);
});
this.refreshLSInfo();
break;
}
}
}
);
}
public dispose(): void {
this.webView = undefined;
for (const disposable of this.disposables) {
disposable.dispose();
}
}
private async getJvms(): Promise<JVM[]> {
const config = await getComputedJavaConfig();
const jres: JVM[] = config.configuration.runtimes.map(jre => ({
name: jre.name,
version: jre.version,
path: jre.path,
}));
return jres;
}
private getWebviewContent(): string {
const scriptUri = getUri(this.webView, this.context.extensionUri, "dist", "dashboard.js");
const styleUri = getUri(this.webView, this.context.extensionUri, "dist", "dashboard.css");
return /* html*/ `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src ${this.webView.cspSource}; font-src ${this.webView.cspSource} data:; style-src ${this.webView.cspSource};">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Dashboard</title>
<link href="${styleUri}" rel="stylesheet">
</head>
<body>
<div id="root"></div>
<script src="${scriptUri}"></script>
</body>
</html>
`;
}
public async refreshLSInfo(): Promise<void> {
if (!this.webView) {
return;
}
try {
vscode.commands.executeCommand<DiagnosticInfo>(Commands.EXECUTE_WORKSPACE_COMMAND, Commands.GET_TROUBLESHOOTING_INFO).then(info => {
currentState.diagnosticInfo = info;
const msg: UpdateMessage = {
type: "update",
diagnosticInfo: info
};
this.postMessage(msg);
});
} catch (e) {
console.error('Failed to get diagnostic info', e);
}
}
}
export namespace Dashboard {
export function initialize(context: vscode.ExtensionContext): void {
console.log('registering dashboard webview provider');
let dashboardPanel: DashboardPanel | undefined = undefined;
let webviewPanel: vscode.WebviewPanel | undefined = undefined;
context.subscriptions.push(vscode.commands.registerCommand('java.dashboard.refresh', async () => {
await vscode.commands.executeCommand(Commands.OPEN_JAVA_DASHBOARD);
if (dashboardPanel) {
await dashboardPanel.refreshLSInfo();
}
}));
context.subscriptions.push(vscode.commands.registerCommand('java.dashboard.dumpState', async () => {
const doc = await vscode.workspace.openTextDocument({
language: 'json',
content: JSON.stringify(currentState, null, 2)
});
vscode.window.showTextDocument(doc);
}));
context.subscriptions.push(vscode.commands.registerCommand(Commands.OPEN_JAVA_DASHBOARD, async () => {
if (!dashboardPanel || !webviewPanel) {
webviewPanel = vscode.window.createWebviewPanel('java.dashboard', 'Java Dashboard', vscode.ViewColumn.Active, {
enableScripts: true,
enableCommandUris: true,
retainContextWhenHidden: true,
localResourceRoots: [context.extensionUri],
});
webviewPanel.iconPath = Uri.file(path.join(context.extensionPath, 'icons', 'icon128.png'));
dashboardPanel = new DashboardPanel(webviewPanel.webview, context);
webviewPanel.onDidDispose(() => {
dashboardPanel?.dispose();
dashboardPanel = undefined;
webviewPanel = undefined;
vscode.commands.executeCommand('setContext', 'java:dashboard', false);
}, undefined, context.subscriptions);
} else {
webviewPanel.reveal();
}
}));
console.log('registered dashboard webview provider');
}
}