merlin icon indicating copy to clipboard operation
merlin copied to clipboard

Go-to-definition does not work on custom operators

Open WardBrian opened this issue 11 months ago • 4 comments

Go-to-definition doesn't work on (all) custom operators, which would be very useful for navigating DSLs.

For example,

let ( := ) v a = Printf.printf "%s = %d" v a
let () = "foo" := 3
(*              ^ *)

If I place my cursor at the point indicated by the ^, go-to-definition does nothing.

Similarly, for custom indexing operators:


let (.%{;..}) a k = Printf.printf "%s.coeffRef(%d);\n" a k.(0)
let (.%{ }) a k = Printf.printf "%s.coeffRef(%d);\n" a k
let name = "baz"
let () = name.%{2;4}
let () = name.%{5}

Placing the cursor at the % on either like 4 or 5 does not succeed.

Originally opened as https://github.com/ocamllabs/vscode-ocaml-platform/issues/1766

WardBrian avatar Mar 27 '25 13:03 WardBrian

I should also say, if someone is willing to give me a few pointers on how to get started and what the scope of this is, I'd be more than willing to have a go at contributing the fix myself!

WardBrian avatar Mar 28 '25 14:03 WardBrian

Here is how I would proceed:

  • Write a test illustrating the wrong behavior (similar as https://github.com/ocaml/merlin/blob/main/tests/test-dirs/locate/issue949.t)
  • Such a test would already be welcome as a contribution
  • Then you can use that test to print some logs by adding -log-file - to the $MERLIN command line.
  • I expect these to guide you toward Query_commands (the entry point for Merlin analyses)
  • My bet would be an issue with reconstruct_identifier (here) that Merlin uses to reparse the identifier under the cursor.
  • You can also have a look at the asts in the test by using the commands $MERLIN single dump -what parsetree ... and dump -what typedtree

voodoos avatar Mar 28 '25 17:03 voodoos

Huh. I started writing the test and was surprised when the output actually was what I expected. I then tried M-x merlin-locate in Emacs and it also worked. Going back into VSCode, it now works too -- so maybe I just hadn't run a dune clean && dune build recently enough. My binary operators also seem to be working now.

Things that aren't:

A custom definition of ( := ) is returning Not a valid identifier, and the custom indexing operators are failing. I'll see if I can at least contribute a test for those.

WardBrian avatar Mar 31 '25 15:03 WardBrian

Thanks @voodoos -- your hints were very helpful (and your hunch about reconstruct_identifier right on the money). I've taken a crack at it over in #1917

WardBrian avatar Mar 31 '25 20:03 WardBrian