Search in sources :

Example 1 with JsonRpcMethod

use of org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod in project xtext-core by eclipse.

the class LanguageServerImpl method supportedMethods.

@Override
public Map<String, JsonRpcMethod> supportedMethods() {
    if (supportedMethods != null) {
        return supportedMethods;
    }
    synchronized (extensionProviders) {
        Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap<>();
        supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(getClass()));
        Map<String, JsonRpcMethod> extensions = new LinkedHashMap<>();
        for (IResourceServiceProvider resourceServiceProvider : getAllLanguages()) {
            ILanguageServerExtension ext = resourceServiceProvider.get(ILanguageServerExtension.class);
            if (ext != null) {
                ext.initialize(access);
                Map<String, JsonRpcMethod> supportedExtensions = ext instanceof JsonRpcMethodProvider ? ((JsonRpcMethodProvider) ext).supportedMethods() : ServiceEndpoints.getSupportedMethods(ext.getClass());
                for (Map.Entry<String, JsonRpcMethod> entry : supportedExtensions.entrySet()) {
                    if (supportedMethods.containsKey(entry.getKey())) {
                        LOG.error("The json rpc method \'" + entry.getKey() + "\' can not be an extension as it is already defined in the LSP standard.");
                    } else {
                        JsonRpcMethod existing = extensions.put(entry.getKey(), entry.getValue());
                        if (existing != null && !Objects.equal(existing, entry.getValue())) {
                            LOG.error("An incompatible LSP extension \'" + entry.getKey() + "\' has already been registered. Using 1 ignoring 2. \n1 : " + existing + " \n2 : " + entry.getValue());
                            extensions.put(entry.getKey(), existing);
                        } else {
                            Endpoint endpoint = ServiceEndpoints.toEndpoint(ext);
                            extensionProviders.put(entry.getKey(), endpoint);
                            supportedMethods.put(entry.getKey(), entry.getValue());
                        }
                    }
                }
            }
        }
        this.supportedMethods = supportedMethods;
        return supportedMethods;
    }
}
Also used : JsonRpcMethodProvider(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) Endpoint(org.eclipse.lsp4j.jsonrpc.Endpoint) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) ImmutableMap(com.google.common.collect.ImmutableMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with JsonRpcMethod

use of org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod in project ballerina by ballerina-lang.

the class BallerinaLangServerService method launchRPCServer.

private <T> Launcher<T> launchRPCServer(Object localService, Class<T> remoteInterface) {
    Consumer<GsonBuilder> configureGson = (gsonBuilder) -> {
    };
    Map<String, JsonRpcMethod> supportedMethods = new LinkedHashMap();
    supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(remoteInterface));
    if (localService instanceof JsonRpcMethodProvider) {
        JsonRpcMethodProvider rpcMethodProvider = (JsonRpcMethodProvider) localService;
        supportedMethods.putAll(rpcMethodProvider.supportedMethods());
    } else {
        supportedMethods.putAll(ServiceEndpoints.getSupportedMethods(localService.getClass()));
    }
    MessageJsonHandler jsonHandler = new MessageJsonHandler(supportedMethods, configureGson);
    MessageConsumer outGoingMessageStream = new WSRPCMessageConsumer(this, jsonHandler);
    RemoteEndpoint serverEndpoint = new RemoteEndpoint(outGoingMessageStream, ServiceEndpoints.toEndpoint(localService));
    jsonHandler.setMethodProvider(serverEndpoint);
    final MessageConsumer messageConsumer = serverEndpoint;
    final MessageProducer reader = new WSRPCMessageProducer(this, jsonHandler);
    final T remoteProxy = ServiceEndpoints.toServiceObject(serverEndpoint, remoteInterface);
    return new Launcher<T>() {

        public Future<?> startListening() {
            return ConcurrentMessageProcessor.startProcessing(reader, messageConsumer, Executors.newCachedThreadPool());
        }

        public T getRemoteProxy() {
            return remoteProxy;
        }
    };
}
Also used : ConcurrentMessageProcessor(org.eclipse.lsp4j.jsonrpc.json.ConcurrentMessageProcessor) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) OnError(javax.websocket.OnError) CloseReason(javax.websocket.CloseReason) OnMessage(javax.websocket.OnMessage) LoggerFactory(org.slf4j.LoggerFactory) Constants(org.ballerinalang.composer.service.ballerina.langserver.Constants) MessageProducer(org.eclipse.lsp4j.jsonrpc.MessageProducer) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) OnOpen(javax.websocket.OnOpen) ComposerService(org.ballerinalang.composer.server.spi.ComposerService) Future(java.util.concurrent.Future) OnClose(javax.websocket.OnClose) Map(java.util.Map) LinkedList(java.util.LinkedList) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) ServiceType(org.ballerinalang.composer.server.spi.ServiceType) Logger(org.slf4j.Logger) Session(javax.websocket.Session) ServiceEndpoints(org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints) MessageJsonHandler(org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler) IOException(java.io.IOException) ServerEndpoint(javax.websocket.server.ServerEndpoint) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) ServerConstants(org.ballerinalang.composer.server.core.ServerConstants) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod) ServiceInfo(org.ballerinalang.composer.server.spi.ServiceInfo) JsonRpcMethodProvider(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider) BallerinaLanguageServer(org.ballerinalang.langserver.BallerinaLanguageServer) RemoteEndpoint(org.eclipse.lsp4j.jsonrpc.RemoteEndpoint) JsonRpcMethodProvider(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider) MessageConsumer(org.eclipse.lsp4j.jsonrpc.MessageConsumer) MessageJsonHandler(org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler) GsonBuilder(com.google.gson.GsonBuilder) RemoteEndpoint(org.eclipse.lsp4j.jsonrpc.RemoteEndpoint) LinkedHashMap(java.util.LinkedHashMap) JsonRpcMethod(org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod) Launcher(org.eclipse.lsp4j.jsonrpc.Launcher) MessageProducer(org.eclipse.lsp4j.jsonrpc.MessageProducer)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 JsonRpcMethod (org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod)2 JsonRpcMethodProvider (org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethodProvider)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 GsonBuilder (com.google.gson.GsonBuilder)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 Executors (java.util.concurrent.Executors)1 Future (java.util.concurrent.Future)1 Consumer (java.util.function.Consumer)1 CloseReason (javax.websocket.CloseReason)1 OnClose (javax.websocket.OnClose)1 OnError (javax.websocket.OnError)1 OnMessage (javax.websocket.OnMessage)1 OnOpen (javax.websocket.OnOpen)1