eclipse-lsp4j / lsp4j

A Java implementation of the language server protocol intended to be consumed by tools and language servers implemented in Java.
https://eclipse.org/lsp4j
Other
613 stars 145 forks source link

Question: create client and connect to lsp server #579

Closed nojaf closed 2 years ago

nojaf commented 2 years ago

Hello,

This is more of a question on how to use the lsp4j instead of an actual bug report. If there is an alternative way of asking questions, please let me know.

I wish to create a client that can connect to an LSP server that runs in a .NET process. I've implemented this server in F# and I now want to connect to it and call these RPC functions. So, I imagine I need to start the process, capture stdin and stdout and then somehow send JSON messages to this.

I'm not really sure what I'm after, although the EndpointProxy looks like what I'm looking for. The code sample in the docs looks like this:

Endpoint endpoint = ...
MyService proxy = ServiceEndpoints.toProxy(endpoint, MyService.class);

but I'm not sure how to create an endpoint from the stdio streams.

Could someone provide me with a pointer on how to set this up?

Many thanks,

Florian

cdietrich commented 2 years ago

hi, did you check the helpers in https://github.com/eclipse/lsp4j/blob/5605ca82252833777dbef82ca13a403dbfb9d906/org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/Launcher.java and https://github.com/eclipse/lsp4j/blob/5605ca82252833777dbef82ca13a403dbfb9d906/org.eclipse.lsp4j/src/main/java/org/eclipse/lsp4j/launch/LSPLauncher.java https://github.com/eclipse/lsp4j/tree/main/documentation

nojaf commented 2 years ago

Hey, that looks indeed like what I'm after. When seeing

        return new Builder<T>()
                .setLocalService(localService)
                .setRemoteInterface(remoteInterface)

What should my local service be exactly? Same for remote interface?

My T currently is

interface FantomasDaemon {
    @JsonRequest("fantomas/version", useSegment = false)
    fun version() : String
}

which matches what my server is implementing.

cdietrich commented 2 years ago

see https://github.com/eclipse/lsp4j/blob/5605ca82252833777dbef82ca13a403dbfb9d906/org.eclipse.lsp4j/src/main/java/org/eclipse/lsp4j/launch/LSPLauncher.java#L100

so localservice is you client instance, remote is the interface of the server. see also this tutorial https://www.eclipse.org/community/eclipse_newsletter/2017/may/article2.php https://github.com/TypeFox/lsp4j-chat-app

nojaf commented 2 years ago

Figured it out, wonderful! Thanks for the pointers @cdietrich!

nojaf commented 2 years ago

@cdietrich small follow-up question, what if I'm done with my client and I want to garbage collect it? Can I just kill the process and assume everything will be cleaned up from there on?

cdietrich commented 2 years ago

no idea. can you check the example i linked. you also may check https://github.com/eclipse/lsp4e