-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLanguageService.java
416 lines (344 loc) · 14 KB
/
LanguageService.java
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
/*
* 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.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.compiler.Compiler;
import nextflow.lsp.compiler.SyntaxWarning;
import nextflow.lsp.file.FileCache;
import nextflow.lsp.file.PathUtils;
import nextflow.lsp.services.util.CustomFormattingOptions;
import nextflow.lsp.util.DebouncingExecutor;
import nextflow.lsp.util.LanguageServerUtils;
import nextflow.lsp.util.Logger;
import nextflow.lsp.util.Positions;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.codehaus.groovy.syntax.SyntaxException;
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.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.SignatureHelp;
import org.eclipse.lsp4j.SignatureHelpParams;
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 <[email protected]>
*/
public abstract class LanguageService {
private static final int DEBOUNCE_MILLIS = 1_000;
private static final Object DEBOUNCE_KEY = new Object();
private static Logger log = Logger.getInstance();
private LanguageClient client;
private FileCache fileCache = new FileCache();
private DebouncingExecutor updateExecutor;
public LanguageService() {
this.updateExecutor = new DebouncingExecutor(DEBOUNCE_MILLIS, (key) -> update());
}
public abstract boolean matchesFile(String uri);
protected abstract ASTNodeCache getAstCache();
protected CallHierarchyProvider getCallHierarchyProvider() { return null; }
protected CodeLensProvider getCodeLensProvider() { return null; }
protected CompletionProvider getCompletionProvider() { 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 SignatureHelpProvider getSignatureHelpProvider() { return null; }
protected SymbolProvider getSymbolProvider() { return null; }
private volatile boolean initialized;
private volatile boolean suppressWarnings;
public void initialize(String rootUri, List<String> excludes, boolean suppressWarnings) {
synchronized (this) {
this.initialized = false;
this.suppressWarnings = suppressWarnings;
var uris = rootUri != null
? getWorkspaceFiles(rootUri, excludes)
: fileCache.getOpenFiles();
var astCache = getAstCache();
astCache.clear();
astCache.update(uris, fileCache);
publishDiagnostics(astCache.getErrors());
this.initialized = true;
}
}
protected Set<URI> getWorkspaceFiles(String rootUri, List<String> excludes) {
try {
var root = Path.of(URI.create(rootUri));
var result = new HashSet<URI>();
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path path, BasicFileAttributes attrs) {
return PathUtils.isPathExcluded(path, excludes)
? FileVisitResult.SKIP_SUBTREE
: FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {
if( matchesFile(path.toString()) && !PathUtils.isPathExcluded(path, excludes) )
result.add(path.toUri());
return FileVisitResult.CONTINUE;
}
});
return result;
}
catch( IOException e ) {
log.error("Failed to query workspace files: " + rootUri + " -- cause: " + e.toString());
return Collections.emptySet();
}
}
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) {
var provider = getCompletionProvider();
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) {
return null;
}
public List<? extends TextEdit> formatting(URI uri, CustomFormattingOptions 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 SignatureHelp signatureHelp(SignatureHelpParams params) {
var provider = getSignatureHelpProvider();
if( provider == null )
return null;
updateNow();
return provider.signatureHelp(params.getTextDocument(), params.getPosition(), params.getContext());
}
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.submit(DEBOUNCE_KEY);
}
protected void updateNow() {
updateExecutor.executeNow(DEBOUNCE_KEY);
}
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) {
if( initialized ) {
var uris = fileCache.removeChangedFiles();
log.debug("update " + DefaultGroovyMethods.join(uris, " , "));
var astCache = getAstCache();
astCache.update(uris, fileCache);
publishDiagnostics(astCache.getErrors());
}
}
updateLock.lock();
try {
updateCondition.signalAll();
awaitingUpdate = false;
}
finally {
updateLock.unlock();
}
}
/**
* Publish diagnostics for a set of compilation errors.
*
* @param errorsByUri
*/
protected void publishDiagnostics(Map<URI, List<SyntaxException>> errorsByUri) {
errorsByUri.forEach((uri, errors) -> {
var diagnostics = new ArrayList<Diagnostic>();
for( var error : errors ) {
var range = LanguageServerUtils.syntaxExceptionToRange(error);
if( range == null ) {
log.error(uri + ": invalid range for error: " + error.getMessage());
continue;
}
var message = error.getMessage();
var severity = error instanceof SyntaxWarning
? DiagnosticSeverity.Warning
: DiagnosticSeverity.Error;
if( suppressWarnings && severity == DiagnosticSeverity.Warning )
continue;
var diagnostic = new Diagnostic(range, message, severity, "nextflow");
diagnostics.add(diagnostic);
}
var params = new PublishDiagnosticsParams(uri.toString(), diagnostics);
client.publishDiagnostics(params);
});
}
/**
* Clear diagnostics when the language service is shut down.
*/
public void clearDiagnostics() {
getAstCache().getErrors().forEach((uri, errors) -> {
var params = new PublishDiagnosticsParams(uri.toString(), Collections.emptyList());
client.publishDiagnostics(params);
});
}
}