searchSymbols
Find symbols in codebase using fuzzy search. Supports partial names, Chinese keywords like '登录' and '支付', and CamelCase prefixes.
Instructions
Fuzzy search for symbols. Supports partial names, Chinese keywords ("登录", "支付"), and CamelCase prefixes.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | Search query |
Implementation Reference
- src/index.ts:484-499 (handler)Handler for the searchSymbols tool. Delegates to codeAnalyzer.smartFindSymbols() and formats the results.
private async handleSearchSymbols(query: string): Promise<{ content: TextContent[] }> { if (!this.codeAnalyzer) { return { content: [{ type: 'text', text: 'Analyzer not initialized' }] }; } const fileName = (f: string) => f.split(/[\/\\]/).pop() || f; const results = this.codeAnalyzer.smartFindSymbols(query, 20); if (results.length === 0) { return { content: [{ type: 'text', text: `找不到匹配 "${query}" 的符号` }] }; } let text = `# 🔍 搜索 "${query}" — 找到 ${results.length} 个匹配\n\n`; for (const sym of results) { const emoji = sym.type === 'class' ? '📦' : '🔧'; text += `- ${emoji} \`${sym.name}\` (${sym.type}) — ${fileName(sym.location.file)}:${sym.location.range.start.line}\n`; } return { content: [{ type: 'text', text }] }; } - Core fuzzy search logic for symbols: supports exact match, keyword mapping (Chinese/English), and substring/prefix scoring.
smartFindSymbols(query: string, limit: number = 10): Symbol[] { if (!query) return []; const lowerQ = query.toLowerCase(); const all = Array.from(this.symbolTable.values()).filter( (s) => s.type === 'function' || s.type === 'class' ); // 1. 精确匹配(不区分大小写) const exact = all.filter((s) => s.name.toLowerCase() === lowerQ); if (exact.length > 0) return exact.slice(0, limit); // 2. 关键字映射 const keywordMap = this.configManager.getKeywords(); const expandedTerms: string[] = [lowerQ]; for (const [keyword, aliases] of Object.entries(keywordMap)) { if (keyword.toLowerCase() === lowerQ || aliases.some((a) => a.toLowerCase() === lowerQ)) { expandedTerms.push(...aliases.map((a) => a.toLowerCase())); } } // 内置中英文关键字映射 const builtin: Record<string, string[]> = { '登录': ['login', 'signin', 'authenticate', 'auth'], '登入': ['login', 'signin', 'authenticate', 'auth'], '注册': ['register', 'signup', 'create'], '支付': ['pay', 'payment', 'charge', 'checkout'], '订单': ['order', 'create-order'], '删除': ['delete', 'remove', 'destroy'], '查询': ['find', 'query', 'search', 'get'], '保存': ['save', 'create', 'persist'], }; if (builtin[query]) { expandedTerms.push(...builtin[query]); } // 3. 子串/前缀匹配,按"匹配质量"打分 // 注意:只允许"符号名包含查询词",不允许反向(避免 "handleLogin" 匹配到 "And") // 且要求查询词至少 3 个字符才做子串匹配 const scored = all.map((s) => { const lname = s.name.toLowerCase(); let score = 0; for (const term of expandedTerms) { if (term.length < 2) continue; if (lname === term) score += 100; else if (lname.startsWith(term)) score += 50; else if (term.length >= 3 && lname.includes(term)) score += 20; } return { sym: s, score }; }); return scored .filter((x) => x.score > 0) .sort((a, b) => b.score - a.score) .slice(0, limit) .map((x) => x.sym); } - src/index.ts:117-130 (registration)Tool registration with name, description, and input schema.
{ name: 'searchSymbols', description: 'Fuzzy search for symbols. Supports partial names, Chinese keywords ("登录", "支付"), and CamelCase prefixes.', inputSchema: { type: 'object' as const, properties: { query: { type: 'string', description: 'Search query', }, }, required: ['query'], }, }, - src/index.ts:175-176 (registration)Tool call routing: dispatches 'searchSymbols' requests to the handler.
case 'searchSymbols': return this.handleSearchSymbols(args.query); - src/types/index.ts:26-148 (schema)Symbol type definition — the return type used by searchSymbols results.
export interface Symbol { id: string; name: string; type: SymbolType; location: Location; scope?: string; parent?: string; signatures?: FunctionSignature[]; docstring?: string; // Java 专用元数据 fqcn?: string; // 全限定类名,例如 com.easymusic.service.PayOrderService fieldType?: string; // 字段类型(解析自 docstring) extendsClass?: string; // class 继承的父类(simple name) implementsInterfaces?: string[]; // class 实现的接口(simple name) } /** 每个文件级别的元数据,用于跨文件解析 */ export interface FileMetadata { filePath: string; language: LanguageType; packageName?: string; // Java package imports: Array<{ // 文件的导入列表 fqcn: string; // 完整路径,如 com.foo.Bar 或 ./services/user simpleName: string; // 短名,如 Bar 或 user }>; } export interface FunctionSignature { name: string; params: Parameter[]; returnType?: string; } export interface Parameter { name: string; type?: string; defaultValue?: string; } export interface CallNode { id: string; symbol: Symbol; calls: Set<string>; calledBy: Set<string>; callType: CallType; confidence: Confidence; isCritical: boolean; criticalPointType?: CriticalPointType; } export interface ImportStatement { source: string; imported: string[]; local?: string; isReExport: boolean; location: Location; } export interface DataFlowEdge { from: string; to: string; variable: string; type: 'parameter' | 'return' | 'assignment' | 'property'; } export interface FlowPath { nodes: Symbol[]; edges: DataFlowEdge[]; confidence: Confidence; } export interface BusinessFlowResult { entryPoint: Symbol; callChain: Symbol[]; dataFlows: FlowPath[]; criticalPoints: Symbol[]; links: { vscode?: string[]; github?: string[]; }; } export interface ProjectConfig { version: string; project: { name: string; description?: string; languages: LanguageType[]; rootPath: string; }; sourceConfig: { include: string[]; exclude: string[]; }; analysisConfig: { maxCallDepth: number; crossFileAnalysis: boolean; includeIndirectCalls: boolean; dataFlowDepth: number; dynamicCallHandling: 'annotate' | 'ignore' | 'warn'; }; linkConfig: { vscode: { enabled: boolean; absolutePaths: boolean; }; github: { enabled: boolean; repository?: string; defaultBranch?: string; useCommitSHA?: boolean; }; }; aliases: Record<string, string>; keywords: Record<string, string[]>; outputConfig: { format: 'json' | 'mermaid' | 'graphviz'; includeDataFlow: boolean; cacheDir: string; cacheTTL: number; }; }