Example of using LSP server in Emacs

Hi all,

I’d be grateful if you share you emacs configs using ocaml-lsp-server.

Many thanks in advence :slight_smile:

I don’t use LSP on a day-to-day basis, but I tried it in the past and, from memory, installing Eglot GitHub - joaotavora/eglot: A client for Language Server Protocol servers, having ocaml-lsp-server in the PATH and activating the mode with M-x eglot was all that was needed. Note that Eglot is even included in Emacs 29.


Thanks for this topic! :))

Have you been able to activate eglot, following the nice advice by njob?

Among the people working on editor support in OCaml, we’re also quite curious about the experience of folks using Emacs or (Neo-)Vim with OCaml LSP.

Btw, there’s currently a small limitation for OCaml LSP support in Emacs/(Neo-)Vim. Hopefully, using OCaml LSP in Emacs is anyways going to be a good experience for you. But for completeness, here’s the full picture: LSP allows extending the original protocal with language-specific features. And OCaml LSP does have a few OCaml-specific extensions (no guarantee that that list is exhaustive). In order to make use of those extensions in Emacs, we’d need an extension of eglot with support for those OCaml custom features. As far as I know, that doesn’t exist yet (but might be something very useful to work on!). Also, there are a few LSP design choices that make native LSP slightly more suitable for visual editors such as VSCode. Again, to fix that in Emacs, we’d need to extend eglot (and similarly for (Neo-)Vim). That being said, the native LSP protocol is already quite expressive and all OCaml key editor features are expressed via it, so hopefully you’ll have a good experience using OCaml LSP in Emacs also without OCaml-specific eglot extension.

Btw, we’d be curious to know what the experience for folks with OCaml LSP on Emacs is!

1 Like

I wrote a blog post about my setup OCaml with Emacs in 2022 · Perpetually Curious Blog The only change I’ve made is to use envrc-mode rather than direnv-mode.

Thanks. Looks exactly like what I need.

Super, please report back on your progress. The OCaml support in Emacs is very good.

Here is my current init.el file

;; Setup envrc to use direnv configuration in the shell.
;; requires an .envrc file
(use-package envrc
  :hook (after-init . envrc-global-mode))

;; Use merlin mode for navigation.
(use-package merlin
  :ensure t
  (define-key merlin-mode-map (kbd "M-.") #'merlin-locate)
  (define-key merlin-mode-map (kbd "M-,") #'merlin-pop-stack))

(use-package lsp-mode
  ;; set prefix for lsp-command-keymap (few alternatives - "C-l", "C-c l")
  (setq lsp-keymap-prefix "C-c l")
  :hook ((tuareg-mode . lsp-deferred)
         ;; if you want which-key integration
         (lsp-mode . lsp-enable-which-key-integration))
  :commands (lsp lsp-deferred))

;; ;; ;; optionally
;; (use-package lsp-ui :commands lsp-ui-mode)
;; ;; ;; if you are helm user
;; ;; (use-package helm-lsp :commands helm-lsp-workspace-symbol)
;; ;; ;; if you are ivy user
;; (use-package lsp-ivy :commands lsp-ivy-workspace-symbol)
;; ;; (use-package lsp-treemacs :commands lsp-treemacs-errors-list)

;; DAP support for LLDB
(require 'dap-mode)
(require 'dap-codelldb)                 ; load lldb adapter for OCaml native
(require 'dap-ocaml)                    ; load earlybird bytecode adapter

;; Reuse mappings from https://v2.ocaml.org/manual/debugger.html#s:inf-debugger
(use-package dap-mode
  (dap-print-io t)
  :bind (("C-c M-n" . dap-next)
         ("C-c M-s" . dap-step-in)
         ("C-c M-a" . dap-step-out)
         ("C-c M-w" . dap-continue)))

@pitag I think we could get by with a small ocaml-lsp mode that provides those OCaml specific extensions. That is how Rust and Haskell provide their language specific pieces.

@pitag I think we could get by with a small ocaml-lsp mode that provides those OCaml specific extensions.

Thanks, @lambda_foo! That ocaml-lsp mode would then serve to extend either lsp-mode or eglot, right? If so, that sounds exactly like what we “need”/want. We just need to find someone who’d like to implement it. If there’s anyone who might be interested, please do let us know! Would be highly appreciated.

Btw, it would also be interesting to know how important it is for Emacs users to have such an extension and how satisfying eglot/lsp-mode are without it. This post, going slightly off-topic from the original purpose of the thread, isn’t the best way to find out, though. We might try to find out via more suitable ways some time soon-ish.