ocaml / tuareg

Emacs OCaml mode
GNU General Public License v3.0
360 stars 79 forks source link

Completion in the *OCaml* buffer #299

Open Fourchaux opened 1 year ago

Fourchaux commented 1 year ago

Configuration: OCaml 4.14.0 - Emacs 29.0.50 - Tuareg (v20220929.1327) - Merlin - Linux Box x86_64

By default, I have no completion in the *OCaml* buffer. Now, if I run 'M-x merlin-mode' I get the message 'Merlin mode disabled in current buffer' and ,oh surprise, the completion appears (in this *OCaml* buffer).

So,three questions:

Note: The emacs mode of Merlin provides 3 ways to achieve completion:

Eventually, this third way (being the native completion engine of Emacs) seems a good candidate to use.

bbatsov commented 1 year ago

There's no way for Merlin to provide completion candidates directly for the top-level, as it operates on source files. utop.el uses utop's completion API to provide completion within the top-level and plays nicely with tuareg-mode.

Fourchaux commented 1 year ago

Small experiment: (same configuration except for Tuareg now v3.0.1)

Using a very basic init.el (with tuareg, merlin, company, and NOTHING else) I get the same behavior.

e.g. with this ugly code (idem with eval-region & eval-buffer):

(defun my-tuareg-eval-phrase ()
  (interactive)
  (tuareg-eval-phrase)
  (set-buffer "*OCaml*")
  (merlin-mode))

I have the completion in the OCaml REPL. (completion-at-point alone works too).

So I guess it must be possible to properly incorporate a completion in the Tuareg's interactive mode.

bbatsov commented 1 year ago

I'll have to take a closer look at where Merlin pulls the candidates from exactly, but I can't imagine how exactly something like this will work. E.g. do completion candidates update after you load stuff in the toplevel or reload the code there? I'm also puzzled by the command you've shared - you first eval something in a source file (I presume), then set the current buffer to the REPL and you enable merlin-mode there? If merlin-mode works in the toplevel buffer you can just enable it there and be done with it.