-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNextflowLanguageServer.java
More file actions
668 lines (588 loc) · 26.4 KB
/
NextflowLanguageServer.java
File metadata and controls
668 lines (588 loc) · 26.4 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
* Copyright 2024, Seqera Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nextflow.lsp;
import java.net.URI;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import nextflow.lsp.file.PathUtils;
import nextflow.lsp.util.Logger;
import nextflow.lsp.services.util.CustomFormattingOptions;
import nextflow.lsp.services.LanguageService;
import nextflow.lsp.services.config.ConfigService;
import nextflow.lsp.services.script.ScriptService;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.eclipse.lsp4j.CallHierarchyIncomingCall;
import org.eclipse.lsp4j.CallHierarchyIncomingCallsParams;
import org.eclipse.lsp4j.CallHierarchyItem;
import org.eclipse.lsp4j.CallHierarchyOutgoingCall;
import org.eclipse.lsp4j.CallHierarchyOutgoingCallsParams;
import org.eclipse.lsp4j.CallHierarchyPrepareParams;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensOptions;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.CreateFilesParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.DeleteFilesParams;
import org.eclipse.lsp4j.DidChangeConfigurationParams;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidChangeWatchedFilesParams;
import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentLinkOptions;
import org.eclipse.lsp4j.DocumentLinkParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.ExecuteCommandOptions;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.RenameFilesParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.ProgressParams;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.ServerCapabilities;
import org.eclipse.lsp4j.SetTraceParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SignatureHelpOptions;
import org.eclipse.lsp4j.SignatureHelpParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentSyncKind;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkDoneProgressBegin;
import org.eclipse.lsp4j.WorkDoneProgressCreateParams;
import org.eclipse.lsp4j.WorkDoneProgressEnd;
import org.eclipse.lsp4j.WorkDoneProgressReport;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceFoldersOptions;
import org.eclipse.lsp4j.WorkspaceServerCapabilities;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.WorkspaceSymbolParams;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
import org.eclipse.lsp4j.jsonrpc.Launcher;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.LanguageServer;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.eclipse.lsp4j.services.WorkspaceService;
/**
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
public class NextflowLanguageServer implements LanguageServer, LanguageClientAware, TextDocumentService, WorkspaceService {
public static void main(String[] args) {
var server = new NextflowLanguageServer();
var launcher = Launcher.createLauncher(server, LanguageClient.class, System.in, System.out);
server.connect(launcher.getRemoteProxy());
launcher.startListening();
}
private static final String DEFAULT_WORKSPACE_FOLDER_NAME = "";
private static Logger log = Logger.getInstance();
private LanguageClient client = null;
private Map<String, String> workspaceRoots = new HashMap<>();
private Map<String, LanguageService> scriptServices = new HashMap<>();
private Map<String, LanguageService> configServices = new HashMap<>();
private List<String> excludePatterns;
private boolean harshilAlignment;
private boolean suppressWarnings;
// -- LanguageServer
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
for( var workspaceFolder : params.getWorkspaceFolders() ) {
var name = workspaceFolder.getName();
var uri = workspaceFolder.getUri();
addWorkspaceFolder(name, uri);
}
if( params.getWorkspaceFolders().isEmpty() )
addWorkspaceFolder(DEFAULT_WORKSPACE_FOLDER_NAME, null);
var serverCapabilities = new ServerCapabilities();
serverCapabilities.setTextDocumentSync(TextDocumentSyncKind.Incremental);
var workspaceFoldersOptions = new WorkspaceFoldersOptions();
workspaceFoldersOptions.setSupported(true);
workspaceFoldersOptions.setChangeNotifications(true);
var workspaceCapabilities = new WorkspaceServerCapabilities(workspaceFoldersOptions);
serverCapabilities.setWorkspace(workspaceCapabilities);
serverCapabilities.setCallHierarchyProvider(true);
var codeLensOptions = new CodeLensOptions(false);
serverCapabilities.setCodeLensProvider(codeLensOptions);
var completionOptions = new CompletionOptions(false, List.of("."));
serverCapabilities.setCompletionProvider(completionOptions);
serverCapabilities.setDefinitionProvider(true);
serverCapabilities.setDocumentFormattingProvider(true);
var documentLinkOptions = new DocumentLinkOptions(false);
serverCapabilities.setDocumentLinkProvider(documentLinkOptions);
serverCapabilities.setDocumentSymbolProvider(true);
var executeCommandOptions = new ExecuteCommandOptions(List.of("nextflow.server.previewDag"));
serverCapabilities.setExecuteCommandProvider(executeCommandOptions);
serverCapabilities.setHoverProvider(true);
serverCapabilities.setReferencesProvider(true);
serverCapabilities.setRenameProvider(true);
var signatureHelpOptions = new SignatureHelpOptions(List.of("(", ","));
serverCapabilities.setSignatureHelpProvider(signatureHelpOptions);
serverCapabilities.setWorkspaceSymbolProvider(true);
var initializeResult = new InitializeResult(serverCapabilities);
return CompletableFuture.completedFuture(initializeResult);
}
@Override
public void setTrace(SetTraceParams params) {
System.err.println("trace " + params.getValue());
}
@Override
public CompletableFuture<Object> shutdown() {
return CompletableFuture.completedFuture(new Object());
}
@Override
public void exit() {
System.exit(0);
}
@Override
public TextDocumentService getTextDocumentService() {
return this;
}
@Override
public WorkspaceService getWorkspaceService() {
return this;
}
// -- LanguageClientAware
@Override
public void connect(LanguageClient client) {
this.client = client;
log.initialize(client);
}
// -- TextDocumentService
@Override
public void didOpen(DidOpenTextDocumentParams params) {
var uri = params.getTextDocument().getUri();
log.debug("textDocument/didOpen " + relativePath(uri));
var service = getLanguageService(uri);
if( service != null )
service.didOpen(params);
}
@Override
public void didChange(DidChangeTextDocumentParams params) {
var uri = params.getTextDocument().getUri();
log.debug("textDocument/didChange " + relativePath(uri));
var service = getLanguageService(uri);
if( service != null )
service.didChange(params);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
var uri = params.getTextDocument().getUri();
log.debug("textDocument/didClose " + relativePath(uri));
var service = getLanguageService(uri);
if( service != null )
service.didClose(params);
}
@Override
public void didSave(DidSaveTextDocumentParams params) {
var uri = params.getTextDocument().getUri();
log.debug("textDocument/didSave " + relativePath(uri));
}
@Override
public CompletableFuture<List<CallHierarchyItem>> prepareCallHierarchy(CallHierarchyPrepareParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
log.debug("textDocument/prepareCallHierarchy " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.prepareCallHierarchy(params);
});
}
@Override
public CompletableFuture<List<CallHierarchyIncomingCall>> callHierarchyIncomingCalls(CallHierarchyIncomingCallsParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var item = params.getItem();
var uri = item.getUri();
log.debug("textDocument/callHierarchyIncomingCalls " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.callHierarchyIncomingCalls(item);
});
}
@Override
public CompletableFuture<List<CallHierarchyOutgoingCall>> callHierarchyOutgoingCalls(CallHierarchyOutgoingCallsParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var item = params.getItem();
var uri = item.getUri();
log.debug("textDocument/callHierarchyOutgoingCalls " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.callHierarchyOutgoingCalls(item);
});
}
@Override
public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
log.debug("textDocument/codeLens " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return Collections.emptyList();
return service.codeLens(params);
});
}
@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var position = params.getPosition();
log.debug(String.format("textDocument/completion %s [ %d, %d ]", relativePath(uri), position.getLine(), position.getCharacter()));
var service = getLanguageService(uri);
if( service == null )
return Either.forLeft(Collections.emptyList());
return service.completion(params);
});
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(DefinitionParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var position = params.getPosition();
log.debug(String.format("textDocument/definition %s [ %d, %d ]", relativePath(uri), position.getLine(), position.getCharacter()));
var service = getLanguageService(uri);
if( service == null )
return Either.forLeft(Collections.emptyList());
return service.definition(params);
});
}
@Override
public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
log.debug("textDocument/documentLink " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return Collections.emptyList();
return service.documentLink(params);
});
}
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(DocumentSymbolParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
log.debug("textDocument/symbol " + relativePath(uri));
var service = getLanguageService(uri);
if( service == null )
return Collections.emptyList();
return service.documentSymbol(params);
});
}
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var options = new CustomFormattingOptions(
params.getOptions().getTabSize(),
params.getOptions().isInsertSpaces(),
harshilAlignment
);
log.debug(String.format("textDocument/formatting %s %s %d", relativePath(uri), options.insertSpaces() ? "spaces" : "tabs", options.tabSize()));
var service = getLanguageService(uri);
if( service == null )
return Collections.emptyList();
return service.formatting(URI.create(uri), options);
});
}
@Override
public CompletableFuture<Hover> hover(HoverParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var service = getLanguageService(uri);
if( service == null )
return null;
return service.hover(params);
});
}
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var position = params.getPosition();
log.debug(String.format("textDocument/references %s [ %d, %d ]", relativePath(uri), position.getLine(), position.getCharacter()));
var service = getLanguageService(uri);
if( service == null )
return Collections.emptyList();
return service.references(params);
});
}
@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var position = params.getPosition();
log.debug(String.format("textDocument/rename %s [ %d, %d ] -> %s", relativePath(uri), position.getLine(), position.getCharacter(), params.getNewName()));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.rename(params);
});
}
@Override
public CompletableFuture<SignatureHelp> signatureHelp(SignatureHelpParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var uri = params.getTextDocument().getUri();
var position = params.getPosition();
log.debug(String.format("textDocument/signatureHelp %s [ %d, %d ]", relativePath(uri), position.getLine(), position.getCharacter()));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.signatureHelp(params);
});
}
// --- WorkspaceService
@Override
public void didChangeConfiguration(DidChangeConfigurationParams params) {
log.debug("workspace/didChangeConfiguration " + params.getSettings());
var shouldInitialize = false;
var debug = getJsonBoolean(params.getSettings(), "nextflow.debug");
if( debug != null )
Logger.setDebugEnabled(debug);
var excludePatterns = getJsonStringArray(params.getSettings(), "nextflow.files.exclude");
if( !DefaultGroovyMethods.equals(this.excludePatterns, excludePatterns) ) {
this.excludePatterns = excludePatterns;
shouldInitialize = true;
}
var harshilAlignment = getJsonBoolean(params.getSettings(), "nextflow.formatting.harshilAlignment");
if( harshilAlignment != null )
this.harshilAlignment = harshilAlignment;
var suppressWarnings = getJsonBoolean(params.getSettings(), "nextflow.suppressWarnings");
if( suppressWarnings != null && this.suppressWarnings != suppressWarnings ) {
this.suppressWarnings = suppressWarnings;
shouldInitialize = true;
}
if( shouldInitialize ) {
var progressToken = "initialize";
progressCreate(progressToken);
progressBegin(progressToken, "Initializing workspace...");
var count = 0;
var total = workspaceRoots.keySet().size();
for( var name : workspaceRoots.keySet() ) {
var progressMessage = String.format("Initializing workspace: %s (%d / %d)", name, count + 1, total);
progressUpdate(progressToken, progressMessage, count * 100 / total);
count++;
var uri = workspaceRoots.get(name);
scriptServices.get(name).initialize(uri, this.excludePatterns, this.suppressWarnings);
configServices.get(name).initialize(uri, this.excludePatterns, this.suppressWarnings);
}
progressEnd(progressToken);
}
}
private void progressCreate(String token) {
client.createProgress(new WorkDoneProgressCreateParams(Either.forLeft(token)));
}
private void progressBegin(String token, String message) {
var progress = new WorkDoneProgressBegin();
progress.setMessage(message);
progress.setPercentage(0);
client.notifyProgress(new ProgressParams(Either.forLeft(token), Either.forLeft(progress)));
}
private void progressUpdate(String token, String message, int percentage) {
var progress = new WorkDoneProgressReport();
progress.setMessage(message);
progress.setPercentage(percentage);
client.notifyProgress(new ProgressParams(Either.forLeft(token), Either.forLeft(progress)));
}
private void progressEnd(String token) {
var progress = new WorkDoneProgressEnd();
client.notifyProgress(new ProgressParams(Either.forLeft(token), Either.forLeft(progress)));
}
@Override
public void didChangeWatchedFiles(DidChangeWatchedFilesParams params) {
log.debug("workspace/didChangeWatchedFiles " + params);
}
@Override
public void didChangeWorkspaceFolders(DidChangeWorkspaceFoldersParams params) {
var event = params.getEvent();
for( var workspaceFolder : event.getRemoved() ) {
var name = workspaceFolder.getName();
log.debug("workspace/didChangeWorkspaceFolders remove " + name);
workspaceRoots.remove(name);
scriptServices.remove(name).clearDiagnostics();
configServices.remove(name).clearDiagnostics();
}
for( var workspaceFolder : event.getAdded() ) {
var name = workspaceFolder.getName();
var uri = workspaceFolder.getUri();
log.debug("workspace/didChangeWorkspaceFolders add " + name + " " + uri);
addWorkspaceFolder(name, uri);
scriptServices.get(name).initialize(uri, excludePatterns, suppressWarnings);
configServices.get(name).initialize(uri, excludePatterns, suppressWarnings);
}
}
@Override
public void didCreateFiles(CreateFilesParams params) {
log.debug("workspace/didCreateFiles " + params);
}
@Override
public void didDeleteFiles(DeleteFilesParams params) {
log.debug("workspace/didDeleteFiles " + params);
}
@Override
public void didRenameFiles(RenameFilesParams params) {
log.debug("workspace/didRenameFiles " + params);
}
@Override
public CompletableFuture<Object> executeCommand(ExecuteCommandParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
var command = params.getCommand();
var arguments = params.getArguments();
if( !"nextflow.server.previewDag".equals(command) || arguments.size() != 2 )
return null;
var uri = getJsonString(arguments.get(0));
log.debug(String.format("textDocument/executeCommand %s %s", command, arguments.toString()));
var service = getLanguageService(uri);
if( service == null )
return null;
return service.executeCommand(command, arguments);
});
}
private String getJsonString(Object json) {
return json instanceof JsonPrimitive jp ? jp.getAsString() : null;
}
@Override
public CompletableFuture<Either<List<? extends SymbolInformation>, List<? extends WorkspaceSymbol>>> symbol(WorkspaceSymbolParams params) {
return CompletableFutures.computeAsync((cancelChecker) -> {
cancelChecker.checkCanceled();
log.debug("workspace/symbol " + params.getQuery());
var result = new ArrayList<WorkspaceSymbol>();
for( var scriptService : scriptServices.values() )
result.addAll(scriptService.symbol(params));
return Either.forRight(result);
});
}
// -- INTERNAL
private void addWorkspaceFolder(String name, String uri) {
workspaceRoots.put(name, uri);
var scriptService = new ScriptService();
scriptService.connect(client);
scriptServices.put(name, scriptService);
var configService = new ConfigService();
configService.connect(client);
configServices.put(name, configService);
}
private String relativePath(String uri) {
return workspaceRoots.entrySet().stream()
.filter((entry) -> uri.startsWith(entry.getValue()))
.findFirst()
.map((entry) -> {
var name = entry.getKey();
var root = entry.getValue();
var prefix = (DEFAULT_WORKSPACE_FOLDER_NAME.equals(name)) ? "" : name + ":";
return uri.replace(root, prefix);
})
.orElse(uri);
}
private LanguageService getLanguageService(String uri) {
var service = Optional.ofNullable(getLanguageService0(uri, scriptServices)).orElse(getLanguageService0(uri, configServices));
if( service == null ) {
log.debug("No language service found for file: " + uri);
return null;
}
var path = Path.of(URI.create(uri));
if( PathUtils.isPathExcluded(path, excludePatterns) )
return null;
return service;
}
private LanguageService getLanguageService0(String uri, Map<String, LanguageService> services) {
var service = workspaceRoots.entrySet().stream()
.filter((entry) -> uri.startsWith(entry.getValue()))
.findFirst()
.map((entry) -> services.get(entry.getKey()))
.orElse(services.get(DEFAULT_WORKSPACE_FOLDER_NAME));
if( service == null || !service.matchesFile(uri) )
return null;
return service;
}
private List<String> getJsonStringArray(Object json, String path) {
var value = getJsonElement(json, path);
if( value == null || !value.isJsonArray() )
return null;
var result = new ArrayList<String>();
for( var el : value.getAsJsonArray() ) {
try {
result.add(el.getAsJsonPrimitive().getAsString());
}
catch( AssertionError e ) {
continue;
}
}
return result;
}
private Boolean getJsonBoolean(Object json, String path) {
var value = getJsonElement(json, path);
if( value == null || !value.isJsonPrimitive() )
return null;
var result = value.getAsJsonPrimitive();
if( !result.isBoolean() )
return null;
return result.getAsBoolean();
}
private JsonElement getJsonElement(Object json, String path) {
if( !(json instanceof JsonObject) )
return null;
JsonObject object = (JsonObject) json;
var names = path.split("\\.");
for( int i = 0; i < names.length - 1; i++ ) {
var scope = names[i];
if( !object.has(scope) || !object.get(scope).isJsonObject() )
return null;
object = object.get(scope).getAsJsonObject();
}
var property = names[names.length - 1];
if( !object.has(property) )
return null;
return object.get(property);
}
}