Fanteria
Fanteria

Reputation: 87

Enable ClangdSwitchSourceHeader to work with separate source and header files

I'm trying to set up my Neovim/LSP/Clangd setup at work. We use two different ways to store source (.cpp). and header (.h) files. The first way is the common approach, where both the source and header files are placed in the same folder. The second way involves keeping the source files in the lib folder and the headers in the include folder. Additionally, all header files are symlinked in a single folder called all_includes.

The LSP suggestions work perfectly with the example .clangd configuration provided below:

CompileFlags:
Add: [-I../include,
      -I/path/to/folder/with/all/includes/all_includes]

However, the feature of switching between source and header with ClangdSwitchSourceHeader only works in the first approach or from source to header.

Does Clangd have an option to locate the source file in this case? I know that the source file for it is located at the path ../lib. Is there an option to provide this information to Clang, or is there an easier solution that involves creating my own Lua function to solve this problem?

Upvotes: 1

Views: 1676

Answers (1)

HighCommander4
HighCommander4

Reputation: 52819

If the project is indexed (this requires a compile_commands.json file, see https://clangd.llvm.org/installation#project-setup), clangd should generally be able to figure out the matching source/header file based on their contents (e.g. the source file is the file containing the definitions of the functions declared in the header file, that sort of thing).

A filename-based heuristic that works with the source and header files being in different directories is not currently implemented, but that would be useful; there is https://github.com/clangd/clangd/issues/1617 open for it.

Upvotes: 1

Related Questions