ocaml / merlin

Context sensitive completion for OCaml in Vim and Emacs
https://ocaml.github.io/merlin/
MIT License
1.58k stars 233 forks source link

merlin gives syntax error on #use directive (or #require) #1708

Open SleepyMug opened 11 months ago

SleepyMug commented 11 months ago

I tried to write an Ocaml program in a scripting style, but I realized merlin (version 4.12-501) does not work with toplevel directives. The simplest example would be

$ cat a.ml
#use "topfind"
let () = print_endline "Hello, world!";;
$ ocaml a.ml
Hello, world!
$ ocamlmerlin single errors -filename a.ml < a.ml
{"class":"return","value":[{"start":{"line":1,"col":0},"end":{"line":1,"col":1},"type":"parser","sub":[],"valid":true,"message":"Syntax error"}],"notifications":[],"timing":{"clock":4,"cpu":3,"query":0,"pp":0,"reader":0,"ppx":0,"typer":3,"error":0}}

The program I was trying to do looks like this

$ cat b.ml
#use "topfind"
#thread
#require "core"
open Core;;
let today = Date.create_exn ~y:2023 ~m:Month.Nov ~d:17;;
let yesterday = Date.create_exn ~y:2023 ~m:Month.Nov ~d:16;;
let () =
  Printf.printf "%d\n" (Date.diff today yesterday);;
$ ocaml b.ml
1
$ ocamlmerlin single errors -filename b.ml < b.ml
{"class":"return","value":[{"start":{"line":1,"col":0},"end":{"line":1,"col":1},"type":"parser","sub":[],"valid":true,"message":"Syntax error"}],"notifications":[],"timing":{"clock":6,"cpu":5,"query":0,"pp":0,"reader":0,"ppx":0,"typer":5,"error":0}}

I'm new to the language, and I have the feeling that this isn't how serious ocaml projects are designed to be run. But it's still useful for simple scripts and test out things.

The problem is similar to #1023 .

ysalmon commented 9 months ago

I second this : using merlin in Emacs+Tuareg is a thing, and this fails if there is a directive.

Note however that many directives can be replaced by their definition from https://github.com/ocaml/ocaml/blob/trunk/toplevel/topdirs.ml.

For instance, #use "thing" seems to be replaceable with Topdirs.dir_use Format.std_formatter "thing".

In fact this is even better because you can use OCaml expressions, including variables, to build the file name : I am a fan of Topdirs.dir_directory ("/path-to-precompiled-files/ ^ Sys.ocaml_version), which lets me put *.cmo files for different OCaml versions in their respectives directories.