-
Notifications
You must be signed in to change notification settings - Fork 27
/
MicroProfileDelegateCommandHandlerForJava.java
487 lines (459 loc) · 20.5 KB
/
MicroProfileDelegateCommandHandlerForJava.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
/*******************************************************************************
* Copyright (c) 2019-2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4mp.jdt.internal.core.ls;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getBoolean;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getCodeActionContext;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getFirst;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getInt;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getObject;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getPosition;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getRange;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getString;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getStringList;
import static org.eclipse.lsp4mp.jdt.internal.core.ls.ArgumentUtils.getTextDocumentIdentifier;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionContext;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolInformation;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4mp.commons.DocumentFormat;
import org.eclipse.lsp4mp.commons.JavaCursorContextKind;
import org.eclipse.lsp4mp.commons.JavaCursorContextResult;
import org.eclipse.lsp4mp.commons.JavaFileInfo;
import org.eclipse.lsp4mp.commons.MicroProfileDefinition;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeActionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeLensParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCompletionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCompletionResult;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDefinitionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsSettings;
import org.eclipse.lsp4mp.commons.MicroProfileJavaFileInfoParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaHoverParams;
import org.eclipse.lsp4mp.commons.codeaction.CodeActionResolveData;
import org.eclipse.lsp4mp.commons.utils.JSONUtility;
import org.eclipse.lsp4mp.jdt.core.PropertiesManagerForJava;
/**
* JDT LS delegate command handler for Java file.
*
* @author Angelo ZERR
*
*/
public class MicroProfileDelegateCommandHandlerForJava extends AbstractMicroProfileDelegateCommandHandler {
private static final String FILE_INFO_COMMAND_ID = "microprofile/java/fileInfo";
private static final String JAVA_CODEACTION_COMMAND_ID = "microprofile/java/codeAction";
private static final String JAVA_CODEACTION_RESOLVE_COMMAND_ID = "microprofile/java/codeActionResolve";
private static final String JAVA_CODELENS_COMMAND_ID = "microprofile/java/codeLens";
private static final String JAVA_COMPLETION_COMMAND_ID = "microprofile/java/completion";
private static final String JAVA_DEFINITION_COMMAND_ID = "microprofile/java/definition";
private static final String JAVA_DIAGNOSTICS_COMMAND_ID = "microprofile/java/diagnostics";
private static final String JAVA_HOVER_COMMAND_ID = "microprofile/java/hover";
private static final String JAVA_WORKSPACE_SYMBOLS_ID = "microprofile/java/workspaceSymbols";
public MicroProfileDelegateCommandHandlerForJava() {
}
@Override
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
switch (commandId) {
case FILE_INFO_COMMAND_ID:
return getFileInfo(arguments, commandId, progress);
case JAVA_CODEACTION_COMMAND_ID:
return getCodeActionForJava(arguments, commandId, progress);
case JAVA_CODEACTION_RESOLVE_COMMAND_ID:
return resolveCodeActionForJava(arguments, commandId, progress);
case JAVA_CODELENS_COMMAND_ID:
return getCodeLensForJava(arguments, commandId, progress);
case JAVA_COMPLETION_COMMAND_ID:
return getCompletionForJava(arguments, commandId, progress);
case JAVA_DEFINITION_COMMAND_ID:
return getDefinitionForJava(arguments, commandId, progress);
case JAVA_DIAGNOSTICS_COMMAND_ID:
return getDiagnosticsForJava(arguments, commandId, progress);
case JAVA_HOVER_COMMAND_ID:
return getHoverForJava(arguments, commandId, progress);
case JAVA_WORKSPACE_SYMBOLS_ID:
return getWorkspaceSymbolsForJava(arguments, commandId, progress);
default:
throw new UnsupportedOperationException(String.format("Unsupported command '%s'!", commandId));
}
}
/**
* Returns the file information (package name, etc) for the given Java file.
*
* @param arguments
* @param commandId
* @param monitor
* @return the file information (package name, etc) for the given Java file.
* @throws CoreException
* @throws JavaModelException
*/
private static JavaFileInfo getFileInfo(List<Object> arguments, String commandId, IProgressMonitor monitor)
throws JavaModelException, CoreException {
// Create java file information parameter
MicroProfileJavaFileInfoParams params = createJavaFileInfoParams(arguments, commandId);
// Return file information from the parameter
return PropertiesManagerForJava.getInstance().fileInfo(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Create the Java file information parameter from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return the Java file information parameter.
*/
private static MicroProfileJavaFileInfoParams createJavaFileInfoParams(List<Object> arguments, String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaFileInfoParams argument!", commandId));
}
// Get project name from the java file URI
String javaFileUri = getString(obj, "uri");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaFileInfoParams.uri (java file URI)!",
commandId));
}
MicroProfileJavaFileInfoParams params = new MicroProfileJavaFileInfoParams();
params.setUri(javaFileUri);
return params;
}
/**
* Returns the code action for the given Java file.
*
* @param arguments
* @param commandId
* @param monitor
* @return the code action for the given Java file.
* @throws CoreException
* @throws JavaModelException
*/
private static List<? extends CodeAction> getCodeActionForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException, CoreException {
// Create java code action parameter
MicroProfileJavaCodeActionParams params = createMicroProfileJavaCodeActionParams(arguments, commandId);
// Return code action from the code action parameter
return PropertiesManagerForJava.getInstance().codeAction(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Create java code action parameter from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return java code action parameter
*/
private static MicroProfileJavaCodeActionParams createMicroProfileJavaCodeActionParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaCodeActionParams argument!", commandId));
}
TextDocumentIdentifier texdDocumentIdentifier = getTextDocumentIdentifier(obj, "textDocument");
if (texdDocumentIdentifier == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaCodeActionParams.texdDocumentIdentifier",
commandId));
}
Range range = getRange(obj, "range");
CodeActionContext context = getCodeActionContext(obj, "context");
boolean resourceOperationSupported = getBoolean(obj, "resourceOperationSupported");
boolean commandConfigurationUpdateSupported = getBoolean(obj, "commandConfigurationUpdateSupported");
boolean resolveSupported = getBoolean(obj, "resolveSupported");
MicroProfileJavaCodeActionParams params = new MicroProfileJavaCodeActionParams();
params.setTextDocument(texdDocumentIdentifier);
params.setRange(range);
params.setContext(context);
params.setResourceOperationSupported(resourceOperationSupported);
params.setCommandConfigurationUpdateSupported(commandConfigurationUpdateSupported);
params.setResolveSupported(resolveSupported);
return params;
}
private static CodeAction resolveCodeActionForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException, CoreException {
// Create java code action parameter
CodeAction unresolved = createMicroProfileJavaCodeActionResolveParams(arguments, commandId);
// Return code action from the code action parameter
return PropertiesManagerForJava.getInstance().resolveCodeAction(unresolved, JDTUtilsLSImpl.getInstance(),
monitor);
}
private static CodeAction createMicroProfileJavaCodeActionResolveParams(List<Object> arguments, String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one CodeAction argument!", commandId));
}
CodeAction codeAction = JSONUtility.toModel(obj, CodeAction.class);
if (codeAction == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one CodeAction argument!", commandId));
}
CodeActionResolveData resolveData = JSONUtility.toModel(codeAction.getData(), CodeActionResolveData.class);
if (resolveData == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with a CodeAction that has CodeActionResolveData!", commandId));
}
codeAction.setData(resolveData);
return codeAction;
}
/**
* Returns the code lenses for the given Java file.
*
* @param arguments
* @param commandId
* @param monitor
* @return the code lenses for the given Java file.
* @throws CoreException
* @throws JavaModelException
*/
private static List<? extends CodeLens> getCodeLensForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException, CoreException {
// Create java code lens parameter
MicroProfileJavaCodeLensParams params = createMicroProfileJavaCodeLensParams(arguments, commandId);
// Return code lenses from the lens parameter
return PropertiesManagerForJava.getInstance().codeLens(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Create java code lens parameter from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return java code lens parameter
*/
private static MicroProfileJavaCodeLensParams createMicroProfileJavaCodeLensParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaCodeLensParams argument!", commandId));
}
String javaFileUri = getString(obj, "uri");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaCodeLensParams.uri (java URI)!",
commandId));
}
MicroProfileJavaCodeLensParams params = new MicroProfileJavaCodeLensParams(javaFileUri);
params.setUrlCodeLensEnabled(getBoolean(obj, "urlCodeLensEnabled"));
params.setCheckServerAvailable(getBoolean(obj, "checkServerAvailable"));
params.setOpenURICommand(getString(obj, "openURICommand"));
params.setLocalServerPort(getInt(obj, "localServerPort"));
return params;
}
/**
* Return the completion result for the given arguments
*
* @param arguments
* @param commandId
* @param monitor
* @return the completion result for the given arguments
* @throws JavaModelException
* @throws CoreException
*/
private static MicroProfileJavaCompletionResult getCompletionForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException, CoreException {
MicroProfileJavaCompletionParams params = createMicroProfileJavaCompletionParams(arguments, commandId);
CompletionList completionList = PropertiesManagerForJava.getInstance().completion(params, JDTUtilsLSImpl.getInstance(), monitor);
JavaCursorContextResult cursorContext = PropertiesManagerForJava.getInstance().javaCursorContext(params, JDTUtilsLSImpl.getInstance(), monitor);
return new MicroProfileJavaCompletionResult(completionList, cursorContext);
}
/**
* Create the completion parameters from the given argument map
*
* @param arguments
* @param commandId
* @return the completion parameters from the given argument map
*/
private static MicroProfileJavaCompletionParams createMicroProfileJavaCompletionParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaCompletionParams argument!", commandId));
}
String javaFileUri = getString(obj, "uri");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaCompletionParams.uri (java URI)!",
commandId));
}
Position position = getPosition(obj, "position");
if (position == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaCompletionParams.position (completion trigger location)!",
commandId));
}
MicroProfileJavaCompletionParams params = new MicroProfileJavaCompletionParams(javaFileUri, position);
return params;
}
/**
* Returns the list o <code>MicroProfileLocationLink</code> for the definition
* described in <code>arguments</code>
*
* @param arguments
* @param commandId
* @param monitor
* @return
* @throws JavaModelException
* @throws CoreException
*/
private static List<MicroProfileDefinition> getDefinitionForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException, CoreException {
// Create java definition parameter
MicroProfileJavaDefinitionParams params = createMicroProfileJavaDefinitionParams(arguments, commandId);
// Return hover info from hover parameter
return PropertiesManagerForJava.getInstance().definition(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Returns the java definition parameters from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return the definition hover parameters
*/
private static MicroProfileJavaDefinitionParams createMicroProfileJavaDefinitionParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaDefinitionParams argument!", commandId));
}
String javaFileUri = getString(obj, "uri");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaDefinitionParams.uri (java URI)!",
commandId));
}
Position hoverPosition = getPosition(obj, "position");
return new MicroProfileJavaDefinitionParams(javaFileUri, hoverPosition);
}
/**
* Returns the publish diagnostics list for a given java file URIs.
*
* @param arguments
* @param commandId
* @param monitor
* @return the publish diagnostics list for a given java file URIs.
* @throws JavaModelException
*/
private static List<PublishDiagnosticsParams> getDiagnosticsForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) throws JavaModelException {
// Create java diagnostics parameter
MicroProfileJavaDiagnosticsParams params = createMicroProfileJavaDiagnosticsParams(arguments, commandId);
// Return diagnostics from parameter
return PropertiesManagerForJava.getInstance().diagnostics(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Returns the java diagnostics parameters from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return the java diagnostics parameters
*/
private static MicroProfileJavaDiagnosticsParams createMicroProfileJavaDiagnosticsParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with one MicroProfileJavaDiagnosticsParams argument!", commandId));
}
List<String> javaFileUri = getStringList(obj, "uris");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaDiagnosticsParams.uri (java URIs)!",
commandId));
}
MicroProfileJavaDiagnosticsSettings settings = null;
Map<String, Object> settingsObj = getObject(obj, "settings");
if (settingsObj != null) {
List<String> patterns = getStringList(settingsObj, "patterns");
settings = new MicroProfileJavaDiagnosticsSettings(patterns);
}
return new MicroProfileJavaDiagnosticsParams(javaFileUri, settings);
}
/**
* Returns the <code>Hover</code> for the hover described in
* <code>arguments</code>
*
* @param arguments
* @param commandId
* @param monitor
* @return
* @throws JavaModelException
* @throws CoreException
*/
private static Hover getHoverForJava(List<Object> arguments, String commandId, IProgressMonitor monitor)
throws JavaModelException, CoreException {
// Create java hover parameter
MicroProfileJavaHoverParams params = createMicroProfileJavaHoverParams(arguments, commandId);
// Return hover info from hover parameter
return PropertiesManagerForJava.getInstance().hover(params, JDTUtilsLSImpl.getInstance(), monitor);
}
/**
* Returns the java hover parameters from the given arguments map.
*
* @param arguments
* @param commandId
*
* @return the java hover parameters
*/
private static MicroProfileJavaHoverParams createMicroProfileJavaHoverParams(List<Object> arguments,
String commandId) {
Map<String, Object> obj = getFirst(arguments);
if (obj == null) {
throw new UnsupportedOperationException(String
.format("Command '%s' must be called with one MicroProfileJavaHoverParams argument!", commandId));
}
String javaFileUri = getString(obj, "uri");
if (javaFileUri == null) {
throw new UnsupportedOperationException(String.format(
"Command '%s' must be called with required MicroProfileJavaHoverParams.uri (java URI)!",
commandId));
}
Position hoverPosition = getPosition(obj, "position");
DocumentFormat documentFormat = DocumentFormat.PlainText;
Number documentFormatIndex = (Number) obj.get("documentFormat");
if (documentFormatIndex != null) {
documentFormat = DocumentFormat.forValue(documentFormatIndex.intValue());
}
boolean surroundEqualsWithSpaces = ((Boolean) obj.get("surroundEqualsWithSpaces")).booleanValue();
return new MicroProfileJavaHoverParams(javaFileUri, hoverPosition, documentFormat, surroundEqualsWithSpaces);
}
private List<SymbolInformation> getWorkspaceSymbolsForJava(List<Object> arguments, String commandId,
IProgressMonitor monitor) {
String projectUri = createMicroProfileJavaWorkspaceSymbolParams(arguments, commandId);
return PropertiesManagerForJava.getInstance().workspaceSymbols(projectUri, JDTUtilsLSImpl.getInstance(),
monitor);
}
private static String createMicroProfileJavaWorkspaceSymbolParams(List<Object> arguments, String commandId) {
Object projectUriObj = (String) arguments.get(0);
if (projectUriObj == null || !(projectUriObj instanceof String)) {
throw new UnsupportedOperationException(String
.format("Command '%s' must be called with one projectUri: String argument!", commandId));
}
return (String)projectUriObj;
}
}