C++ Include Finder
在 C/C++ 代码中悬停任意单词或标识符,快速查找并插入匹配的头文件。同时提供可配置的模糊匹配。
快速开始
- 在工作区设置好待搜索的目录与插入前缀(见下方配置示例)
- 打开任意 C/C++ 源文件,将鼠标悬停到目标标识符或文件名片段上(不必在引号内)
- 悬停提示会列出匹配的头文件,点击“插入 <...>”即可自动插入 #include 行
说明:
- 匹配逻辑以“文件名第一个点号前”的基名为准。例如:
- 悬停 Foo 可匹配 Foo.h、Foo.hpp、Foo.pb.h、Foo.any.h 等
- 悬停 GetFoo 也可通过 hoverMatchRules 转换为 Foo 再匹配
- 模糊匹配会在精确匹配不足时补充“(模糊)”标记的候选
配置
将以下内容加入工作区 .vscode/settings.json 并按需调整:
{
"cppIncludeFinder.searchRoots": [
{ "dir": "include", "prefix": "test/include/", "useSubpath": true },
{ "dir": "third_party/ProtocolPB", "prefix": "ProtocolPB/", "useSubpath": true },
{ "dir": "src", "prefix": "", "useSubpath": true }
],
"cppIncludeFinder.headerExtensions": ["h", "hpp", "hh", "hxx"],
"cppIncludeFinder.useAngleBrackets": false,
"cppIncludeFinder.maxResults": 30,
"cppIncludeFinder.excludeGlobs": [
"**/.git/**",
"**/node_modules/**",
"**/build/**",
"**/out/**",
"**/bazel-*/**",
"**/cmake-*/**"
],
"cppIncludeFinder.hoverMatchRules": [
{ "type": "glob", "pattern": "Get*", "replacement": "$1" },
{ "type": "regex", "pattern": "^(?:to_)?(.*?)(?:_req|_resp)?$", "replacement": "$1" }
],
"cppIncludeFinder.enableFuzzy": true,
"cppIncludeFinder.fuzzyThreshold": 0.6,
"cppIncludeFinder.fuzzyScanLimitPerRoot": 2000
}
关键项说明:
- cppIncludeFinder.searchRoots
- dir:相对工作区根的搜索目录
- prefix:插入时前缀(例如 third_party/ProtocolPB 对应插入 ProtocolPB/xxx.h)
- useSubpath:true 时保留目录层级,false 仅插入文件名
- cppIncludeFinder.headerExtensions:可匹配的最终扩展名集合。X.pb.h 通过 base.*.h 规则自动覆盖,无需额外配置
- cppIncludeFinder.useAngleBrackets:true 使用尖括号 #include <...>;false 使用双引号
- cppIncludeFinder.hoverMatchRules:将悬停到的词转换为搜索基名的规则(glob 或 regex)
- 模糊匹配相关
- enableFuzzy:开启/关闭模糊匹配
- fuzzyThreshold:匹配阈值(0~1,越高越严格,0.6~0.8 常用)
- fuzzyScanLimitPerRoot:每个根目录参与模糊扫描的上限,避免大仓库卡顿
使用小贴士
- 想匹配 Foo.pb.h,请悬停 Foo(或能通过规则转换得到 Foo 的词),无需输入 .pb
- 想优先某些目录的结果,可在 searchRoots 中将该目录放到更靠前位置(并合理设置 prefix)
- 大型仓库如遇卡顿,可:
- 增加 excludeGlobs(排除 build、out 等)
- 降低 fuzzyScanLimitPerRoot 或暂时关闭 enableFuzzy
命令
- C++ Include Finder: Insert Include(命令 ID: cppIncludeFinder.insertInclude)
- 悬停提示中的“插入 ...”链接即调用该命令,将对应 #include 插入到合适位置