Wednesday, January 18, 2023

Emacs lsp-mode multiple completions

Recently I started using lsp-mode with company-mode for completions (Following instructions from How to setup Emacs LSP Mode for Go). I was pretty happy with my configuration which for pretty basic using use-package
(use-package lsp-mode
:ensure t
:config
(setq lsp-log-io nil) ; only enable for debugging
(add-hook 'go-mode-hook #'lsp-deferred)
)
(use-package lsp-ui
:ensure t)
(use-package company
:ensure t
:config
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 1))
But, soon ran into the following issue where 2 completion minibuffers started showing up.
So, did a bit of googling to land in Multiple completion modes enabled. How to fix this?. It did not fix my issue but, pointed me in the right direction. So, I looked at the current values for company-frontends & company-backends in the buffer in question and found that auto-complete-mode was enabled which was the contributor to the lower completion buffer which I wanted to get rid off. I also remembered disabling global-auto-complete-mode
(use-package auto-complete
:ensure t
:init
(ac-config-default)
; (global-auto-complete-mode t)
)
But, the devil is in the details taking a peek into ac-config-default showed the culprit (all the way below in line #10).
;;;###autoload
(defun ac-config-default ()
"No documentation."
(setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers))
(add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup)
(add-hook 'c-mode-common-hook 'ac-cc-mode-setup)
(add-hook 'ruby-mode-hook 'ac-ruby-mode-setup)
(add-hook 'css-mode-hook 'ac-css-mode-setup)
(add-hook 'auto-complete-mode-hook 'ac-common-setup)
(global-auto-complete-mode t))
So, with a little tinkering later we get
(defun fc/lsp-deferred ()
"When enabling lsp-mode disable auto-complete"
(auto-complete-mode -1)
(lsp-deferred))
(use-package lsp-mode
:ensure t
:config
(setq lsp-log-io nil) ; only enable for debugging
(add-hook 'go-mode-hook #'fc/lsp-deferred)
)
view raw lsp-deferred.el hosted with ❤ by GitHub
And, all is well in Emacs :)