-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBaseLanguageServer.java
More file actions
412 lines (369 loc) · 19.1 KB
/
Copy pathBaseLanguageServer.java
File metadata and controls
412 lines (369 loc) · 19.1 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.rascalmpl.vscode.lsp;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.function.Function;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.SetTraceParams;
import org.eclipse.lsp4j.WorkDoneProgressCancelParams;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
import org.eclipse.lsp4j.jsonrpc.messages.Tuple.Two;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.rascalmpl.ideservices.GsonUtils;
import org.rascalmpl.library.util.PathConfig;
import org.rascalmpl.uri.URIResolverRegistry;
import org.rascalmpl.uri.remote.jsonrpc.RemoteIOError;
import org.rascalmpl.util.NamedThreadPool;
import org.rascalmpl.vscode.lsp.log.LogRedirectConfiguration;
import org.rascalmpl.vscode.lsp.parametric.LanguageRegistry.LanguageParameter;
import org.rascalmpl.vscode.lsp.terminal.RemoteIDEServicesThread;
import org.rascalmpl.vscode.lsp.uri.jsonrpc.RascalFileSystemInVSCode;
import org.rascalmpl.vscode.lsp.uri.jsonrpc.messages.PathConfigParameter;
import org.rascalmpl.vscode.lsp.util.Sets;
import org.rascalmpl.vscode.lsp.util.concurrent.CompletableFutureUtils;
import org.rascalmpl.vscode.lsp.util.locations.Locations;
import io.usethesource.vallang.IList;
import io.usethesource.vallang.ISourceLocation;
/**
* The main language server class for Rascal is built on top of the Eclipse lsp4j library
*/
@SuppressWarnings("java:S106") // we are using system.in/system.out correctly in this class
public abstract class BaseLanguageServer {
private static final PrintStream capturedOut;
private static final InputStream capturedIn;
public static final boolean DEPLOY_MODE;
private static final String LOG_CONFIGURATION_KEY = "log4j2.configurationFactory";
static {
DEPLOY_MODE = System.getProperty("rascal.lsp.deploy", "false").equalsIgnoreCase("true");
if (DEPLOY_MODE){
// we redirect system.out & system.in so that we can use them exclusively for lsp
capturedIn = System.in;
capturedOut = System.out;
System.setIn(new ByteArrayInputStream(new byte[0]));
System.setOut(new PrintStream(System.err, false)); // wrap stderr with a non flushing stream as that is how std.out normally works
}
else {
capturedIn = InputStream.nullInputStream();
capturedOut = new PrintStream(OutputStream.nullOutputStream());
}
System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
// Do not overwrite existing settings (e.g. passed by the extension)
System.setProperty(LOG_CONFIGURATION_KEY, System.getProperty(LOG_CONFIGURATION_KEY, LogRedirectConfiguration.class.getName()));
}
// hide implicit constructor
protected BaseLanguageServer() {}
private static final Logger logger = LogManager.getLogger(BaseLanguageServer.class);
protected static Launcher<IBaseLanguageClient> constructLSPClient(Socket client, ActualLanguageServer server, ExecutorService threadPool)
throws IOException {
client.setTcpNoDelay(true);
return constructLSPClient(client.getInputStream(), client.getOutputStream(), server, threadPool);
}
protected static Launcher<IBaseLanguageClient> constructLSPClient(InputStream in, OutputStream out, ActualLanguageServer server, ExecutorService threadPool) {
Launcher<IBaseLanguageClient> clientLauncher = new Launcher.Builder<IBaseLanguageClient>()
.setLocalService(server)
.setRemoteInterface(IBaseLanguageClient.class)
.setInput(in)
.setOutput(out)
.configureGson(GsonUtils.complexAsJsonObject())
.setExecutorService(threadPool)
.setExceptionHandler(t -> {
if (t instanceof ResponseErrorException) {
return ((ResponseErrorException) t).getResponseError();
}
return RemoteIOError.translate(t).getResponseError();
})
.create();
server.connect(clientLauncher.getRemoteProxy());
return clientLauncher;
}
protected static void printClassPath() {
logger.trace("Started with classpath: {}", () -> System.getProperty("java.class.path"));
}
@FunctionalInterface
protected interface ServerBuilder {
ActualLanguageServer apply(Runnable a, ExecutorService b, IBaseTextDocumentService c, BaseWorkspaceService d);
}
protected static void startLanguageServer(String requestPoolName, String workerPoolName, Function<ExecutorService, IBaseTextDocumentService> docServiceProvider, Function<ExecutorService, BaseWorkspaceService> workspaceServiceProvider, int portNumber) {
startLanguageServer(ActualLanguageServer::new, requestPoolName, workerPoolName, docServiceProvider, workspaceServiceProvider, portNumber);
}
@SuppressWarnings({"java:S2189", "java:S106"})
protected static void startLanguageServer(ServerBuilder serverBuilder, String requestPoolName, String workerPoolName, Function<ExecutorService, IBaseTextDocumentService> docServiceProvider, Function<ExecutorService, BaseWorkspaceService> workspaceServiceProvider, int portNumber) {
logger.info("Starting Rascal Language Server: {}", getVersion());
printClassPath();
if (DEPLOY_MODE) {
var requestPool = NamedThreadPool.single(requestPoolName);
var workerPool = NamedThreadPool.cached(workerPoolName);
try {
var docService = docServiceProvider.apply(workerPool);
var wsService = workspaceServiceProvider.apply(workerPool);
startLSP(constructLSPClient(capturedIn, capturedOut, serverBuilder.apply(() -> System.exit(0), workerPool, docService, wsService), requestPool));
} finally {
requestPool.shutdown();
workerPool.shutdown();
}
}
else {
try (ServerSocket serverSocket = new ServerSocket(portNumber, 0, InetAddress.getByName("127.0.0.1"))) {
logger.info("Rascal LSP server listens on port number: {}", portNumber);
while (true) {
var requestPool = NamedThreadPool.single(requestPoolName);
var workerPool = NamedThreadPool.cached(workerPoolName);
try (Socket clientSocket = serverSocket.accept()) {
logger.info("New client connected to Rascal LSP server (listening on port number: {})", portNumber);
var docService = docServiceProvider.apply(workerPool);
var wsService = workspaceServiceProvider.apply(workerPool);
startLSP(constructLSPClient(clientSocket, serverBuilder.apply(() -> {}, workerPool, docService, wsService), requestPool));
}
finally {
requestPool.shutdown();
workerPool.shutdown();
}
}
} catch (IOException e) {
logger.fatal("Failure to start TCP server on port {}", portNumber, e);
}
}
}
private static final String DEFAULT_VERSION = "unknown";
protected static String getVersion() {
try (InputStream prop = ActualLanguageServer.class.getClassLoader().getResourceAsStream("project.properties")) {
if (prop == null) {
logger.error("Could not find project.properties file");
return DEFAULT_VERSION;
}
Properties properties = new Properties();
properties.load(prop);
return properties.getProperty("rascal.lsp.version", DEFAULT_VERSION) + " at "
+ properties.getProperty("rascal.lsp.build.timestamp",DEFAULT_VERSION);
}
catch (IOException e) {
logger.debug("Cannot find lsp version", e);
return DEFAULT_VERSION;
}
}
protected static void startLSP(Launcher<IBaseLanguageClient> server) {
try {
server.startListening().get();
} catch (InterruptedException e) {
logger.trace("Interrupted server", e);
Thread.currentThread().interrupt();
} catch (ExecutionException e) {
logger.fatal("Unexpected exception", e.getCause());
if (DEPLOY_MODE) {
System.exit(1);
}
} catch (Throwable e) {
logger.fatal("Unexpected exception", e);
if (DEPLOY_MODE) {
System.exit(1);
}
}
}
public static class ActualLanguageServer extends RascalFileSystemInVSCode implements IBaseLanguageServerExtensions, LanguageClientAware {
static final Logger logger = LogManager.getLogger(ActualLanguageServer.class);
private final IBaseTextDocumentService lspDocumentService;
private final BaseWorkspaceService lspWorkspaceService;
private final Runnable onExit;
private final ExecutorService executor;
private @MonotonicNonNull IDEServicesConfiguration remoteIDEServicesConfiguration;
private @MonotonicNonNull IBaseLanguageClient client;
protected ActualLanguageServer(Runnable onExit, ExecutorService executor, IBaseTextDocumentService lspDocumentService, BaseWorkspaceService lspWorkspaceService) {
this.onExit = onExit;
this.executor = executor;
this.lspDocumentService = lspDocumentService;
this.lspWorkspaceService = lspWorkspaceService;
lspDocumentService.pair(lspWorkspaceService);
lspWorkspaceService.pair(lspDocumentService);
}
@Override
public CompletableFuture<IDEServicesConfiguration> supplyRemoteIDEServicesConfiguration() {
if (remoteIDEServicesConfiguration == null) {
return CompletableFuture.failedFuture(new IllegalStateException("No RemoteIDEServices configuration is set"));
}
return CompletableFutureUtils.completedFuture(remoteIDEServicesConfiguration, executor);
}
private static URI[] toURIArray(IList src) {
return src.stream()
.map(ISourceLocation.class::cast)
.map(Locations::toUri)
.toArray(URI[]::new);
}
@Override
public CompletableFuture<Two<String, URI[]>[]> supplyPathConfig(PathConfigParameter projectFolder) {
return CompletableFuture.supplyAsync(() -> {
try {
// TODO: why are we not communicating the JSON representation of the PathConfig constructor?
var pcfg = PathConfig.fromSourceProjectMemberRascalManifest(projectFolder.getLocation(), projectFolder.getMode().mapConfigMode());
@SuppressWarnings("unchecked")
Two<String, URI[]>[] result = new Two[2];
result[0] = new Two<>("Sources", toURIArray(pcfg.getSrcs()));
result[1] = new Two<>("Libraries", toURIArray(pcfg.getLibs()));
return result;
} catch (URISyntaxException e) {
logger.catching(e);
throw new CompletionException(e);
}
}, executor);
}
@Override
public CompletableFuture<Void> sendRegisterLanguage(LanguageParameter lang) {
logger.debug("rascal/sendRegisterLanguage({}, {})", lang.getName(), lang.getMainFunction());
lspDocumentService.registerLanguage(lang);
return CompletableFutureUtils.completedFuture(null, executor);
}
@Override
public CompletableFuture<Void> sendUnregisterLanguage(LanguageParameter lang) {
logger.debug("rascal/sendUnregisterLanguage({})", lang.getName());
lspDocumentService.unregisterLanguage(lang);
return CompletableFutureUtils.completedFuture(null, executor);
}
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
// Exit when our parent process exits
executor.submit(() -> ProcessHandle.of(params.getProcessId()).ifPresent(p -> p.onExit().thenAccept(ignored -> this.exit())));
logger.info("LSP connection started (connected to {} version {})", params.getClientInfo().getName(), params.getClientInfo().getVersion());
logger.debug("LSP client capabilities: {}", params.getCapabilities());
final InitializeResult initializeResult = new InitializeResult(new ServerCapabilities());
lspDocumentService.initializeServerCapabilities(params.getCapabilities(), initializeResult.getCapabilities());
lspWorkspaceService.initialize(params.getCapabilities(), params.getWorkspaceFolders(), initializeResult.getCapabilities());
logger.debug("Initialized LSP connection with capabilities: {}", initializeResult);
return CompletableFutureUtils.completedFuture(initializeResult, executor);
}
@Override
@SuppressWarnings("unused") // InitializedParams is an empty interface
public void initialized(InitializedParams params) {
logger.debug("LSP connection initialized");
lspWorkspaceService.initialized();
lspDocumentService.initialized();
}
@Override
public CompletableFuture<Object> shutdown() {
return CompletableFuture.completedFuture(new Object());
}
@Override
public void exit() {
onExit.run();
}
@Override
public IBaseTextDocumentService getTextDocumentService() {
return lspDocumentService;
}
@Override
public BaseWorkspaceService getWorkspaceService() {
return lspWorkspaceService;
}
@Override
public void setTrace(SetTraceParams params) {
logger.trace("Got trace request: {}", params);
// TODO: handle this trace level
}
@Override
public void connect(LanguageClient client) {
this.client = addShutdownDetectionTo(client);
lspDocumentService.connect(this.client);
lspWorkspaceService.connect(this.client);
connectRemoteRegistryClient(this.client);
remoteIDEServicesConfiguration = RemoteIDEServicesThread.startRemoteIDEServicesServer(this.client, lspDocumentService, executor);
logger.debug("Remote IDE Services Port {}", remoteIDEServicesConfiguration);
}
protected IBaseLanguageClient availableClient() {
if (client == null) {
throw new IllegalStateException("Language Client has not been connected yet");
}
return client;
}
/**
* Creates a proxy instance that forwards method calls to the provided
* language client only when (the thread pool of) this language server
* isn't shutdown yet. Otherwise, the proxy instance throws an
* exception.
*
* This is a workaround for the LSP4J issue that (dis)connection-related
* IO exceptions aren't propagated out of LSP4J (but only logged) when
* the language client is used after shutdown. Thus, proactive effort is
* needed to make sure such usage doesn't happen to begin with
* (https://github.com/eclipse-lsp4j/lsp4j/issues/849).
*/
private IBaseLanguageClient addShutdownDetectionTo(LanguageClient client) {
var loader = IBaseLanguageClient.class.getClassLoader();
var interfaces = new Class<?>[] { IBaseLanguageClient.class };
var handler = (InvocationHandler) (Object proxy, Method method, Object[] args) -> {
if (this.executor.isShutdown()) {
throw new IllegalStateException("The language client can no longer be used, as the server's thread pool is shutdown.");
}
return method.invoke(client, args);
};
return (IBaseLanguageClient) Proxy.newProxyInstance(loader, interfaces, handler);
}
protected ExecutorService getExecutor() {
return executor;
}
@Override
public void cancelProgress(WorkDoneProgressCancelParams params) {
lspDocumentService.cancelProgress(params.getToken().getLeft());
}
@Override
public void setMinimumLogLevel(String level) {
final var l = Level.toLevel(level, Level.DEBUG); // fall back to debug when the string cannot be mapped
Configurator.setRootLevel(l);
}
@Override
public CompletableFuture<String[]> fileSystemSchemes() {
var reg = URIResolverRegistry.getInstance();
var inputs = reg.getRegisteredInputSchemes();
var logicals = reg.getRegisteredLogicalSchemes();
return CompletableFutureUtils.completedFuture(Sets.union(inputs, logicals).toArray(String[]::new), executor);
}
}
}