Search code examples
c++neovimclangdnvim-lspconfig

Using clangd in files included verbatim


I am using Neovim with lsp-zero plugin. The following minimal example models a situation I came across in a large code base. The #include statement is used for snippets which repeat throughout the code base and every application has dozens of such files in the main function.

File defs.h:

a = 1;
b = 2;

File app.cpp

#include <iostream>

int main (int argc, char *argv[]) {
    int a, b;
#include "defs.h"
    std::cout << a + b << std::endl;
    return 0;
}

LSP features work in app.cpp but not in defs.h. Is there a way to get some LSP functionality inside the included file? Ideally, I'd like it to correctly process compilation errors as if the file was included in app.cpp and be able to navigate to declarations of each variable.

Notes

Running, LspInfo I noticed that that by default clangd runs in single file mode. I tried defining .clangd and compile_commands.json, but so far no success e.g. using bear I did

bear -- g++ a.cpp

But the LSP functionality was still absent from defs.h.


Solution

  • Clangd does not currently support files which are not self-contained.

    There is an open bug for this in the clangd issue tracker: https://github.com/clangd/clangd/issues/45