Enable Elixir code folding in Doom Emacs

The end result

Code folding recently landed in Elixir's LSP implementation 🎉 and if you want to give it a try in Doom Emacs, here are the steps needed:

Ensure you have the latest Elixir LSP

Support just hit master and has not been pushed in a tagged release. Make sure you have updated your install.

If you have not installed Elixir LSP before – that is outside the scope of this article – but the gist is something like:

git clone https://github.com/elixir-lsp/elixir-ls.git
cd elixir-ls
mix deps.get
mix compile
mix elixir_ls.release

Disable existing fold layer

Doom Emacs has a custom folding layer that marries hideshow, vimish-fold, and outline-minor-mode. Since we need to use origami to get LSP-backed folding support, it is best to comment out the existing folding layer and avoid any potential conflicts.

This is a safety measure, I am not sure if these things can co-exist. Feel free to try and report back in #emacs in the Elixir community Slack.

;; ~/.doom.d/init.el

(doom! 
  ;; ... more config ...
  
  :editor
  ;;fold              ; (nigh) universal code folding
  
  ;; ... more config ...

Enable code folding for the LSP layer with Origami

Add the lsp-origami to the doom packages:

;; ~/.doom.d/packages.el

(package! lsp-origami)

Enable code folding with lsp and set up package hooks:

;; ~/.doom.d/config.el

;; Enable folding
(setq lsp-enable-folding t)

;; Add origami and LSP integration
(use-package! lsp-origami)
(add-hook! 'lsp-after-open-hook #'lsp-origami-try-enable)

Then run doom sync to install the new packages and remove the old one.

Testing it out

Open up an elixir file and use all the usual keybindings z c to collapse, z o to open, z r to open recursively. You can discover all the commands running SPC : and fuzzy searching for commands starting with fold or origami

Bugs

If you run into any bugs with the new folding feature, report it upstream to the Elixir Language Server project.