-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathprojectSettingsView.ts
More file actions
236 lines (204 loc) · 8.77 KB
/
Copy pathprojectSettingsView.ts
File metadata and controls
236 lines (204 loc) · 8.77 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as path from "path";
import * as vscode from "vscode";
import { instrumentOperation, sendError, sendInfo, setUserError } from "vscode-extension-telemetry-wrapper";
import { getExtensionContext, getNonce } from "../utils";
import { ClasspathRequestHandler } from "./handlers/ClasspathRequestHandler";
import { MavenRequestHandler } from "./handlers/MavenRequestHandler";
import { CompilerRequestHandler } from "./handlers/CompilerRequestHandler";
import { ProjectSettingsException, ProjectInfo } from "./types";
import _ from "lodash";
import { getProjectNameFromUri, isDefaultProject } from "../utils/jdt";
import { compareVersions } from "compare-versions";
let projectSettingsPanel: vscode.WebviewPanel | undefined;
let lsApi: LanguageServerAPI | undefined;
const MINIMUM_JAVA_EXTENSION_VERSION: string = "1.31.0";
class ProjectSettingView {
private classpathRequestHandler: ClasspathRequestHandler | undefined = undefined;
public async showProjectSettingsPage(sectionId: string = "classpath"): Promise<void> {
const context: vscode.ExtensionContext = getExtensionContext();
if (!projectSettingsPanel) {
projectSettingsPanel = vscode.window.createWebviewPanel(
"java.projectSettings",
"Project Settings",
vscode.ViewColumn.Active,
{
retainContextWhenHidden: true
}
);
await this.initializeWebview(context);
const oneTimeHook = projectSettingsPanel.webview.onDidReceiveMessage(() => {
// send the route change msg once react component is ready.
// and dispose it once it's done.
projectSettingsPanel?.webview.postMessage({
command: "main.onWillChangeRoute",
route: sectionId
});
oneTimeHook.dispose();
});
} else {
// if the panel is already opened, we just send the route change msg.
projectSettingsPanel?.webview.postMessage({
command: "main.onWillChangeRoute",
route: sectionId
});
}
projectSettingsPanel.reveal();
}
public async initializeWebview(context: vscode.ExtensionContext): Promise<void> {
if (!projectSettingsPanel) {
sendError(new Error("projectSettingsPanel is not defined."));
return;
}
projectSettingsPanel.webview.options = {
enableScripts: true,
enableCommandUris: true,
}
projectSettingsPanel.onDidDispose(() => {
projectSettingsPanel = undefined;
});
context.subscriptions.push(projectSettingsPanel.onDidDispose(_e => projectSettingsPanel = undefined));
projectSettingsPanel.iconPath = {
light: vscode.Uri.file(path.join(context.extensionPath, "caption.light.svg")),
dark: vscode.Uri.file(path.join(context.extensionPath, "caption.dark.svg"))
};
context.subscriptions.push(
this.classpathRequestHandler = new ClasspathRequestHandler(projectSettingsPanel.webview),
new CompilerRequestHandler(projectSettingsPanel.webview),
new MavenRequestHandler(projectSettingsPanel.webview),
projectSettingsPanel.webview.onDidReceiveMessage(async (message) => {
switch (message.command) {
case "common.onWillListProjects":
await this.listProjects();
break;
case "common.onWillExecuteCommand":
this.executeCommand(message.id);
break;
default:
break;
}
}),
);
projectSettingsPanel.webview.html = this.getHtmlForWebview(projectSettingsPanel.webview, context.asAbsolutePath("./out/assets/project-settings/index.js"));
}
private listProjects = instrumentOperation("projectSettings.classpath.listProjects", async (operationId: string) => {
// listProjects() will be called when the component is mounted,
// we first check the requirement here in case user triggers 'reload webview'
if (!(await this.checkRequirement())) {
return;
}
let projects: ProjectInfo[] = await this.getProjectsFromLS();
_.remove(projects, (p: ProjectInfo) => {
return isDefaultProject(p.rootPath);
});
if (projects.length === 0) {
projectSettingsPanel?.webview.postMessage({
command: "main.onException",
exception: ProjectSettingsException.NoJavaProjects,
});
} else {
projectSettingsPanel?.webview.postMessage({
command: "main.onDidListProjects",
projectInfo: projects,
});
}
sendInfo(operationId, {
projectNumber: projects.length,
});
});
private checkRequirement = async (): Promise<boolean> => {
if (lsApi) {
return true;
}
const javaExt = vscode.extensions.getExtension("redhat.java");
if (!javaExt) {
projectSettingsPanel?.webview.postMessage({
command: "main.onException",
exception: ProjectSettingsException.JavaExtensionNotInstalled,
});
const err: Error = new Error("The extension 'redhat.java' is not installed.");
setUserError(err);
sendError(err);
return false;
}
const javaExtVersion: string = javaExt.packageJSON.version;
if (compareVersions(javaExtVersion, MINIMUM_JAVA_EXTENSION_VERSION) < 0) {
projectSettingsPanel?.webview.postMessage({
command: "main.onException",
exception: ProjectSettingsException.StaleJavaExtension,
});
const err: Error = new Error(`The extension version of 'redhat.java' (${javaExtVersion}) is too stale.`);
setUserError(err);
sendError(err);
return false;
}
await javaExt.activate();
lsApi = javaExt.exports;
if (lsApi) {
getExtensionContext().subscriptions.push(
lsApi.onDidProjectsImport(() => {
this.listProjects();
}),
lsApi.onDidClasspathUpdate((uri: vscode.Uri) => {
this.classpathRequestHandler?.debounceLoadProjectClasspath(uri);
}),
);
}
return true;
};
private async getProjectsFromLS(): Promise<ProjectInfo[]> {
const ret: ProjectInfo[] = [];
let projects: string[] = [];
try {
projects = await vscode.commands.executeCommand("java.execute.workspaceCommand", "java.project.getAll") || [];
} catch (error) {
// LS not ready
}
for (const projectRoot of projects) {
ret.push({
name: getProjectNameFromUri(projectRoot),
rootPath: projectRoot,
});
}
return ret;
}
private getHtmlForWebview(webview: vscode.Webview, scriptPath: string) {
const scriptPathOnDisk = vscode.Uri.file(scriptPath);
const scriptUri = webview.asWebviewUri(scriptPathOnDisk);
// Use a nonce to whitelist which scripts can be run
const nonce = getNonce();
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<title>Project Settings</title>
</head>
<body>
<script nonce="${nonce}" src="${scriptUri}" type="module"></script>
<div id="content"></div>
</body>
</html>
`;
}
private executeCommand = instrumentOperation("projectSetting.executeCommand", async (operationId: string, commandId: string) => {
await vscode.commands.executeCommand(commandId);
sendInfo(operationId, {
operationName: "projectSetting.executeCommand",
arg: commandId,
});
});
}
export class ProjectSettingsViewSerializer implements vscode.WebviewPanelSerializer {
async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, _state: any) {
projectSettingsPanel = webviewPanel;
await projectSettingView.initializeWebview(getExtensionContext());
}
}
interface LanguageServerAPI {
onDidProjectsImport: vscode.Event<vscode.Uri>;
onDidClasspathUpdate: vscode.Event<vscode.Uri>;
}
export const projectSettingView = new ProjectSettingView();