Search in sources :

Example 1 with ResponseJsonAdapter

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;
}
Also used : TypeAdapterFactory(com.google.gson.TypeAdapterFactory) ResponseJsonAdapter(org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter) LinkedHashMap(java.util.LinkedHashMap) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod)

Example 2 with ResponseJsonAdapter

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());
}
Also used : BroadcastingArchetypeRepository(com.nedap.openehr.lsp.repository.BroadcastingArchetypeRepository) ResponseJsonAdapter(org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ADLVersion(com.nedap.openehr.lsp.document.ADLVersion) CodeActionResponseAdapter(org.eclipse.lsp4j.adapters.CodeActionResponseAdapter) CompletableFuture(java.util.concurrent.CompletableFuture) GenerateExampleCommand(com.nedap.openehr.lsp.commands.GenerateExampleCommand) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) JsonPrimitive(com.google.gson.JsonPrimitive) URI(java.net.URI) org.eclipse.lsp4j(org.eclipse.lsp4j) TextDocumentService(org.eclipse.lsp4j.services.TextDocumentService) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) AQLStorage(com.nedap.openehr.lsp.aql.AQLStorage) WorkspaceService(org.eclipse.lsp4j.services.WorkspaceService) AddTerminologyCommmand(com.nedap.openehr.lsp.commands.AddTerminologyCommmand) Collectors(java.util.stream.Collectors) File(java.io.File) List(java.util.List) ANTLRParserErrors(com.nedap.archie.antlr.errors.ANTLRParserErrors) ErrorType(com.nedap.archie.archetypevalidator.ErrorType) RangeUtils(com.nedap.openehr.lsp.utils.RangeUtils) ConvertToOptCommand(com.nedap.openehr.lsp.commands.ConvertToOptCommand) RemoteEndpoint(org.eclipse.lsp4j.jsonrpc.RemoteEndpoint) Collections(java.util.Collections) ValidationResult(com.nedap.archie.archetypevalidator.ValidationResult) GenerateExampleCommand(com.nedap.openehr.lsp.commands.GenerateExampleCommand) ConvertToOptCommand(com.nedap.openehr.lsp.commands.ConvertToOptCommand) DocumentInformation(com.nedap.openehr.lsp.document.DocumentInformation) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) JsonRequest(org.eclipse.lsp4j.jsonrpc.services.JsonRequest) ResponseJsonAdapter(org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter)

Aggregations

ResponseJsonAdapter (org.eclipse.lsp4j.jsonrpc.json.ResponseJsonAdapter)2 Lists (com.google.common.collect.Lists)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 TypeAdapterFactory (com.google.gson.TypeAdapterFactory)1 ANTLRParserErrors (com.nedap.archie.antlr.errors.ANTLRParserErrors)1 ErrorType (com.nedap.archie.archetypevalidator.ErrorType)1 ValidationResult (com.nedap.archie.archetypevalidator.ValidationResult)1 AQLStorage (com.nedap.openehr.lsp.aql.AQLStorage)1 AddTerminologyCommmand (com.nedap.openehr.lsp.commands.AddTerminologyCommmand)1 ConvertToOptCommand (com.nedap.openehr.lsp.commands.ConvertToOptCommand)1 GenerateExampleCommand (com.nedap.openehr.lsp.commands.GenerateExampleCommand)1 ADLVersion (com.nedap.openehr.lsp.document.ADLVersion)1 DocumentInformation (com.nedap.openehr.lsp.document.DocumentInformation)1 BroadcastingArchetypeRepository (com.nedap.openehr.lsp.repository.BroadcastingArchetypeRepository)1 RangeUtils (com.nedap.openehr.lsp.utils.RangeUtils)1 File (java.io.File)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1