-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLanguageService.java
More file actions
496 lines (417 loc) · 16.7 KB
/
LanguageService.java
File metadata and controls
496 lines (417 loc) · 16.7 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
/*
* Copyright 2024-2025, 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.services;
import java.io.IOException;
import java.net.URI;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import nextflow.lsp.ast.ASTNodeCache;
import nextflow.lsp.file.FileCache;
import nextflow.lsp.util.DebouncingExecutor;
import nextflow.lsp.util.LanguageServerUtils;
import nextflow.lsp.util.Logger;
import nextflow.lsp.util.ProgressNotification;
import nextflow.lsp.util.Positions;
import nextflow.script.control.ParanoidWarning;
import nextflow.script.control.RelatedInformationAware;
import nextflow.script.formatter.FormattingOptions;
import nextflow.util.PathUtils;
import org.eclipse.lsp4j.CallHierarchyIncomingCall;
import org.eclipse.lsp4j.CallHierarchyItem;
import org.eclipse.lsp4j.CallHierarchyOutgoingCall;
import org.eclipse.lsp4j.CallHierarchyPrepareParams;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticRelatedInformation;
import org.eclipse.lsp4j.DiagnosticSeverity;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentLinkParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.SemanticTokensParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.WorkspaceSymbol;
import org.eclipse.lsp4j.WorkspaceSymbolParams;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;
/**
* Base language service which handles document updates,
* publishes diagnostics, and provides language features.
*
* @author Ben Sherman <bentshermann@gmail.com>
*/
public abstract class LanguageService {
private static final long DEBOUNCE_MILLIS = 1_000;
private static Logger log = Logger.getInstance();
private String rootUri;
private LanguageClient client;
private FileCache fileCache = new FileCache();
private DebouncingExecutor updateExecutor;
public LanguageService(String rootUri) {
this.rootUri = rootUri;
this.updateExecutor = new DebouncingExecutor(DEBOUNCE_MILLIS, this::update);
}
public abstract boolean matchesFile(String uri);
protected abstract ASTNodeCache getAstCache();
protected CallHierarchyProvider getCallHierarchyProvider() { return null; }
protected CodeLensProvider getCodeLensProvider() { return null; }
protected CompletionProvider getCompletionProvider(int maxItems, boolean extended) { return null; }
protected DefinitionProvider getDefinitionProvider() { return null; }
protected FormattingProvider getFormattingProvider() { return null; }
protected HoverProvider getHoverProvider() { return null; }
protected LinkProvider getLinkProvider() { return null; }
protected ReferenceProvider getReferenceProvider() { return null; }
protected RenameProvider getRenameProvider() { return null; }
protected SemanticTokensProvider getSemanticTokensProvider() { return null; }
protected SymbolProvider getSymbolProvider() { return null; }
private volatile boolean initialized;
private volatile boolean scanned;
private volatile LanguageServerConfiguration configuration;
public void initialize(LanguageServerConfiguration configuration) {
synchronized (this) {
this.initialized = false;
this.scanned = false;
this.configuration = configuration;
clearDiagnostics();
getAstCache().clear();
this.initialized = true;
}
}
public void connect(LanguageClient client) {
this.client = client;
}
// -- NOTIFICATIONS
public void didOpen(DidOpenTextDocumentParams params) {
fileCache.didOpen(params);
updateLater();
}
public void didChange(DidChangeTextDocumentParams params) {
fileCache.didChange(params);
updateLater();
}
public void didClose(DidCloseTextDocumentParams params) {
fileCache.didClose(params);
updateLater();
}
// --- REQUESTS
public List<CallHierarchyItem> prepareCallHierarchy(CallHierarchyPrepareParams params) {
var provider = getCallHierarchyProvider();
if( provider == null )
return Collections.emptyList();
return provider.prepare(params.getTextDocument(), params.getPosition());
}
public List<CallHierarchyIncomingCall> callHierarchyIncomingCalls(CallHierarchyItem item) {
var provider = getCallHierarchyProvider();
if( provider == null )
return Collections.emptyList();
return provider.incomingCalls(item);
}
public List<CallHierarchyOutgoingCall> callHierarchyOutgoingCalls(CallHierarchyItem item) {
var provider = getCallHierarchyProvider();
if( provider == null )
return Collections.emptyList();
return provider.outgoingCalls(item);
}
public List<CodeLens> codeLens(CodeLensParams params) {
var provider = getCodeLensProvider();
if( provider == null )
return Collections.emptyList();
awaitUpdate();
return provider.codeLens(params.getTextDocument());
}
public Either<List<CompletionItem>, CompletionList> completion(CompletionParams params, int maxItems, boolean extended) {
var provider = getCompletionProvider(maxItems, extended);
if( provider == null )
return Either.forLeft(Collections.emptyList());
updateNow();
return provider.completion(params.getTextDocument(), params.getPosition());
}
public Either<List<? extends Location>, List<? extends LocationLink>> definition(DefinitionParams params) {
var provider = getDefinitionProvider();
if( provider == null )
return Either.forLeft(Collections.emptyList());
return provider.definition(params.getTextDocument(), params.getPosition());
}
public List<DocumentLink> documentLink(DocumentLinkParams params) {
var provider = getLinkProvider();
if( provider == null )
return Collections.emptyList();
awaitUpdate();
return provider.documentLink(params.getTextDocument());
}
public List<Either<SymbolInformation, DocumentSymbol>> documentSymbol(DocumentSymbolParams params) {
var provider = getSymbolProvider();
if( provider == null )
return Collections.emptyList();
awaitUpdate();
return provider.documentSymbol(params.getTextDocument());
}
public Object executeCommand(String command, List<Object> arguments, LanguageServerConfiguration configuration) {
return null;
}
public List<? extends TextEdit> formatting(URI uri, FormattingOptions options) {
var provider = getFormattingProvider();
if( provider == null )
return Collections.emptyList();
updateNow();
return provider.formatting(uri, options);
}
public Hover hover(HoverParams params) {
var provider = getHoverProvider();
if( provider == null )
return null;
return provider.hover(params.getTextDocument(), params.getPosition());
}
public List<? extends Location> references(ReferenceParams params) {
var provider = getReferenceProvider();
if( provider == null )
return Collections.emptyList();
return provider.references(params.getTextDocument(), params.getPosition(), params.getContext().isIncludeDeclaration());
}
public WorkspaceEdit rename(RenameParams params) {
var provider = getRenameProvider();
if( provider == null )
return null;
return provider.rename(params.getTextDocument(), params.getPosition(), params.getNewName());
}
public SemanticTokens semanticTokensFull(SemanticTokensParams params) {
var provider = getSemanticTokensProvider();
if( provider == null )
return null;
awaitUpdate();
return provider.semanticTokensFull(params.getTextDocument());
}
public List<? extends WorkspaceSymbol> symbol(WorkspaceSymbolParams params) {
var provider = getSymbolProvider();
if( provider == null )
return Collections.emptyList();
return provider.symbol(params.getQuery());
}
// --- INTERNAL
private Lock updateLock = new ReentrantLock();
private Condition updateCondition = updateLock.newCondition();
private volatile boolean awaitingUpdate;
protected void updateLater() {
awaitingUpdate = true;
updateExecutor.executeLater();
}
protected void updateNow() {
updateExecutor.executeNow();
}
protected void awaitUpdate() {
if( !awaitingUpdate )
return;
updateLock.lock();
try {
updateCondition.await(DEBOUNCE_MILLIS * 2, TimeUnit.MILLISECONDS);
}
catch( InterruptedException e ) {
}
finally {
updateLock.unlock();
}
}
/**
* Re-compile any changed files.
*/
protected void update() {
synchronized (this) {
update0();
}
updateLock.lock();
try {
updateCondition.signalAll();
awaitingUpdate = false;
}
finally {
updateLock.unlock();
}
}
private void update0() {
if( !initialized )
return;
var astCache = getAstCache();
var uris = fileCache.removeChangedFiles();
if( !scanned ) {
if( uris.isEmpty() ) {
uris = getWorkspaceFiles();
astCache.clear();
this.scanned = true;
}
else {
updateLater();
}
}
if( log.isDebugEnabled() ) {
var builder = new StringBuilder();
builder.append("update\n");
uris.forEach((uri) -> {
builder.append("- ");
builder.append(uri.toString().replace(rootUri, "."));
builder.append('\n');
});
log.debug(builder.toString());
}
// Create progress notification for multi-file updates
ProgressNotification progress = null;
if( uris.size() > 1 && client != null ) {
var token = "indexing-" + (rootUri != null ? rootUri.hashCode() : "default");
progress = new ProgressNotification(client, token);
progress.create();
progress.begin(String.format("Indexing %d files...", uris.size()));
}
try {
final var progressNotification = progress;
var changedUris = astCache.update(uris, fileCache,
progressNotification != null
? (current, total) -> progressNotification.update(
String.format("Indexing: %d / %d files", current, total),
current * 100 / total)
: null);
publishDiagnostics(changedUris);
}
catch( Throwable e ) {
System.err.println(e);
e.printStackTrace();
}
finally {
if( progress != null ) {
progress.end();
}
}
}
/**
* Scan the workspace for files that can be managed by the language service.
*
* If there is no workspace root, use the set of open files instead.
*/
protected Set<URI> getWorkspaceFiles() {
if( rootUri == null ) {
return fileCache.getOpenFiles();
}
try {
var result = new HashSet<URI>();
PathUtils.visitFiles(
Path.of(URI.create(rootUri)),
(path) -> !PathUtils.isExcluded(path, configuration.excludePatterns()),
(path) -> {
if( matchesFile(path.toString()) )
result.add(path.toUri());
});
return result;
}
catch( IOException e ) {
log.error("Failed to query workspace files: " + rootUri + " -- cause: " + e.toString());
return Collections.emptySet();
}
}
/**
* Publish diagnostics for a set of compilation errors.
*
* @param changedUris
*/
protected void publishDiagnostics(Set<URI> changedUris) {
var astCache = getAstCache();
changedUris.forEach((uri) -> {
var diagnostics = new ArrayList<Diagnostic>();
for( var error : astCache.getErrors(uri) ) {
if( !configuration.errorReportingMode().isRelevant(error) )
continue;
var message = error.getOriginalMessage();
var range = LanguageServerUtils.errorToRange(error);
if( range == null ) {
log.error(uri + ": invalid range for error: " + message);
continue;
}
var diagnostic = new Diagnostic(range, message, DiagnosticSeverity.Error, "nextflow");
if( error instanceof RelatedInformationAware ria )
diagnostic.setRelatedInformation(relatedInformation(ria, uri));
diagnostics.add(diagnostic);
}
for( var warning : astCache.getWarnings(uri) ) {
if( !configuration.errorReportingMode().isRelevant(warning) )
continue;
var message = warning.getMessage();
var range = LanguageServerUtils.warningToRange(warning);
if( range == null ) {
log.error(uri + ": invalid range for warning: " + message);
continue;
}
var severity = warning instanceof ParanoidWarning
? DiagnosticSeverity.Information
: DiagnosticSeverity.Warning;
var diagnostic = new Diagnostic(range, message, severity, "nextflow");
if( warning instanceof RelatedInformationAware ria )
diagnostic.setRelatedInformation(relatedInformation(ria, uri));
diagnostics.add(diagnostic);
}
var params = new PublishDiagnosticsParams(uri.toString(), diagnostics);
client.publishDiagnostics(params);
});
}
private static List<DiagnosticRelatedInformation> relatedInformation(RelatedInformationAware ria, URI uri) {
var otherNode = ria.getOtherNode();
if( otherNode != null ) {
var result = new ArrayList<DiagnosticRelatedInformation>();
var location = LanguageServerUtils.astNodeToLocation(otherNode, uri);
var dri = new DiagnosticRelatedInformation(location, ria.getOtherMessage());
result.add(dri);
return result;
}
return null;
}
/**
* Clear diagnostics when the language service is shut down.
*/
public void clearDiagnostics() {
getAstCache().getUris().forEach((uri) -> {
var params = new PublishDiagnosticsParams(uri.toString(), Collections.emptyList());
client.publishDiagnostics(params);
});
}
}