04 May 2024

Orderless completion in lsp-mode

If you, like me, are using corfu to get in-buffer completion and extend it with orderless to make it even more powerful, you might have noticed that you lose the orderless style as soon as you enter lsp-mode.

My setup of orderless looks like this

(use-package orderless
  :custom
  (orderless-matching-styles '(orderless-literal orderless-regexp orderless-flex))
  (completion-styles '(orderless partial-completion basic))
  (completion-category-defaults nil)
  (completion-category-overrides '((file (styles partial-completion)))))

which basically turns on orderless style for all things except when completing filenames.

It turns out that lsp-mode messes around with completion-category-defaults and when entering lsp-mode this code here adds a setting for 'lsp-capf. Unfortunately there seems to be no way to prevent lsp-mode from doing this so the only option is to fix it up afterwards. Luckily there's a hook for running code after the completion for lsp-mode is set up, lsp-completion-mode-hook. Adding the following function to it makes sure I now get to enjoy orderless also when writing code.

(lambda ()
  (setq-local completion-category-defaults
              (assoc-delete-all 'lsp-capf completion-category-defaults)))
Tags: emacs lsp
Comment here.