clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
639 stars 113 forks source link

`json` config with glob pattern and editor variables support #650

Open Hedgefog opened 4 months ago

Hedgefog commented 4 months ago

Hi there,

I've recently started using clangd and noticed a lack of support for glob patterns in include paths. This feature is crucial for projects with large libraries and frameworks that have complex nested directory structures.

Currently, the C++ Language Server uses the c_cpp_properties.json file, which supports glob patterns and vscode variables. Here is an example configuration:

{
  "env": {
    "_BUILD_PLATFORM_DIR": "${config:myproject.buildPlatform}",
    "_GLOB_HEADERS_DIR": "{,headers,classes}",
    "_GLOB_LIBRARY_CORE_DIR": "{common,core}",
    "_GLOB_LIBRARY_MODULES_DIR": "{modules,plugins}",
    "_GLOB_LIBRARY_SOURCE_PATH": "${config:myproject.libraryPath}/src",
    "_GLOB_LIBRARY_INCLUDE_PATH": "${_GLOB_LIBRARY_SOURCE_PATH}/${_GLOB_LIBRARY_CORE_DIR}/*/${_GLOB_HEADERS_DIR}",
    "_GLOB_LIBRARY_MODULES_INCLUDE_PATH": "${_GLOB_LIBRARY_SOURCE_PATH}/${_GLOB_LIBRARY_MODULES_DIR}/*/src/${_GLOB_HEADERS_DIR}",
    "_INCLUDE_PATH": [
      "${workspaceFolder}/src",
      "${_GLOB_LIBRARY_SOURCE_PATH}",
      "${_GLOB_LIBRARY_INCLUDE_PATH}",
      "${_GLOB_LIBRARY_MODULES_INCLUDE_PATH}"
    ]
  },
  "configurations": [
    {
      "name": "Win64",
      "compilerPath": "${config:myproject.msvcPath}/bin/Hostx64/x64/cl.exe",
      "intelliSenseMode": "msvc-x64",
      "cStandard": "c17",
      "cppStandard": "c++20",
      "includePath": [
        "${env:_INCLUDE_PATH}"
      ]
    }
  ]
}

env section allows set environment variables used during compilation. config:* - variables reference settings from the settings.json file. env:* - environment variables (can be defined within the env section)

VSCode Glob Pattern References: https://code.visualstudio.com/docs/editor/glob-patterns https://code.visualstudio.com/docs/editor/codebasics#_advanced-search-options

I think integrating support for glob patterns and variables would greatly enhance its utility for large projects.

HighCommander4 commented 4 months ago

Clangd expects to get information about include paths from a compile_commands.json file, as described at https://clangd.llvm.org/installation#project-setup.

This file is intended to be generated by the build system, and should reflect the same commands used to actually compile the files.

The idea behind this approach is that information about the project's include paths, is only specified in one place: wherever the build system gets it from (often a metadata file like CMakeLists.txt). The build system then uses the same information both for the actual build, and for generating the compile_commands.json file which clangd then uses. If you make a modification such as adding a new include path, you only have to make it in one place (e.g. in CMakeLists.txt); it's not necessary to make the update in a second place which is specific to the editor.