use of org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter in project lsp4j by eclipse.
the class ServiceEndpoints method getSupportedMethods.
/**
* Finds all Json RPC methods on a given type
*/
private static Map<String, JsonRpcMethod> getSupportedMethods(Class<?> type, Set<Class<?>> visitedTypes) {
Map<String, JsonRpcMethod> result = new LinkedHashMap<String, JsonRpcMethod>();
AnnotationUtil.findRpcMethods(type, visitedTypes, (methodInfo) -> {
JsonRpcMethod meth;
if (methodInfo.isNotification) {
meth = JsonRpcMethod.notification(methodInfo.name, methodInfo.parameterTypes);
} else {
Type genericReturnType = methodInfo.method.getGenericReturnType();
if (genericReturnType instanceof ParameterizedType) {
Type returnType = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0];
TypeAdapterFactory responseTypeAdapter = null;
ResponseJsonAdapter responseTypeAdapterAnnotation = methodInfo.method.getAnnotation(ResponseJsonAdapter.class);
if (responseTypeAdapterAnnotation != null) {
try {
responseTypeAdapter = responseTypeAdapterAnnotation.value().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
meth = JsonRpcMethod.request(methodInfo.name, returnType, responseTypeAdapter, methodInfo.parameterTypes);
} else {
throw new IllegalStateException("Expecting return type of CompletableFuture but was : " + genericReturnType);
}
}
if (result.put(methodInfo.name, meth) != null) {
throw new IllegalStateException("Duplicate RPC method " + methodInfo.name + ".");
}
;
});
AnnotationUtil.findDelegateSegments(type, new HashSet<>(), (method) -> {
Map<String, JsonRpcMethod> supportedDelegateMethods = getSupportedMethods(method.getReturnType(), visitedTypes);
for (JsonRpcMethod meth : supportedDelegateMethods.values()) {
if (result.put(meth.getMethodName(), meth) != null) {
throw new IllegalStateException("Duplicate RPC method " + meth.getMethodName() + ".");
}
;
}
});
return result;
}
use of org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter in project archetype-languageserver by nedap.
the class ADL2TextDocumentService method codeAction.
/**
* The code action request is sent from the client to the server to compute
* commands for a given text document and range. These commands are
* typically code fixes to either fix problems or to beautify/refactor code.
*
* Registration Options: TextDocumentRegistrationOptions
*/
@JsonRequest
@ResponseJsonAdapter(CodeActionResponseAdapter.class)
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(CodeActionParams params) {
DocumentInformation info = storage.getDocumentInformation(params.getTextDocument().getUri());
if (info == null) {
return CompletableFuture.completedFuture(new ArrayList<>());
}
if (info.getADLVersion() == ADLVersion.VERSION_1_4) {
CodeAction action1 = new CodeAction("convert this file to ADL 2");
action1.setKind(CodeActionKind.Source + ".convert.adl2");
{
Command command = new Command("Convert to ADL 2", ADL2_COMMAND);
command.setArguments(Lists.newArrayList(params.getTextDocument().getUri()));
action1.setCommand(command);
}
CodeAction actionAll = new CodeAction("convert all ADL 1.4 files to ADL 2");
actionAll.setKind(CodeActionKind.Source + ".convert.adl2");
{
Command command = new Command("Convert all ADL 1.4 files to ADL 2", ALL_ADL2_COMMAND);
command.setArguments(Lists.newArrayList(params.getTextDocument().getUri()));
actionAll.setCommand(command);
}
return CompletableFuture.completedFuture(Lists.newArrayList(Either.forRight(action1), Either.forRight(actionAll)));
} else {
if (info.getDiagnostics() != null) {
List<Diagnostic> missingTermsCodes = info.getDiagnostics().stream().filter(d -> d.getCode() != null && d.getCode().isLeft() && d.getCode().getLeft().startsWith(ErrorType.VATID.getCode()) && RangeUtils.rangesOverlap(params.getRange(), d.getRange())).collect(Collectors.toList());
// TODO: store in intermediate list so we can add more code actions :)
List<Either<Command, CodeAction>> codeActions = missingTermsCodes.stream().map(d -> {
CodeAction action = new CodeAction("add to terminology");
action.setKind(ADD_TO_TERMINOLOGY);
action.setIsPreferred(true);
action.setDiagnostics(Lists.newArrayList(d));
Command c = new Command("add to terminology", ADD_TO_TERMINOLOGY);
// TODO: store id/ac/at code here!
c.setArguments(Lists.newArrayList(params.getTextDocument().getUri(), d.getMessage()));
action.setCommand(c);
return Either.<Command, CodeAction>forRight(action);
}).collect(Collectors.toList());
for (String format : Lists.newArrayList("adl", "json", "xml")) {
CodeAction convertToOpt = new CodeAction("Convert to Opt (" + format + ")");
convertToOpt.setKind(WRITE_OPT_COMMAND + "." + format);
Command c = new Command("Write OPT", WRITE_OPT_COMMAND);
c.setArguments(Lists.newArrayList(params.getTextDocument().getUri(), format));
convertToOpt.setCommand(c);
codeActions.add(Either.<Command, CodeAction>forRight(convertToOpt));
}
for (String format : Lists.newArrayList("json", "flat_json", "xml")) {
CodeAction convertToOpt = new CodeAction("Generate example (" + format + ")");
convertToOpt.setKind(WRITE_EXAMPLE_COMMAND + "." + format);
Command c = new Command("Write example", WRITE_EXAMPLE_COMMAND);
c.setArguments(Lists.newArrayList(params.getTextDocument().getUri(), format));
convertToOpt.setCommand(c);
codeActions.add(Either.<Command, CodeAction>forRight(convertToOpt));
}
return CompletableFuture.completedFuture(codeActions);
}
}
return CompletableFuture.completedFuture(Collections.emptyList());
}
Aggregations