ocaml-lsp icon indicating copy to clipboard operation
ocaml-lsp copied to clipboard

Documentation with simple example to interact with ocamllsp

Open aryx opened this issue 5 years ago • 4 comments

Hi,

I would like to write a simple program that works across multiple languages (hence the use of LSP) which given a file and a position return typing information at that position.

For OCaml, I'm able to do that with ocamlmerlin by just running the command 'ocamlmerlin single type-enclosing -position 1:1 -index 0' in a shell and process the output. It's pretty easy to do that programmatically from OCaml by using Unix.pipe.

I would like to do the same but using ocamllsp so I can hope to later port that to other languages using different LSP servers depending on the language. I've started to look at the code of ocamllsp and lsp/ but the interface are a bit complicated and there's no much documentation. I can see there's a Client.start, but it returns a fiber, which I have no idea to further use to give the appropriate Client_request commands.

It would be great to have a simple demo on how to invoke ocamllsp and a simple ocaml program that use lsp.ml to interact with this ocamllsp to get type information.

aryx avatar Aug 28 '20 14:08 aryx

A ocamllsp vs ocamlmerlin cheatsheet would be great to show the interface differences. I am currently using ocamlmerlin in my Emacs-clone editor written in OCaml (https://github.com/aryx/fork-efuns ) but would like to migrate to ocamllsp, which would provide again a path towards supporting other languages in my editor.

aryx avatar Aug 31 '20 19:08 aryx

For reference here is how I currently call ocamlmerlin: https://github.com/aryx/fork-efuns/blob/master/prog_modes/ocaml_merlin.ml

aryx avatar Aug 31 '20 19:08 aryx

You can use ocamllsp the same way you use ocamlmerlin. Just by running it as a binary and communicating with it using the lsp protocol which is much better documented here. In particular, to get type information you're looking at the hover request.

You could use Client_request.t to construct your Json requests. But apart from that, we don't have a functional client implementation yet.

rgrinberg avatar Aug 31 '20 23:08 rgrinberg

Ok I'll try that. I was hoping I could use some of the APIs in lsp/ to start and communicate the server but maybe I can do it myself if the API is too complicated to use.

aryx avatar Sep 01 '20 07:09 aryx

I think that new end-to-end tests should cover basic interaction between a client and olsp.

ulugbekna avatar Oct 28 '22 09:10 ulugbekna