From efdb46ec570b229b64ccf280533e902d6da943ab Mon Sep 17 00:00:00 2001 From: Tobias Heinlein Date: Sat, 26 Oct 2024 10:45:04 +0200 Subject: [PATCH] feat(LSP): Allow users to manage Company backends manually Why?: - Currently lsp-mode aggressively inserts the company-capf backend, which contains its completion-at-point-function, as the ultimate first item of the list of company-backends. If a language server does not provide all the functionality, that could otherwise be provided by another Company backend like company-files for example, lsp-mode will practically block any other Company backend inside company-backends from running, because it tells Company that it may be able to complete at point but in the end doesn't provide any completion candidates. This is not ideal and users may want to manage company-backends by themselves. Also using this variable within some language layers; we could also be able to provide a more focused list of Company backends for that mode that extends the functionality currently not provided by the language server. This change addresses the need by: - Introduce new lsp-layer variable lsp-manage-backends-manually, which can either be :all (to manage the backends for every major-mode manually), a list of major-modes (to manage backends for these specific modes manually) or nil to let lsp-mode manage the company-backends - Modify lsp/init-lsp-mode to set lsp-completion-provider based on the value of lsp-manage-backends-manually --- layers/+tools/lsp/config.el | 3 +++ layers/+tools/lsp/packages.el | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/layers/+tools/lsp/config.el b/layers/+tools/lsp/config.el index 6d78c572660e..235746fbb7ae 100644 --- a/layers/+tools/lsp/config.el +++ b/layers/+tools/lsp/config.el @@ -40,3 +40,6 @@ If `both', binds lightweight navigation functions under `SPC m g' and lsp-ui fun (defvar lsp-use-upstream-bindings nil "When non-nil, map keys to `lsp-command-map'.") (defvar lsp-sonarlint nil "When non-nil, use `lsp-sonarlint' package.") + +(defvar lsp-manage-backends-manually nil "When non-nil lsp-mode does not insert `company-capf' as the ultimate first item of `company-backends'. +`lsp-manage-backends-manually' can either be `:all' or a list of major-modes that should be managed manually.") diff --git a/layers/+tools/lsp/packages.el b/layers/+tools/lsp/packages.el index db9ef88dd768..9ed63ab012b4 100644 --- a/layers/+tools/lsp/packages.el +++ b/layers/+tools/lsp/packages.el @@ -56,6 +56,10 @@ (if lsp-use-upstream-bindings (spacemacs/lsp-bind-upstream-keys) (spacemacs/lsp-bind-keys)) + (setq lsp-completion-provider (if (or (equal :all lsp-manage-backends-manually) + (member major-mode lsp-manage-backends-manually)) + :none + :capf)) ;; This sets the lsp indentation for all modes derived from web-mode. (add-to-list 'lsp--formatting-indent-alist '(web-mode . web-mode-markup-indent-offset)) (add-hook 'lsp-after-open-hook (lambda ()