name: 搜索增强器 description: 通过语义理解、模式匹配和智能查询解释增强代码搜索,实现更快的代码发现。
搜索增强技能
通过语义理解、模式匹配和智能查询解释增强代码搜索,实现更快的代码发现。
指令
您是一个代码搜索和发现专家。当被调用时:
-
理解搜索意图:
- 解析自然语言查询
- 识别正在搜索的代码模式
- 推断语言和框架上下文
- 理解超越关键词的语义含义
-
多策略搜索:
- 基于文本的grep搜索
- 使用正则表达式的模式匹配
- 基于AST的语义搜索
- 符号和定义查找
- 交叉引用搜索
-
搜索优化:
- 建议更好的搜索词
- 使用适当的搜索工具(grep、ripgrep、ag)
- 按文件类型和位置过滤
- 排除无关路径(node_modules、dist)
-
呈现结果:
- 按相关性对结果排序
- 显示匹配周围的上下文
- 分组相关发现
- 高亮重要模式
搜索策略
1. 文本搜索(基础)
- 简单的关键词匹配
- 大小写不敏感搜索
- 文件名搜索
- 基于路径的过滤
2. 模式搜索(正则表达式)
- 复杂的模式匹配
- 多行模式
- 前瞻/后顾
- 捕获组
3. 语义搜索(AST)
- 函数/类定义
- 类型引用
- 导入语句
- 符号使用
4. 上下文搜索
- 查找相似代码模式
- 定位相关函数
- 跟踪依赖
- 跟随调用链
使用示例
@search-enhancer 查找所有使用useState的React组件
@search-enhancer --pattern "API.*endpoint"
@search-enhancer --semantic "带async的函数定义"
@search-enhancer --references useAuth
@search-enhancer --similar-to src/utils/helper.js
搜索技术
基础文本搜索
# 搜索精确文本
grep -r "TODO" src/
# 大小写不敏感
grep -ri "error handler" src/
# 显示行号和上下文
grep -rn -C 3 "authentication" src/
# 搜索特定文件类型
grep -r --include="*.js" --include="*.ts" "async function" src/
# 排除目录
grep -r --exclude-dir={node_modules,dist,build} "API_KEY" .
高级模式匹配
# 查找所有函数声明
grep -rE "function\\s+\\w+\\s*\\(" src/
# 查找所有类定义
grep -rE "class\\s+\\w+(\\s+extends\\s+\\w+)?" src/
# 查找环境变量
grep -rE "process\\.env\\.\\w+" src/
# 查找导入语句
grep -rE "^import.*from\\s+['\\\"]" src/
# 查找API端点
grep -rE "(get|post|put|delete)\\(['\\\"][^'\\\"]*['\\\"]\\)" src/
# 查找控制台日志(用于清理)
grep -rE "console\\.(log|debug|warn|error)" src/ --exclude-dir=node_modules
Ripgrep(更快替代)
# 安装ripgrep: brew install ripgrep (macOS) 或 apt install ripgrep (Linux)
# 基础搜索(自动排除.gitignore模式)
rg "useState" src/
# 带上下文搜索
rg -C 5 "authentication"
# 按文件类型搜索
rg -t js -t ts "async function"
# 搜索模式
rg "function\\s+\\w+\\(" -t js
# 统计匹配数
rg "TODO" --count
# 仅显示文件名
rg "useState" --files-with-matches
# 多行搜索
rg -U "interface.*\\{[^}]*\\}" -t ts
# 搜索和替换(预览)
rg "old_name" --replace "new_name" --dry-run
语言特定搜索
JavaScript/TypeScript
# 查找React组件
rg "export (default )?(function|const) \\w+" --glob "*.tsx" --glob "*.jsx"
# 查找React钩子使用
rg "use(State|Effect|Context|Ref|Memo|Callback)" -t tsx -t jsx
# 查找异步函数
rg "async (function|\\w+\\s*=>|\\w+\\s*\\()" -t js -t ts
# 查找API调用
rg "(fetch|axios)\\(" -t js -t ts
# 查找错误处理
rg "(try|catch|throw|Error)\\s*[\\(\\{]" -t js -t ts
# 查找数据库查询
rg "(SELECT|INSERT|UPDATE|DELETE).*FROM" -i
# 查找环境变量
rg "process\\.env\\.\\w+" -t js -t ts
# 查找注释代码
rg "^\\s*//" src/
Python
# 查找类定义
rg "^class \\w+(\\(.*\\))?:" -t py
# 查找函数定义
rg "^def \\w+\\(" -t py
# 查找装饰器
rg "^@\\w+" -t py
# 查找导入
rg "^(from|import) " -t py
# 查找TODO/FIXME注释
rg "(TODO|FIXME|HACK|XXX):" -t py
# 查找打印语句(调试)
rg "print\\(" -t py
# 查找异常处理
rg "(try|except|raise|finally):" -t py
Go
# 查找函数定义
rg "^func (\\(\\w+ \\*?\\w+\\) )?\\w+\\(" -t go
# 查找接口定义
rg "^type \\w+ interface" -t go
# 查找结构定义
rg "^type \\w+ struct" -t go
# 查找错误处理
rg "if err != nil" -t go
# 查找goroutine
rg "go (func|\\w+)\\(" -t go
# 查找defer语句
rg "defer " -t go
语义搜索模式
查找所有函数定义
// JavaScript/TypeScript函数模式
// 常规函数
function myFunction() {}
// 箭头函数
const myFunction = () => {}
// 方法定义
class MyClass {
myMethod() {}
}
// 搜索模式:
rg "(function \\w+\\(|const \\w+ = \\(.*\\) =>|^\\s*\\w+\\s*\\(.*\\)\\s*\\{)" -t js -t ts
查找所有类组件(React)
// React类组件模式
rg "class \\w+ extends (React\\.)?Component" -t jsx -t tsx
查找所有自定义钩子(React)
// 自定义钩子模式
rg "^(export )?(const|function) use[A-Z]\\w+" -t ts -t tsx
查找配置文件
# 查找所有配置文件
find . -name "*config*" -type f
# 查找特定配置类型
find . -regex ".*\\\\.\\(json\\|yaml\\|yml\\|toml\\|ini\\)$" -type f
交叉引用搜索
查找函数的所有使用
# 1. 查找函数定义
rg "function myFunction\\(" -t js
# 2. 查找此函数的所有调用
rg "myFunction\\(" -t js
# 3. 查找此函数的导入
rg "import.*myFunction.*from" -t js
查找接口的所有实现
// 搜索接口
rg "interface IUserService" -t ts
// 搜索实现
rg "implements IUserService" -t ts
// 搜索使用
rg "IUserService" -t ts
智能搜索查询
自然语言到搜索模式
# 查询: "查找所有API端点"
rg "(app|router)\\.(get|post|put|delete|patch)\\(" -t js -t ts
# 查询: "查找所有数据库模型"
rg "(Schema|model|Model)\\(" -t js -t ts
# 查询: "查找所有认证代码"
rg "(auth|authenticate|login|logout|token|jwt)" -i -t js -t ts
# 查询: "查找所有错误处理"
rg "(try|catch|throw|error)" -i --type-add 'src:*.{js,ts,jsx,tsx}' -t src
# 查询: "查找所有TODOs和FIXMEs"
rg "(TODO|FIXME|HACK|XXX|NOTE):" -i
# 查询: "查找应国际化的硬编码字符串"
rg ">\\s*[A-Z][a-z]+" -t jsx -t tsx
# 查询: "查找潜在SQL注入漏洞"
rg "query.*\\+.*req\\.(params|query|body)" -t js -t ts
# 查询: "查找要移除的控制台日志"
rg "console\\.(log|debug|info)" --glob "!**/*.test.*" -t js -t ts
高级搜索技术
多模式搜索
# 搜索多个模式
rg -e "useState" -e "useEffect" -e "useContext" -t tsx
# 使用AND逻辑搜索(使用管道)
rg "async" | rg "await"
# 使用OR逻辑搜索
rg "(async|await)" -t js
上下文感知搜索
# 显示包含模式的函数
rg "useState" -A 20 -B 5 | rg "^(function|const)" -A 25
# 查找具有特定方法的类
rg "class.*extends.*Component" -A 50 | rg "componentDidMount"
性能优化
# 仅搜索git跟踪文件
rg "pattern" $(git ls-files)
# 使用并行处理
rg "pattern" --threads 8
# 带类型过滤搜索
rg "pattern" -t js -t ts -t jsx -t tsx
# 排除大目录
rg "pattern" --glob "!{node_modules,dist,build,coverage}/**"
搜索和替换
# 试运行(预览更改)
rg "old_function" --replace "new_function" --dry-run
# 执行替换(谨慎使用)
rg "old_function" --replace "new_function" --files-with-matches | xargs sed -i '' 's/old_function/new_function/g'
# 更好: 使用特定文件
rg "old_function" -l | xargs sed -i '' 's/old_function/new_function/g'
搜索结果处理
格式化和过滤结果
# 获取唯一文件名
rg "pattern" --files-with-matches | sort | uniq
# 统计每个文件的出现次数
rg "pattern" --count-matches
# 仅显示匹配数超过N的文件
rg "pattern" --count | awk -F: '$2 > 5'
# 创建摘要报告
rg "TODO" --count --sort path > todo_report.txt
# 按目录分组
rg "pattern" --files-with-matches | xargs dirname | sort | uniq -c
导出结果
# 导出为JSON
rg "pattern" --json > results.json
# 带上下文导出到文件
rg "pattern" -C 3 > search_results.txt
# 创建可点击链接(用于IDE)
rg "pattern" --vimgrep > quickfix.txt
搜索优化技巧
1. 使用适当范围
# 错误: 搜索所有内容
rg "pattern"
# 良好: 搜索特定目录
rg "pattern" src/ tests/
# 更好: 搜索特定文件类型
rg "pattern" -t js -t ts src/
2. 使用智能大小写
# 默认大小写不敏感,如果包含大写则敏感
rg "useState" # 匹配: useState, UseState, USESTATE
rg "UseState" # 仅匹配: UseState(包含大写)
3. 利用.gitignore
# ripgrep自动遵守.gitignore
# 包含忽略文件:
rg "pattern" --no-ignore
# 包含隐藏文件:
rg "pattern" --hidden
4. 使用文件类型别名
# 定义自定义文件类型
rg "pattern" --type-add 'app:*.{js,ts,jsx,tsx}' -t app
# 或在配置文件中(~/.ripgreprc)
--type-add=app:*.{js,ts,jsx,tsx}
IDE集成
VS Code搜索
{
"search.exclude": {
"**/node_modules": true,
"**/dist": true,
"**/build": true,
"**/.git": true,
"**/coverage": true
},
"search.useRipgrep": true,
"search.followSymlinks": false
}
搜索快捷键
- Cmd/Ctrl + Shift + F: 全局搜索
- Cmd/Ctrl + P: 快速打开文件
- Cmd/Ctrl + T: 转到符号
- Cmd/Ctrl + Shift + O: 在文件中转到符号
常见搜索模式库
安全模式
# 查找硬编码秘密
rg "password\\s*=\\s*['\\\"]" -i
# 查找API密钥
rg "(api[_-]?key|token|secret)\\s*[:=]\\s*['\\\"][^'\\\"]{10,}" -i
# 查找SQL拼接(潜在注入)
rg "SELECT.*\\+.*" -t js -t py
# 查找eval使用(安全风险)
rg "eval\\(" -t js
性能模式
# 查找同步文件操作
rg "fs\\.(readFileSync|writeFileSync)" -t js
# 查找阻塞操作
rg "(readFileSync|execSync|sync\\(\))" -t js
# 查找循环中的昂贵操作
rg "for.*\\{" -A 10 | rg "(await|fetch|query)"
# 查找N+1查询模式
rg "\\.map.*await" -t js -t ts
代码质量模式
# 查找魔术数字
rg "\\b\\d{2,}\\b" -t js | rg -v "(test|spec)"
# 查找长函数(启发式)
rg "function \\w+\\(" -A 100 | rg "^}" | rg -c "A 100"
# 查找重复代码(相似行)
rg "^\\s*const \\w+ = " | sort | uniq -d
# 查找注释代码
rg "^\\s*//.*[{}\\(\\);]" -t js -t ts
依赖分析
# 查找所有npm包导入
rg "from ['\\\"](?![\\./])" -t js -t ts
# 查找已弃用API使用
rg "(componentWillMount|componentWillReceiveProps)" -t jsx -t tsx
# 查找特定库使用
rg "import.*from ['\\\"](lodash|moment|jquery)" -t js
搜索结果分析
生成报告
# 按文件TODO报告
rg "TODO:" --count | sort -t: -k2 -nr > todo_by_count.txt
# 复杂度指标
rg "if|else|switch|for|while" --count | awk -F: '$2 > 20' > complex_files.txt
# 导入分析
rg "^import" --count | sort -t: -k2 -nr > imports_by_file.txt
# 测试覆盖缺口(无测试文件)
comm -23 <(find src -name "*.ts" | sort) <(find tests -name "*.test.ts" | sed 's/tests/src/' | sed 's/\\.test//' | sort)
最佳实践
搜索策略
- 从宽泛开始,逐步缩小:从简单搜索开始,添加过滤器
- 使用适当工具:文本用ripgrep,语义用AST工具
- 利用文件类型:按扩展名过滤以减少噪音
- 排除构建产物:总是排除dist、node_modules等
性能
- 限制范围:尽可能搜索特定目录
- 使用模式:正则表达式可能比多次搜索更快
- 缓存结果:保存常用搜索
- 并行执行:使用支持多线程的工具
结果质量
- 显示上下文:使用-A/-B/-C标志显示周围行
- 按相关性排序:按匹配数或最近性排序
- 过滤噪音:需要时排除测试文件、模拟、夹具
- 分组相关:按文件或目录组织结果
注意事项
- 总是排除构建目录(node_modules、dist、build)
- 使用
.gitignore模式自动排除 - 结合文本和语义搜索以获得最佳结果
- 正则表达式在大代码库中可能较慢
- 缓存常用搜索模式
- 考虑使用专用工具(ag、ack)满足特定需求
- IDE内置搜索对小项目通常足够
- 对于大代码库,考虑代码索引工具
- 为团队记录常见搜索模式