Search in sources :

Example 6 with LanguageClient

use of org.eclipse.lsp4j.services.LanguageClient in project xtext-core by eclipse.

the class AbstractIdeQuickfixTest method quickfixesAreOffered.

private void quickfixesAreOffered(EObject target, String issueCode, String originalText, QuickfixExpectation... expected) {
    List<QuickfixExpectation> expectedSorted = IterableExtensions.sortBy(Arrays.asList(expected), it -> it.label);
    ICompositeNode elementNode = NodeModelUtils.getNode(target);
    LineAndColumn elementStartPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getOffset());
    LineAndColumn elementEndPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getEndOffset());
    Position startPos = new Position(elementStartPosition.getLine() - 1, elementStartPosition.getColumn() - 1);
    Position endPos = new Position(elementEndPosition.getLine() - 1, elementEndPosition.getColumn() - 1);
    Diagnostic issue = new Diagnostic();
    issue.setCode(issueCode);
    issue.setMessage("error");
    issue.setSeverity(DiagnosticSeverity.Error);
    issue.setSource("source");
    issue.setRange(new Range(startPos, endPos));
    ICodeActionService2.Options options = new ICodeActionService2.Options();
    options.setCancelIndicator(CancelIndicator.NullImpl);
    options.setDocument(new Document(Integer.valueOf(0), originalText));
    options.setResource((XtextResource) target.eResource());
    options.setLanguageServerAccess(new ILanguageServerAccess() {

        @Override
        public void addBuildListener(ILanguageServerAccess.IBuildListener listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T extends Object> CompletableFuture<T> doRead(String uri, Function<ILanguageServerAccess.Context, T> function) {
            ILanguageServerAccess.Context ctx = new ILanguageServerAccess.Context(options.getResource(), options.getDocument(), true, CancelIndicator.NullImpl);
            return CompletableFuture.completedFuture(function.apply(ctx));
        }

        @Override
        public <T extends Object> CompletableFuture<T> doReadIndex(Function<? super ILanguageServerAccess.IndexContext, ? extends T> function) {
            return null;
        }

        @Override
        public InitializeParams getInitializeParams() {
            return null;
        }

        @Override
        public InitializeResult getInitializeResult() {
            return null;
        }

        @Override
        public LanguageClient getLanguageClient() {
            return null;
        }

        @Override
        public ResourceSet newLiveScopeResourceSet(URI uri) {
            // re-using the existing ResourceSet because it contains the URI protocol mapping for "inmemory" resources.
            ResourceSet resourceSet = options.getResource().getResourceSet();
            return resourceSet;
        }
    });
    CodeActionParams codeActionParams = new CodeActionParams();
    codeActionParams.setRange(new Range(startPos, endPos));
    codeActionParams.setTextDocument(new TextDocumentIdentifier(target.eResource().getURI().toString()));
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setDiagnostics(Collections.singletonList(issue));
    codeActionParams.setContext(codeActionContext);
    options.setCodeActionParams(codeActionParams);
    List<DiagnosticResolution> actualIssueResolutions = IterableExtensions.sortBy(quickFixProvider.getResolutions(options, issue), DiagnosticResolution::getLabel);
    assertEquals("The number of quickfixes does not match!", expectedSorted.size(), actualIssueResolutions.size());
    for (int i = 0; i < actualIssueResolutions.size(); i++) {
        DiagnosticResolution actualIssueResolution = actualIssueResolutions.get(i);
        QuickfixExpectation expectedIssueResolution = expectedSorted.get(i);
        assertEquals(expectedIssueResolution.label, actualIssueResolution.getLabel());
        assertEquals(expectedIssueResolution.description, actualIssueResolution.getLabel());
        assertIssueResolutionResult(toUnixLineSeparator(expectedIssueResolution.getExpectedResult()), actualIssueResolution, originalText, options.getDocument());
    }
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DiagnosticResolution(org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution) Diagnostic(org.eclipse.lsp4j.Diagnostic) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) CompletableFuture(java.util.concurrent.CompletableFuture) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ILanguageServerAccess(org.eclipse.xtext.ide.server.ILanguageServerAccess) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Position(org.eclipse.lsp4j.Position) ICodeActionService2(org.eclipse.xtext.ide.server.codeActions.ICodeActionService2) InitializeParams(org.eclipse.lsp4j.InitializeParams) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LineAndColumn(org.eclipse.xtext.util.LineAndColumn) Range(org.eclipse.lsp4j.Range) InitializeResult(org.eclipse.lsp4j.InitializeResult) CodeActionContext(org.eclipse.lsp4j.CodeActionContext)

Example 7 with LanguageClient

use of org.eclipse.lsp4j.services.LanguageClient in project xtext-core by eclipse.

the class SocketServerLauncher method launch.

public void launch(String[] args) {
    Injector injector = Guice.createInjector(getServerModule());
    try (AsynchronousServerSocketChannel serverSocket = AsynchronousServerSocketChannel.open().bind(getSocketAddress(args))) {
        LOG.info("Started server socket at " + getSocketAddress(args));
        while (true) {
            AsynchronousSocketChannel socketChannel = serverSocket.accept().get();
            InputStream in = Channels.newInputStream(socketChannel);
            OutputStream out = Channels.newOutputStream(socketChannel);
            PrintWriter trace = getTrace(args);
            boolean validate = shouldValidate(args);
            LanguageServerImpl languageServer = injector.getInstance(LanguageServerImpl.class);
            LOG.info("Starting Xtext Language Server for client " + socketChannel.getRemoteAddress());
            Launcher<LanguageClient> launcher = Launcher.createLauncher(languageServer, LanguageClient.class, in, out, validate, trace);
            languageServer.connect(launcher.getRemoteProxy());
            launcher.startListening();
            LOG.info("Xtext Language Server has been started.");
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }
}
Also used : AsynchronousSocketChannel(java.nio.channels.AsynchronousSocketChannel) Injector(com.google.inject.Injector) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) AsynchronousServerSocketChannel(java.nio.channels.AsynchronousServerSocketChannel) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) PrintWriter(java.io.PrintWriter)

Example 8 with LanguageClient

use of org.eclipse.lsp4j.services.LanguageClient in project ballerina by ballerina-lang.

the class BallerinaLangServerService method onOpen.

@OnOpen
public void onOpen(Session session) {
    sessions.add(session);
    if (launcher != null) {
        return;
    }
    this.launcher = this.launchRPCServer(server, LanguageClient.class);
    LanguageClient client = launcher.getRemoteProxy();
    server.connect(client);
    Future<?> startListening = launcher.startListening();
    try {
        startListening.get();
    } catch (InterruptedException | ExecutionException e) {
        LOGGER.error("Error starting language server", e);
    }
}
Also used : LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ExecutionException(java.util.concurrent.ExecutionException) OnOpen(javax.websocket.OnOpen)

Example 9 with LanguageClient

use of org.eclipse.lsp4j.services.LanguageClient in project sts4 by spring-projects.

the class SimpleLanguageServer method onError.

public void onError(String message, Throwable error) {
    LanguageClient cl = this.client;
    if (cl != null) {
        if (error instanceof ShowMessageException)
            client.showMessage(((ShowMessageException) error).message);
        else {
            Log.log(message, error);
            MessageParams m = new MessageParams();
            m.setMessage(message);
            m.setType(MessageType.Error);
            client.showMessage(m);
        }
    }
}
Also used : LanguageClient(org.eclipse.lsp4j.services.LanguageClient) STS4LanguageClient(org.springframework.ide.vscode.commons.languageserver.STS4LanguageClient) MessageParams(org.eclipse.lsp4j.MessageParams)

Example 10 with LanguageClient

use of org.eclipse.lsp4j.services.LanguageClient in project smarthome by eclipse.

the class ModelServer method handleConnection.

private void handleConnection(final Socket client) {
    logger.debug("Client {} connected", client.getRemoteSocketAddress());
    try {
        LanguageServerImpl languageServer = injector.getInstance(LanguageServerImpl.class);
        Launcher<LanguageClient> launcher = LSPLauncher.createServerLauncher(languageServer, client.getInputStream(), client.getOutputStream());
        languageServer.connect(launcher.getRemoteProxy());
        Future<?> future = launcher.startListening();
        future.get();
    } catch (IOException e) {
        logger.warn("Error communicating with LSP client {}", client.getRemoteSocketAddress());
    } catch (InterruptedException e) {
    // go on, let the thread finish
    } catch (ExecutionException e) {
        logger.error("Error running the Language Server", e);
    }
    logger.debug("Client {} disconnected", client.getRemoteSocketAddress());
}
Also used : LanguageServerImpl(org.eclipse.xtext.ide.server.LanguageServerImpl) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

LanguageClient (org.eclipse.lsp4j.services.LanguageClient)10 Injector (com.google.inject.Injector)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 PrintWriter (java.io.PrintWriter)2 ExecutionException (java.util.concurrent.ExecutionException)2 LanguageClientAware (org.eclipse.lsp4j.services.LanguageClientAware)2 LanguageServer (org.eclipse.lsp4j.services.LanguageServer)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 AsynchronousServerSocketChannel (java.nio.channels.AsynchronousServerSocketChannel)1 AsynchronousSocketChannel (java.nio.channels.AsynchronousSocketChannel)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1 OnOpen (javax.websocket.OnOpen)1 BallerinaLanguageServer (org.ballerinalang.langserver.BallerinaLanguageServer)1 URI (org.eclipse.emf.common.util.URI)1