Search for occurrences of a symbol in a project/file using Merlin/OCaml LSP

I am missing the “references” functionality in OCaml LSP that I really like in, say, in the clang C/C++ language server.

Example: Let say in C in while using clang LSP in my favourite editor I invoke “get references” on “somefunc()”. I then get a list of all the usages of the function across the project in various .c files at difference line numbers.

In the OCaml language server the same functionality works in an incomplete way.

Let say I’m looking for references of the function parse. When I “get references” I will get all the references to that function in only the .ml file I am searching.

But this can be defeated very easily. If somewhere else in the same .ml file, the parse function was invoked using SomeModule.parse, the result does not appear even though the function does live in the SomeModule after all.

All in all:
(1) Get references only returns results in a single .ml file
(2) Get references does not tolerate any kind of aliasing – if the function is referred to via an aliased name it will not be found.

Workaround

One silly workaround is simply rename the function parse to something like _parse and see all the compiler errors that result in the ml file. Of course one can use regexes but a strongly typed language should be able to do search for occurrences quite easily.

I am not so familiar with merlin but am aware that it is the backend for ocaml-lsp-server. Searching for occurrences using merlin seems to behave similarly like it did for ocaml-lsp-server. Maybe I did not use the correct command?

Question

How can I get the references functionality while editing OCaml code?

1 Like

@rgrinberg Any tips/suggestion from you regarding ^^ will be very useful :slight_smile: !

Seems like this feature is not available at the moment though some recent changes to the compiler have unblocked it !

See Support "Find occurrences" at whole project scale, not just inside the current file · Issue #377 · ocaml/merlin · GitHub

1 Like

This feature is being worked on by the merlin team. I’m not directly involved but the approach they’ve picked seems promising. @vds is probably the best person to give an update.

4 Likes

In the workaround category you have ocp-grep which is smart and able to find references over the whole project. It’s not perfect, but in the meantime it’s probably the most comprehensive solution.

ocp-grep map
ocp-grep List.map
ocp-grep List
ocp-grep option

Some examples of invocations.

1 Like

ocp-grep is cool! Thanks for the tip. Like you said, it is not perfect but its better than regexes in many situations…

Yes, we are now actively working on this feature !

Here is some teaser from the very first prototype:
Screen Recording 2022-09-16 at 11.10.03(4)

9 Likes

This looks amazing ! This feature will really help add “discoverability” to OCaml codebases.

Can’t wait to try this out! If possible, do let us know when this will be available to the public to play around with! :slight_smile:

1 Like