7

I'm trying to use lsp-mode (together with company-lsp) for auto completion in C++. According to the docs[0], recent lsp-mode includes clangd support, so I should be all set when I do (require 'lsp-clients). However, lsp-mode can't find any clients to handle c++-mode, and from the documentation I can't find any way of debugging this.

The Main Problem

Here is the (almost) complete init.el file, which I have produced as a minimal example:

(unless (package-installed-p 'lsp-mode)
  (package-install 'lsp-mode))
(unless (package-installed-p 'company-lsp)
  (package-install 'company-lsp))
(unless (package-installed-p 'lsp-ui)
  (package-install 'lsp-ui))

(require 'company-lsp)
(require 'lsp-mode)
(require 'lsp-ui)
(require 'lsp-clients)

(push 'company-lsp company-backends)
(add-hook 'after-init-hook 'global-company-mode)

(add-hook 'lsp-mode-hook 'lsp-ui-mode)

(when (string= (system-name) "i11pcbarth")
    (setq lsp-clients-clangd-executable "/usr/lib/llvm-7/bin/clangd"))

(add-hook 'c++-mode-hook 'lsp)

Now, when I open a C++ file, I get the message

Unable to find client(s) handling c++-mode. Make sure you have required lsp-clients.el or proper extension.

Since I already do (require 'lsp-clients) above, I'm not sure what to do.

Further Weirdness

Looking into lsp-clients.el, I can see that it should define e.g. the variable lsp-clients-clangd-executable. However, the variable seems not to be defined (I can't find it using C-h v). So, I assume that for some reason, the (require 'lsp-clients) from my init.el did not work. Thus, I require the file again (by typing (require 'lsp-clients) into the current buffer and calling eval-last-sexp). Now, C-h v sees the variable lsp-clients-clangd-executable. However, M-x lsp RET (to enable lsp-mode) again only gives me the error message about no client that handles c++-mode.

[0] https://github.com/emacs-lsp/lsp-clangd/blob/master/README.md

Lukas Barth
  • 271
  • 3
  • 10

1 Answers1

8

EDIT: As of late December 2018, this is no longer required, and lsp-mode does the required setup automatically. It was fixed in this commit as far as I can tell.

You need to call lsp-clients-register-clangd, as per https://github.com/emacs-lsp/lsp-clangd/issues/11. To do this, you can either call it interactively using M-x lsp-clients-register-clangd to enable it in your current emacs session, or call it from your init.el after requiring lsp and lsp-clients. The result should look something like this:

(require 'lsp)
(require 'lsp-clients)
(lsp-clients-register-clangd)
(add-hook 'c++-mode-hook 'lsp)

According to https://github.com/emacs-lsp/lsp-mode/issues/537, this is a temporary issue, and calling lsp-clients-register-clangd should not be necessary in the future.

Lorenz
  • 144
  • 4
  • Welcome! For the sake of completeness, could you please update your answer to be explicit about what code needs to be added? – Dan Dec 12 '18 at 17:40
  • I’m not sure what you mean, Dan. No code is missing from lsp-mode, but its documentation is sparse. You only need to call `lsp-clients-register-clangd` either manually via `M-x lsp-clients-register-clangd` or put `(lsp-clients-register-clangd)` in your init.el after requiring lsp-mode. – Lorenz Dec 13 '18 at 18:54
  • 1
    Please edit your post to explain exactly that. The idea is to make sure someone who sees this post somewhere down the line will be able to figure out exactly what you did to resolve the problem. – Dan Dec 13 '18 at 18:56
  • 1
    Ah, right! I updated it with a full config example that should be sufficient to get it working. – Lorenz Dec 13 '18 at 20:49