-
Notifications
You must be signed in to change notification settings - Fork 1
/
extension.ts
293 lines (256 loc) · 7.65 KB
/
extension.ts
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import * as child_process from "child_process";
import * as os from "os";
import * as path from "path";
import * as util from "util";
import {
ExtensionContext,
OutputChannel,
ProgressLocation,
Uri,
WorkspaceConfiguration,
window,
workspace,
} from "vscode";
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from "vscode-languageclient/node";
import { getApi, FileDownloader } from "@microsoft/vscode-file-downloader-api";
import { findRuntimes, JAVA_FILENAME } from "jdk-utils";
const isLinux = os.platform() === "linux";
const isWindows = os.platform() === "win32";
const execFile = util.promisify(child_process.execFile);
const COURSIER_DOWNLOAD_TIMEOUT_MS = 120000;
const COURSIER_LAUNCHER_URI = Uri.parse(
"https://github.com/coursier/coursier/raw/gh-pages/coursier"
);
let client: LanguageClient;
async function downloadCoursierJar(
context: ExtensionContext,
fileDownloader: FileDownloader
): Promise<Uri> {
return await window.withProgress(
{
location: ProgressLocation.Window,
},
(progress, cancellationToken) => {
return fileDownloader.downloadFile(
COURSIER_LAUNCHER_URI,
"coursier.jar",
context,
cancellationToken,
(_downloadedBytes, _totalBytes) =>
progress.report({ message: "Downloading coursier JAR" }),
{ timeoutInMs: COURSIER_DOWNLOAD_TIMEOUT_MS }
);
}
);
}
async function fetchCoursierJar(
context: ExtensionContext,
fileDownloader: FileDownloader
): Promise<Uri> {
return (
(await fileDownloader.tryGetItem("coursier.jar", context)) ||
(await downloadCoursierJar(context, fileDownloader))
);
}
async function getServerClasspath(
javaExecutable: string,
coursierJar: Uri,
serverConfiguration: WorkspaceConfiguration
): Promise<string> {
const serverVersion = serverConfiguration.get<number>("version");
return (
await execFile(
javaExecutable,
[
"-jar",
coursierJar.fsPath,
"fetch",
"--classpath",
`org.mina-lang:mina-lang-server:${serverVersion}`,
],
{ env: { COURSIER_NO_TERM: "true", ...process.env } }
)
).stdout.trim();
}
function getGcOptions(): string[] {
// As per [https://access.redhat.com/documentation/en-us/openjdk/11/html/using_shenandoah_garbage_collector_with_openjdk_11/shenandoah-gc-basic-configuration]
const gcOptions = [
"-XX:+UseShenandoahGC",
"-XX:+AlwaysPreTouch",
"-XX:+UseNUMA",
"-XX:+DisableExplicitGC",
];
if (isLinux) {
gcOptions.push("-XX:+UseLargePages");
gcOptions.push("-XX:+UseTransparentHugePages");
}
return gcOptions;
}
function getRemoteDebugOptions(): string[] {
const remoteDebugConfig = workspace.getConfiguration(
"mina.languageServer.remoteDebug"
);
const remoteDebugEnabled = remoteDebugConfig.get<boolean>("enabled");
const remoteDebugAddress = remoteDebugConfig.get<string>("address");
const remoteDebugPort = remoteDebugConfig.get<number>("port");
const remoteDebugSuspend = remoteDebugConfig.get<boolean>("suspend")
? "y"
: "n";
if (!remoteDebugEnabled) {
return [];
}
const jdwpOptions = [
"transport=dt_socket",
"server=y",
`suspend=${remoteDebugSuspend}`,
`address=${remoteDebugAddress}:${remoteDebugPort}`,
].join(",");
return [`-agentlib:jdwp=${jdwpOptions}`];
}
async function getProfilingOptions(
javaExecutable: string,
coursierJar: Uri
): Promise<string[]> {
const profilingConfig = workspace.getConfiguration(
"mina.languageServer.profiling"
);
const profilingEnabled = profilingConfig.get<boolean>("enabled");
const profilingAppName = profilingConfig.get<string>("applicationName");
const profilingServerAddress = profilingConfig.get<string>("serverAddress");
const profilingAgentVersion = profilingConfig.get<string>("agentVersion");
if (!profilingEnabled || !profilingAgentVersion) {
return [];
}
const profilingAgentClasspath = await getProfilingAgentClasspath(
javaExecutable,
coursierJar,
profilingAgentVersion
);
return [
`-javaagent:${profilingAgentClasspath}`,
`-Dpyroscope.application.name=${profilingAppName}`,
`-Dpyroscope.server.address=${profilingServerAddress}`,
"-Dpyroscope.format=jfr",
];
}
async function getProfilingAgentClasspath(
javaExecutable: string,
coursierJar: Uri,
agentVersion: string
): Promise<string> {
return (
await execFile(
javaExecutable,
[
"-jar",
coursierJar.fsPath,
"fetch",
"--classpath",
`io.pyroscope:agent:${agentVersion}`,
],
{ env: { COURSIER_NO_TERM: "true", ...process.env } }
)
).stdout.trim();
}
async function start(context: ExtensionContext, outputChannel: OutputChannel) {
const runtimes = await findRuntimes({ withVersion: true, withTags: true });
const javaHomeRuntime = runtimes.find((runtime) => runtime.isJavaHomeEnv);
const serverConfiguration = workspace.getConfiguration("mina.languageServer");
if (javaHomeRuntime) {
const fileDownloader: FileDownloader = await getApi();
const coursierJar = await fetchCoursierJar(context, fileDownloader);
const javaExecutable = path.join(
javaHomeRuntime.homedir,
"bin",
JAVA_FILENAME
);
const gcOptions = getGcOptions();
const remoteDebugOptions = getRemoteDebugOptions();
const jvmOptions = serverConfiguration.get<string[]>("jvmOptions", []);
const profilingOptions = await getProfilingOptions(
javaExecutable,
coursierJar
);
const serverClasspath = await getServerClasspath(
javaExecutable,
coursierJar,
serverConfiguration
);
const serverOptions: ServerOptions = {
transport: isWindows
? { kind: TransportKind.socket, port: 8084 }
: TransportKind.pipe,
command: javaExecutable,
args: [
`-DSTORAGE_FOLDER=${context.globalStorageUri.fsPath}`,
`-DLOG_FOLDER=${context.logUri.fsPath}`,
...jvmOptions,
...remoteDebugOptions,
...gcOptions,
...profilingOptions,
"-cp",
serverClasspath,
"org.mina_lang.langserver.MinaLanguageServerLauncher",
],
};
const clientOptions: LanguageClientOptions = {
outputChannel,
documentSelector: [
{ scheme: "file", language: "mina" },
{ scheme: "jar", language: "mina" },
],
synchronize: {
fileEvents: workspace.createFileSystemWatcher("**/*.mina"),
},
};
client = new LanguageClient(
"mina-lang-server",
"Mina Language Server",
serverOptions,
clientOptions
);
await client.start();
} else {
return await window.showErrorMessage(
"The JAVA_HOME variable is not set. Unable to determine JDK location."
);
}
}
async function stop() {
if (!client) {
return;
} else {
return await client.stop();
}
}
async function restart(
context: ExtensionContext,
outputChannel: OutputChannel
) {
await stop();
await start(context, outputChannel);
}
export async function activate(context: ExtensionContext) {
const outputChannel = window.createOutputChannel("Mina Language Server");
await start(context, outputChannel);
context.subscriptions.push(
workspace.onDidChangeConfiguration(async (event) => {
// Restart when configuration that affects server artifact or process args is changed
if (
event.affectsConfiguration("mina.languageServer.version") ||
event.affectsConfiguration("mina.languageServer.remoteDebug") ||
event.affectsConfiguration("mina.languageServer.profiling")
) {
await restart(context, outputChannel);
}
})
);
}
export async function deactivate() {
await stop();
}