forked from camel-tooling/camel-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCamelTextDocumentService.java
338 lines (301 loc) · 15.2 KB
/
CamelTextDocumentService.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
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* https://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 com.github.cameltooling.lsp.internal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import org.apache.camel.catalog.CamelCatalog;
import org.apache.camel.catalog.DefaultCamelCatalog;
import org.apache.camel.catalog.RuntimeProvider;
import org.apache.camel.catalog.maven.MavenVersionManager;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CodeLensParams;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.CompletionParams;
import org.eclipse.lsp4j.DefinitionParams;
import org.eclipse.lsp4j.DidChangeTextDocumentParams;
import org.eclipse.lsp4j.DidCloseTextDocumentParams;
import org.eclipse.lsp4j.DidOpenTextDocumentParams;
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.DocumentHighlightParams;
import org.eclipse.lsp4j.DocumentOnTypeFormattingParams;
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.DocumentSymbolParams;
import org.eclipse.lsp4j.FoldingRange;
import org.eclipse.lsp4j.FoldingRangeRequestParams;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.HoverParams;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.LocationLink;
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.TextDocumentContentChangeEvent;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.TextDocumentItem;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.WorkspaceEdit;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.TextDocumentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.cameltooling.lsp.internal.catalog.runtimeprovider.CamelRuntimeProvider;
import com.github.cameltooling.lsp.internal.catalog.util.CamelKafkaConnectorCatalogManager;
import com.github.cameltooling.lsp.internal.catalog.util.KameletsCatalogManager;
import com.github.cameltooling.lsp.internal.codeactions.CodeActionProcessor;
import com.github.cameltooling.lsp.internal.completion.CamelEndpointCompletionProcessor;
import com.github.cameltooling.lsp.internal.completion.CamelPropertiesCompletionProcessor;
import com.github.cameltooling.lsp.internal.completion.modeline.CamelKModelineCompletionprocessor;
import com.github.cameltooling.lsp.internal.definition.DefinitionProcessor;
import com.github.cameltooling.lsp.internal.diagnostic.DiagnosticRunner;
import com.github.cameltooling.lsp.internal.documentsymbol.DocumentSymbolProcessor;
import com.github.cameltooling.lsp.internal.folding.FoldingRangeProcessor;
import com.github.cameltooling.lsp.internal.hover.CamelKModelineHoverProcessor;
import com.github.cameltooling.lsp.internal.hover.CamelPropertiesFileHoverProcessor;
import com.github.cameltooling.lsp.internal.hover.CamelURIHoverProcessor;
import com.github.cameltooling.lsp.internal.parser.CamelKModelineParser;
import com.github.cameltooling.lsp.internal.references.ReferencesProcessor;
import com.github.cameltooling.lsp.internal.settings.JSONUtility;
import com.github.cameltooling.lsp.internal.settings.SettingsManager;
import com.google.gson.Gson;
/**
* @author lhein
*/
public class CamelTextDocumentService implements TextDocumentService {
private static final Logger LOGGER = LoggerFactory.getLogger(CamelTextDocumentService.class);
private Map<String, TextDocumentItem> openedDocuments = new HashMap<>();
private CompletableFuture<CamelCatalog> camelCatalog;
private CamelLanguageServer camelLanguageServer;
private CamelKafkaConnectorCatalogManager camelKafkaConnectorManager = new CamelKafkaConnectorCatalogManager();
private KameletsCatalogManager kameletsCatalogManager = new KameletsCatalogManager();
public CamelTextDocumentService(CamelLanguageServer camelLanguageServer) {
this.camelLanguageServer = camelLanguageServer;
camelCatalog = CompletableFuture.supplyAsync(() -> new DefaultCamelCatalog(true));
}
public void updateCatalog(String camelVersion, String camelCatalogRuntimeProvider, List<Map<?,?>> extraComponents) {
camelCatalog = CompletableFuture.supplyAsync(() -> {
DefaultCamelCatalog catalog = new DefaultCamelCatalog(true);
updateCatalogVersion(camelVersion, catalog);
updateCatalogRuntimeProvider(camelCatalogRuntimeProvider, catalog);
updateCatalogExtraComponents(extraComponents, catalog);
return catalog;
});
}
private void updateCatalogExtraComponents(List<Map<?, ?>> extraComponents, DefaultCamelCatalog catalog) {
if (extraComponents != null) {
for (Map<?,?> extraComponent : extraComponents) {
JSONUtility jsonUtility = new JSONUtility();
Map<?,?> extraComponentTopLevel = jsonUtility.toModel(extraComponent, Map.class);
Map<?,?> componentAttributes = jsonUtility.toModel(extraComponentTopLevel.get("component"), Map.class);
String name = (String) componentAttributes.get("scheme");
String className = (String) componentAttributes.get("javaType");
catalog.addComponent(name, className, new Gson().toJson(extraComponent));
}
}
}
private void updateCatalogRuntimeProvider(String camelCatalogRuntimeProvider, DefaultCamelCatalog catalog) {
if(camelCatalogRuntimeProvider != null && !camelCatalogRuntimeProvider.isEmpty()) {
RuntimeProvider runtimeProvider = CamelRuntimeProvider.getProvider(camelCatalogRuntimeProvider);
if(runtimeProvider != null) {
catalog.setRuntimeProvider(runtimeProvider);
}
}
}
private void updateCatalogVersion(String camelVersion, DefaultCamelCatalog catalog) {
if (camelVersion != null && !camelVersion.isEmpty()) {
catalog.setVersionManager(new MavenVersionManager());
if (!catalog.loadVersion(camelVersion)) {
LOGGER.warn("Cannot load Camel catalog with version {}", camelVersion);
}
}
}
@Override
public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completion(CompletionParams completionParams) {
String uri = completionParams.getTextDocument().getUri();
LOGGER.info("completion: {}", uri);
TextDocumentItem textDocumentItem = openedDocuments.get(uri);
if (textDocumentItem != null) {
if (uri.endsWith(".properties")){
return new CamelPropertiesCompletionProcessor(textDocumentItem, getCamelCatalog(), getCamelKafkaConnectorManager()).getCompletions(completionParams.getPosition(), getSettingsManager(), getKameletsCatalogManager()).thenApply(Either::forLeft);
} else if(new CamelKModelineParser().isOnCamelKModeline(completionParams.getPosition().getLine(), textDocumentItem)){
return new CamelKModelineCompletionprocessor(textDocumentItem, getCamelCatalog()).getCompletions(completionParams.getPosition()).thenApply(Either::forLeft);
} else {
return new CamelEndpointCompletionProcessor(textDocumentItem, getCamelCatalog(), getKameletsCatalogManager()).getCompletions(completionParams.getPosition(), getSettingsManager()).thenApply(Either::forLeft);
}
} else {
LOGGER.warn("The document with uri {} has not been found in opened documents. Cannot provide completion.", uri);
return CompletableFuture.completedFuture(Either.forLeft(Collections.emptyList()));
}
}
@Override
public CompletableFuture<CompletionItem> resolveCompletionItem(CompletionItem unresolved) {
LOGGER.info("resolveCompletionItem: {}", unresolved.getLabel());
return CompletableFuture.completedFuture(unresolved);
}
@Override
public CompletableFuture<Hover> hover(HoverParams hoverParams) {
LOGGER.info("hover: {}", hoverParams.getTextDocument());
String uri = hoverParams.getTextDocument().getUri();
TextDocumentItem textDocumentItem = openedDocuments.get(uri);
if (uri.endsWith(".properties")){
return new CamelPropertiesFileHoverProcessor(textDocumentItem).getHover(hoverParams.getPosition(), getCamelCatalog(), getCamelKafkaConnectorManager(), getKameletsCatalogManager());
} else if(new CamelKModelineParser().isOnCamelKModeline(hoverParams.getPosition().getLine(), textDocumentItem)) {
return new CamelKModelineHoverProcessor(textDocumentItem).getHover(hoverParams.getPosition(), getCamelCatalog());
} else {
return new CamelURIHoverProcessor(textDocumentItem, getCamelCatalog(), getKameletsCatalogManager()).getHover(hoverParams.getPosition());
}
}
@Override
public CompletableFuture<SignatureHelp> signatureHelp(SignatureHelpParams signatureHelpParams) {
LOGGER.info("signatureHelp: {}", signatureHelpParams.getTextDocument());
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<Either<List<? extends Location>, List<? extends LocationLink>>> definition(DefinitionParams params) {
TextDocumentIdentifier textDocument = params.getTextDocument();
LOGGER.info("definition: {}", textDocument);
TextDocumentItem textDocumentItem = openedDocuments.get(textDocument.getUri());
return new DefinitionProcessor(textDocumentItem).getDefinitions(params.getPosition());
}
@Override
public CompletableFuture<List<? extends Location>> references(ReferenceParams params) {
LOGGER.info("references: {}", params.getTextDocument());
return new ReferencesProcessor(this, openedDocuments.get(params.getTextDocument().getUri())).getReferences(params.getPosition());
}
@Override
public CompletableFuture<List<? extends DocumentHighlight>> documentHighlight(DocumentHighlightParams position) {
LOGGER.info("documentHighlight: {}", position.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> documentSymbol(DocumentSymbolParams params) {
LOGGER.info("documentSymbol: {}", params.getTextDocument());
return new DocumentSymbolProcessor(openedDocuments.get(params.getTextDocument().getUri())).getDocumentSymbols();
}
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
LOGGER.info("codeAction: {}", params.getTextDocument());
return new CodeActionProcessor(this).getCodeActions(params);
}
@Override
public CompletableFuture<List<? extends CodeLens>> codeLens(CodeLensParams params) {
LOGGER.info("codeLens: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<CodeLens> resolveCodeLens(CodeLens unresolved) {
LOGGER.info("resolveCodeLens: {}", unresolved.getCommand().getCommand());
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
LOGGER.info("formatting: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
LOGGER.info("rangeFormatting: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<List<? extends TextEdit>> onTypeFormatting(DocumentOnTypeFormattingParams params) {
LOGGER.info("onTypeFormatting: {}", params.getTextDocument());
return CompletableFuture.completedFuture(Collections.emptyList());
}
@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
LOGGER.info("rename: {}", params.getTextDocument());
return CompletableFuture.completedFuture(null);
}
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(FoldingRangeRequestParams params) {
TextDocumentIdentifier textDocument = params.getTextDocument();
LOGGER.info("foldingRange: {}", textDocument);
String uri = textDocument.getUri();
TextDocumentItem textDocumentItem = openedDocuments.get(uri);
return new FoldingRangeProcessor().computeFoldingRanges(textDocumentItem);
}
@Override
public void didOpen(DidOpenTextDocumentParams params) {
TextDocumentItem textDocument = params.getTextDocument();
LOGGER.info("didOpen: {}", textDocument);
openedDocuments.put(textDocument.getUri(), textDocument);
new DiagnosticRunner(getCamelCatalog(), camelLanguageServer).compute(params);
}
@Override
public void didChange(DidChangeTextDocumentParams params) {
LOGGER.info("didChange: {}", params.getTextDocument());
List<TextDocumentContentChangeEvent> contentChanges = params.getContentChanges();
TextDocumentItem textDocumentItem = openedDocuments.get(params.getTextDocument().getUri());
if (!contentChanges.isEmpty()) {
textDocumentItem.setText(contentChanges.get(0).getText());
new DiagnosticRunner(getCamelCatalog(), camelLanguageServer).compute(params);
}
}
@Override
public void didClose(DidCloseTextDocumentParams params) {
LOGGER.info("didClose: {}", params.getTextDocument());
String uri = params.getTextDocument().getUri();
openedDocuments.remove(uri);
/* The rule observed by VS Code servers as explained in LSP specification is to clear the Diagnostic when it is related to a single file.
* https://microsoft.github.io/language-server-protocol/specification#textDocument_publishDiagnostics
* */
new DiagnosticRunner(getCamelCatalog(), camelLanguageServer).clear(uri);
}
@Override
public void didSave(DidSaveTextDocumentParams params) {
LOGGER.info("didSave: {}", params.getTextDocument());
new DiagnosticRunner(getCamelCatalog(), camelLanguageServer).compute(params);
}
public TextDocumentItem getOpenedDocument(String uri) {
return openedDocuments.get(uri);
}
public Collection<TextDocumentItem> getAllOpenedDocuments() {
return openedDocuments.values();
}
/**
* /!\ public for test purpose
* @return a Future of the Camel Catalog
*/
public CompletableFuture<CamelCatalog> getCamelCatalog() {
return camelCatalog;
}
public CamelKafkaConnectorCatalogManager getCamelKafkaConnectorManager() {
return camelKafkaConnectorManager;
}
public void setCamelKafkaConnectorManager(CamelKafkaConnectorCatalogManager camelKafkaConnectorManager) {
this.camelKafkaConnectorManager = camelKafkaConnectorManager;
}
public SettingsManager getSettingsManager() {
return camelLanguageServer.getSettingsManager();
}
public KameletsCatalogManager getKameletsCatalogManager() {
return kameletsCatalogManager;
}
}