The instructions on https://preview.dune.build configures dune preview for emacs via lsp-mode.
I was able to configure eglot to use dune tools exec ocamllsp with the following configuration. The snippet configures: tuareg, eglot (for dune preview), ocaml-eglot (copied from its official README).
Caveat: After tracing the relevant portions of codebases, I figured out that all eglot does is send a formatting request to ocamlformat isn’t working. I don’t know where I can specify dune tools exec ocamlformat in this setup. Any help is appreciated.ocamllsp because ocamllsp claims to provide formatting capabilities. The onus of finding ocamlformat is on ocamllsp, which simply looks for ocamlformat binary on PATH.
I didn’t find a “proper” way (like a command line argument) to pass the path of ocamlformat binary to ocamllsp. So, updating PATH it is. PATH="$(dirname $(dune tools which ocamlformat)):$PATH" emacs from terminal does the job. I would appreciate a better approach than this.
(use-package tuareg
:ensure t)
;; configure eglot's ocaml setup to use `dune tools exec ocamllsp`
;; TODO: ocamlformat has stopped working.
;; I don't know how to configure `dune tools exec ocamlformat`.
(use-package eglot
:config
(add-to-list 'eglot-server-programs
'(((caml-mode :language-id "ocaml")
(ocaml-ts-mode :language-id "ocaml")
(tuareg-mode :language-id "ocaml") reason-mode)
. ("dune" "tools" "exec" "ocamllsp"))))
(use-package ocaml-eglot
:ensure t
:after tuareg
:hook
(tuareg-mode . ocaml-eglot)
(ocaml-eglot . eglot-ensure)
(ocaml-eglot . (lambda ()
(add-hook #'before-save-hook #'eglot-format nil t))))
Now launch emacs from terminal with an updated PATH so that ocamllsp can find ocamlformat binary in emacs.
$ PATH="$(dirname $(dune tools which ocamlformat)):$PATH" emacs