-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBaseLanguageServer.java
More file actions
339 lines (305 loc) · 15.5 KB
/
BaseLanguageServer.java
File metadata and controls
339 lines (305 loc) · 15.5 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
/*
* 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.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.BiFunction;
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.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.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;
private 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);
private static Launcher<IBaseLanguageClient> constructLSPClient(Socket client, ActualLanguageServer server, ExecutorService threadPool)
throws IOException {
client.setTcpNoDelay(true);
return constructLSPClient(client.getInputStream(), client.getOutputStream(), server, threadPool);
}
private 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 -> RemoteIOError.translate((Exception) t).getResponseError())
.create();
server.connect(clientLauncher.getRemoteProxy());
return clientLauncher;
}
private static void printClassPath() {
logger.trace("Started with classpath: {}", () -> System.getProperty("java.class.path"));
}
@SuppressWarnings({"java:S2189", "java:S106"})
public static void startLanguageServer(ExecutorService requestPool, ExecutorService workerPool, Function<ExecutorService, IBaseTextDocumentService> docServiceProvider, BiFunction<ExecutorService, IBaseTextDocumentService, BaseWorkspaceService> workspaceServiceProvider, int portNumber) {
logger.info("Starting Rascal Language Server: {}", getVersion());
printClassPath();
if (DEPLOY_MODE) {
var docService = docServiceProvider.apply(workerPool);
var wsService = workspaceServiceProvider.apply(workerPool, docService);
docService.pair(wsService);
startLSP(constructLSPClient(capturedIn, capturedOut, new ActualLanguageServer(() -> System.exit(0), workerPool, docService, wsService), requestPool));
}
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 docService = docServiceProvider.apply(workerPool);
var wsService = workspaceServiceProvider.apply(workerPool, docService);
docService.pair(wsService);
startLSP(constructLSPClient(serverSocket.accept(), new ActualLanguageServer(() -> {}, workerPool, docService, wsService), requestPool));
}
} catch (IOException e) {
logger.fatal("Failure to start TCP server on port {}", portNumber, e);
}
}
}
private static final String DEFAULT_VERSION = "unknown";
private 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;
}
}
private 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);
}
}
}
private 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 ActualLanguageServer(Runnable onExit, ExecutorService executor, IBaseTextDocumentService lspDocumentService, BaseWorkspaceService lspWorkspaceService) {
this.onExit = onExit;
this.executor = executor;
this.lspDocumentService = lspDocumentService;
this.lspWorkspaceService = lspWorkspaceService;
}
@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) {
return CompletableFuture.runAsync(() -> lspDocumentService.registerLanguage(lang), executor);
}
@Override
public CompletableFuture<Void> sendUnregisterLanguage(LanguageParameter lang) {
return CompletableFuture.runAsync(() -> lspDocumentService.unregisterLanguage(lang), executor);
}
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
return CompletableFuture.supplyAsync(() -> {
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 initializeResult;
}, executor);
}
@Override
@SuppressWarnings("unused") // InitializedParams is an empty interface
public void initialized(InitializedParams params) {
executor.submit(() -> {
logger.debug("LSP connection initialized");
lspWorkspaceService.initialized();
lspDocumentService.initialized();
});
}
@Override
public CompletableFuture<Object> shutdown() {
return CompletableFuture.supplyAsync(() -> {
lspDocumentService.shutdown();
return true;
}, executor);
}
@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) {
var actualClient = (IBaseLanguageClient) client;
lspDocumentService.connect(actualClient);
lspWorkspaceService.connect(actualClient);
this.client = actualClient;
remoteIDEServicesConfiguration = RemoteIDEServicesThread.startRemoteIDEServicesServer(client, lspDocumentService, executor);
logger.debug("Remote IDE Services Port {}", remoteIDEServicesConfiguration);
}
@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);
}
}
}