-
Notifications
You must be signed in to change notification settings - Fork 3
/
AcceleoTextDocumentService.java
537 lines (482 loc) · 21.2 KB
/
AcceleoTextDocumentService.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
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
/*******************************************************************************
* Copyright (c) 2020, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ls.services.textdocument;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import org.eclipse.acceleo.aql.completion.AcceleoCompletor;
import org.eclipse.acceleo.aql.completion.proposals.AcceleoCompletionProposal;
import org.eclipse.acceleo.aql.ls.AcceleoLanguageServer;
import org.eclipse.acceleo.aql.ls.common.AcceleoLanguageServerPositionUtils;
import org.eclipse.acceleo.aql.ls.common.AcceleoLanguageServerServicesUtils;
import org.eclipse.acceleo.aql.ls.services.exceptions.LanguageServerProtocolException;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoProject;
import org.eclipse.acceleo.aql.ls.services.workspace.AcceleoWorkspace;
import org.eclipse.acceleo.aql.outline.AcceleoOutliner;
import org.eclipse.acceleo.aql.outline.AcceleoSymbol;
import org.eclipse.acceleo.aql.validation.IAcceleoValidationResult;
import org.eclipse.acceleo.query.runtime.namespace.workspace.IQueryWorkspaceQualifiedNameResolver;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionContext;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.DeclarationParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameParams;
import org.eclipse.lsp4j.PrepareRenameResult;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.ReferenceParams;
import org.eclipse.lsp4j.RenameParams;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WillSaveTextDocumentParams;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.CompletableFutures;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.jsonrpc.messages.Either3;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageClientAware;
import org.eclipse.lsp4j.services.TextDocumentService;
/**
* The {@link TextDocumentService} implementation for Acceleo.
*
* @author Florent Latombe
*/
public class AcceleoTextDocumentService implements TextDocumentService, LanguageClientAware {
/**
* {@link Map} of the opened documents, uniquely identified by their {@link URI}.
*/
private final Map<URI, AcceleoTextDocument> openedDocumentsIndex = new HashMap<>();
/**
* The owner {@link AcceleoLanguageServer} of this service.
*/
private final AcceleoLanguageServer server;
/**
* The current client.
*/
private LanguageClient languageClient;
/**
* Creates a new {@link AcceleoTextDocumentService}.
*
* @param acceleoLanguageServer
* the (non-{@code null}) owning {@link AcceleoLanguageServer}.
*/
public AcceleoTextDocumentService(AcceleoLanguageServer acceleoLanguageServer) {
this.server = Objects.requireNonNull(acceleoLanguageServer);
}
// LanguageClientAware API.
@Override
public void connect(LanguageClient newLanguageClient) {
this.languageClient = newLanguageClient;
this.openedDocumentsIndex.clear();
}
////
/**
* Disconnects from the {@link LanguageClient}.
*/
public void disconnect() {
this.languageClient = null;
}
// Mandatory TextDocumentService API.
@Override
public void didOpen(DidOpenTextDocumentParams params) {
final AcceleoWorkspace workspace = server.getWorkspace();
synchronized(workspace) {
final URI openedDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
AcceleoTextDocument openedAcceleoTextDocument = this.server.getWorkspace().getDocument(
openedDocumentUri);
// the workspace might not be synchronized yet, try to add the document
if (openedAcceleoTextDocument == null) {
final AcceleoProject project = workspace.getProject(openedDocumentUri);
final IQueryWorkspaceQualifiedNameResolver resolver = workspace.getResolver(project);
final URI binaryURI = resolver.getBinaryURI(openedDocumentUri);
workspace.addResource(project, binaryURI);
openedAcceleoTextDocument = workspace.getDocument(openedDocumentUri);
if (openedAcceleoTextDocument != null) {
openedAcceleoTextDocument.validateAndPublishResults();
}
}
if (openedAcceleoTextDocument == null) {
throw new IllegalStateException("Could not find the Acceleo Text Document at URI "
+ openedDocumentUri);
} else {
open(openedDocumentUri, openedAcceleoTextDocument);
openedAcceleoTextDocument.open(params.getTextDocument().getText());
}
}
}
public void open(URI openedDocumentUri, AcceleoTextDocument acceleoTextDocument) {
this.openedDocumentsIndex.put(openedDocumentUri, acceleoTextDocument);
}
public void close(URI closedDocumentUri) {
this.openedDocumentsIndex.remove(closedDocumentUri);
}
@Override
public void didChange(DidChangeTextDocumentParams params) {
final URI changedDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
this.checkDocumentIsOpened(changedDocumentUri);
final List<TextDocumentContentChangeEvent> textDocumentContentchangeEvents = params
.getContentChanges();
final AcceleoTextDocument changedAcceleoTextDocument = this.server.getWorkspace().getDocument(
changedDocumentUri);
changedAcceleoTextDocument.applyChanges(textDocumentContentchangeEvents);
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
final URI closedDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(closedDocumentUri);
final AcceleoTextDocument closedAcceleoTextDocument = this.server.getWorkspace().getDocument(
closedDocumentUri);
if (closedAcceleoTextDocument == null) {
throw new IllegalStateException("Could not find the Acceleo Text Document at URI "
+ closedDocumentUri);
} else {
closedAcceleoTextDocument.close();
close(closedDocumentUri);
}
}
@Override
public void didSave(DidSaveTextDocumentParams params) {
// the Eclipse workspace will notified the change
}
////
/**
* Retrieves the latest {@link IAcceleoValidationResult} of the given {@link AcceleoTextDocument} and
* publishes them to the client if there is one.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
*/
public void publishValidationResults(AcceleoTextDocument acceleoTextDocument) {
if (this.languageClient != null) {
final IAcceleoValidationResult validationResults = acceleoTextDocument
.getAcceleoValidationResults();
final List<Diagnostic> diagnosticsToPublish = AcceleoLanguageServerServicesUtils.transform(
validationResults, acceleoTextDocument.getContents());
final URI sourceURI = acceleoTextDocument.getProject().getResolver().getSourceURI(
acceleoTextDocument.getModuleQualifiedName());
if (sourceURI != null) {
this.languageClient.publishDiagnostics(new PublishDiagnosticsParams(sourceURI.toASCIIString(),
diagnosticsToPublish));
}
}
}
// Implementation of the various capabilities declared by the {@link AcceleoLanguageServer}.
/**
* Checks that this service knows of an open document with the given URI. Otherwise, a
* {@link LanguageServerProtocolException} is thrown because we are not supposed to receive requests on
* open documents before the document is opened.
*
* @param documentUri
* the {@link URI} of the document.
*/
protected void checkDocumentIsOpened(URI documentUri) {
if (!this.openedDocumentsIndex.containsKey(documentUri)) {
throw new LanguageServerProtocolException("Received a notification for document \"" + documentUri
+ "\" but it has not previously been opened. This should never happen.");
}
}
@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(
CompletionParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return completion(acceleoTextDocument, position);
}
/**
* Provides the completion for a {@link Position} in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @return the asynchronous computation of the completion proposals provided by an
* {@link AcceleoCompletor}.
*/
private static CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(
AcceleoTextDocument acceleoTextDocument, Position position) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
// Acceleo provides an API to access completion proposals.
final AcceleoCompletor acceleoCompletor = new AcceleoCompletor(System.lineSeparator());
final String source = acceleoTextDocument.getContents();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
source);
final List<AcceleoCompletionProposal> completionProposals = acceleoCompletor.getProposals(
acceleoTextDocument.getQueryEnvironment(), acceleoTextDocument
.getFileNameWithoutExtension(), source, atIndex);
canceler.checkCanceled();
final CompletionList res = new CompletionList();
final List<CompletionItem> completionItems = AcceleoLanguageServerServicesUtils.transform(
completionProposals);
res.getItems().addAll(completionItems);
res.setIsIncomplete(true);
canceler.checkCanceled();
return Either.forRight(res);
});
}
@Override
public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem unresolved) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
// For now, the completion already provides fully-resolved items.
return unresolved;
});
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> declaration(
DeclarationParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return declaration(acceleoTextDocument, position);
}
/**
* Provides the "go to declaration" results for a {@link Position} in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @return the asynchronous computation of the "go to declaration" proposals.
*/
private CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> declaration(
AcceleoTextDocument acceleoTextDocument, Position position) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
acceleoTextDocument.getContents());
canceler.checkCanceled();
return Either.forRight(acceleoTextDocument.getDeclarationLocations(atIndex, false));
});
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
DefinitionParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return definition(acceleoTextDocument, position);
}
/**
* Provides the "go to definition" results for a {@link Position} in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @return the asynchronous computation of the "go to definition" proposals.
*/
private CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(
AcceleoTextDocument acceleoTextDocument, Position position) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
acceleoTextDocument.getContents());
canceler.checkCanceled();
return Either.forRight(acceleoTextDocument.getDefinitionLocations(atIndex));
});
}
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(
DocumentSymbolParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
return documentSymbol(acceleoTextDocument);
}
/**
* Provides all the symbols (templates, queries, etc.) defined in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @return the asynchronous computation of all the symbols defined in the document.
*/
private static CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(
AcceleoTextDocument acceleoTextDocument) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final List<Either<SymbolInformation, DocumentSymbol>> documentSymbols;
if (acceleoTextDocument.getAcceleoValidationResults() != null) {
// Acceleo provides an API to access all defined symbols
final AcceleoOutliner acceleoOutliner = new AcceleoOutliner();
List<AcceleoSymbol> acceleoSymbols = acceleoOutliner.getAllDeclaredSymbols(acceleoTextDocument
.getAcceleoValidationResults());
canceler.checkCanceled();
documentSymbols = acceleoSymbols.stream().map(
acceleoSymbol -> AcceleoLanguageServerServicesUtils.transform(acceleoSymbol,
acceleoTextDocument.getContents())).map(
Either::<SymbolInformation, DocumentSymbol> forRight).collect(
Collectors.toList());
} else {
documentSymbols = Collections.emptyList();
}
canceler.checkCanceled();
return documentSymbols;
});
}
////
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return references(acceleoTextDocument, position);
}
/**
* Provides the "go to definition" results for a {@link Position} in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @return the asynchronous computation of the "go to definition" proposals.
*/
private CompletableFuture<List<? extends Location>> references(AcceleoTextDocument acceleoTextDocument,
Position position) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
acceleoTextDocument.getContents());
final List<Location> locations = acceleoTextDocument.getReferencesLocations(atIndex, true);
canceler.checkCanceled();
return locations;
});
}
@Override
public CompletableFuture<List<TextEdit>> willSaveWaitUntil(WillSaveTextDocumentParams params) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
// TODO we can return a list of text edit here for organize import or formating on save.
return Collections.emptyList();
});
}
@Override
public CompletableFuture<Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior>> prepareRename(
PrepareRenameParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return prepareRename(acceleoTextDocument, position);
}
/**
* Provides the rename results for a {@link Position} in a {@link AcceleoTextDocument}.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @return the asynchronous computation of the "go to definition" proposals.
*/
private CompletableFuture<Either3<Range, PrepareRenameResult, PrepareRenameDefaultBehavior>> prepareRename(
AcceleoTextDocument acceleoTextDocument, Position position) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
acceleoTextDocument.getContents());
final PrepareRenameResult prepareRenameResult = acceleoTextDocument.getPrepareRenameResult(
atIndex);
canceler.checkCanceled();
return Either3.forSecond(prepareRenameResult);
});
}
@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Position position = params.getPosition();
return rename(acceleoTextDocument, position, params.getNewName());
}
/**
* Gets the {@link WorkspaceEdit} corresponding to the rename operation.
*
* @param acceleoTextDocument
* the (non-{@code null}) {@link AcceleoTextDocument}.
* @param position
* the (non-{@code null}) {@link Position}.
* @param newName
* the (non-{@code null}) new name.
* @return
*/
private CompletableFuture<WorkspaceEdit> rename(AcceleoTextDocument acceleoTextDocument,
Position position, String newName) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final int atIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(position,
acceleoTextDocument.getContents());
final Map<String, List<TextEdit>> changes = acceleoTextDocument.getRenames(atIndex, newName);
canceler.checkCanceled();
return new WorkspaceEdit(changes);
});
}
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
final URI textDocumentUri = AcceleoLanguageServerServicesUtils.toUri(params.getTextDocument()
.getUri());
checkDocumentIsOpened(textDocumentUri);
final AcceleoTextDocument acceleoTextDocument = this.openedDocumentsIndex.get(textDocumentUri);
final Range position = params.getRange();
final CodeActionContext context = params.getContext();
return codeAction(acceleoTextDocument, position, context);
}
private CompletableFuture<List<Either<Command, CodeAction>>> codeAction(
AcceleoTextDocument acceleoTextDocument, Range range, CodeActionContext context) {
return CompletableFutures.computeAsync(canceler -> {
canceler.checkCanceled();
final int atStartIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getStart(), acceleoTextDocument.getContents());
final int atEndIndex = AcceleoLanguageServerPositionUtils.getCorrespondingCharacterIndex(range
.getEnd(), acceleoTextDocument.getContents());
final List<Either<Command, CodeAction>> codeActions = acceleoTextDocument.getCodeActions(
atStartIndex, atEndIndex, context);
canceler.checkCanceled();
return codeActions;
});
}
}