8

I have a personal Editor that implements its own "protocol" for Code completion and would like to switch to Language server protocol to relieve myself from some development burden. However I have not been able to fully comprehend the LSP Documentation concerning client implementation. It's mixed up with Server implementation and cannot find clear difference between the two.

Much of online documentation (including questions on SO) revolved around VSCode extensions of which I have zero knowledge and are not helpful.

I would appreciate any help to get started writing a simple client that just queries autocomplete. I write my editor in C++ and would appreciate any tutorial that explains how to create client. I can understand Python, C, C#, Pure JavaScript, C++, PHP and similar languages, so I can easily follow any tutorial in those languages.

If anything is not clear let me know so that I can explain (this being my first post in LSP :) )

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93

2 Answers2

5

Mattie of course lead me to the right direction. The most difficult was for me to understand the request/response. LSP have a very helpful page on that with unfortunate name (IMHO) that had misled me , "inspection". It turns out to be a good example on request/response.

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
3

I've successfully written a client that receives completion results from a server. Here's (approximately) what it does:

  • send an initialize
  • send an initialized notification
  • send a textDocument/didOpen

At this point, your server should be ready to answer the textDocument/completion request. Depending on the response, you may also need to use completionItem/resolve as well. To date, I've never needed this for the servers I've used, however.

Good luck!

Mattie
  • 2,868
  • 2
  • 25
  • 40
  • Thanks for the answer. Which interfaces did you use in client? – Stefano Mtangoo Mar 23 '19 at 12:43
  • I'm not 100% sure I understand the question. Which interfaces are you referring to? Do you mean the register/unregisterCapability? Those are optional, and are used for dynamically configuring your support for features. I haven't used them at all. – Mattie Mar 23 '19 at 13:27
  • I get it. Thank you – Stefano Mtangoo Mar 23 '19 at 18:13
  • can you update the contents of your jsonrpc as mine here doesn't work. Here is what I send in initialize: `{"jsonrpc": "2.0", "method": "initialize", "params":{ "rootPath": "/Users/userpc/Desktop/folder", "rootUri": "file:////Users/userpc/Desktop/folder", "initializationOptions": {}, "trace": "verbose", } }` – Stefano Mtangoo Mar 24 '19 at 14:54
  • I don't have an easy way to capture the raw JSON, but yes, the initialize call is complex. Depending on the server, you many need to provide a lot of configuration before things will work. Just looking at what you posted above, I can tell you that JSON in invalid. Trailing `,`'s are now allowed, unfortunately. – Mattie Mar 24 '19 at 15:23
  • 2
    Thank you. LSP is a good thing. However, one mistake is they think everyone is a VSCode/Typescript guy. and Docs are lacking a lot for someone outside that community (where a lot of tooling exists for LSP) – Stefano Mtangoo Mar 24 '19 at 19:40
  • @Mattie do you have some examples of theses JSON requests from `initialize` to `textDocument/completion`? – shellwhale Jun 24 '23 at 13:08
  • I'm afraid I do not have any that are easy to share. But two comments. The first is @StefanoMtangoo posted JSON that is actually invalid (trailing commas are not allowed). And the second is I have found servers to have very different sensitivities to the JSON supplied . Some care about case, some don't. Some care about nullable keys present, some don't. And many do not report errors well in these cases. – Mattie Jun 30 '23 at 18:28