ocaml-community / utop

Universal toplevel for OCaml
Other
844 stars 112 forks source link

Completion broken when running utop from emacs #455

Open jbulow opened 1 year ago

jbulow commented 1 year ago

Typing Lis<TAB> correctly completes into either List or ListLabels but when typing List.<TAB> a message "No match" appears in the minibuffer.

 utop -version
The universal toplevel for OCaml, version 2.13.1, compiled for OCaml version 5.0.0

ocamlc -version
5.0.0

emacs --version
GNU Emacs 29.1.50
rchog commented 7 months ago

I'm having this exact problem still, with utop-20230707.1535 installed from melpa today (I've had the problem for much longer but only got round to looking into it today). I also tried deleting the utop package and copying utop.el straight from this repository, with the same results. My Emacs version is 29.2. My utop version is 2.13.1 and OCaml is 5.1.1. \<TAB> is bound to utop-complete.

Edit, next day: turns out I must've messed up when trying the changes in @steffenhaug's branch -- they fix the problem after all. I'll delete my extensive and pointless explanation below. Without the changes above, the completion stops in completion--nth-completion (from minibuffer.el) as no completion styles, at least in my Emacs, will match when given e.g "List.ap" as the string argument and just '("append") as the table.

steffenhaug commented 7 months ago

There are still some cases where my version determines the incorrect completion prefix, but if I recall correctly, the status quo is broken in the same ways. It is a start, but it would be nice to also be able to complete in the middle of the input, and in the cases where the prefix isn't correctly determined by just looking for .s.

monkeyjunglejuice commented 5 months ago
Screenshot 2024-04-17 at 20 06 59

Workaround: Turns out if you turn off merlin-mode in the utop-buffer and then enable merlin-mode again in the utop buffer, then utop suddenly gets full completions from merlin. So that means if you type List. like in the example by @jbulow, you'll see all the completion candidates from the List module (tested with Corfu). What you don't get though, are symbols defined in the source buffer. Here's an example: the hook turns merlin-mode off and then on again:

(use-package utop
  :ensure t
  :custom
  ;; Utop will start with Dune project awareness:
  (utop-command "opam exec -- dune utop . -- -emacs")
  :hook
  (tuareg-mode . utop-minor-mode)
  ;; HACK Workaround to get completions in Utop from Merlin
  (utop-mode . (lambda ()
                 (merlin-mode -1)
                 (merlin-mode +1)))
  :bind
  ;; Reach `utop' from anywhere via global key binding
  (:map ctl-z-x-map
        ("u" . utop)))