MCPcopy Create free account
hub / github.com/CodeGraphContext/CodeGraphContext / CodeFinder

Class CodeFinder

src/codegraphcontext/tools/code_finder.py:160–1461  ·  view source on GitHub ↗

Module for finding relevant code snippets and analyzing relationships.

Source from the content-addressed store, hash-verified

158
159
160class CodeFinder:
161 """Module for finding relevant code snippets and analyzing relationships."""
162
163 def __init__(self, db_manager: DatabaseManager):
164 self.db_manager = db_manager
165 self._lacks_native_fulltext = getattr(db_manager, 'get_backend_type', lambda: 'neo4j')() != 'neo4j'
166 self._active_graph = None
167
168 @property
169 def driver(self):
170 """Returns driver for the active graph (set via graph_name params), or default."""
171 return self.db_manager.get_driver(self._active_graph)
172
173 def audit_kotlin_call_ambiguity(
174 self,
175 repo_path: Optional[str] = None,
176 limit: int = 20,
177 ) -> Dict[str, Any]:
178 """Audit Kotlin function-to-function CALLS edges for multi-target callsites."""
179 repo_path = Path(repo_path).resolve().as_posix() if repo_path else None
180 repo_filter = "AND a.path STARTS WITH $repo_path" if repo_path else ""
181 query = f"""
182 MATCH (a:Function)-[r:CALLS|HEURISTIC_CALLS]->(b:Function)
183 WHERE a.path ENDS WITH '.kt'
184 AND b.path ENDS WITH '.kt'
185 {repo_filter}
186 RETURN a.name as caller_name,
187 a.path as caller_path,
188 a.line_number as caller_line,
189 a.end_line as caller_end_line,
190 r.line_number as call_line,
191 r.full_call_name as full_call_name,
192 b.name as target_name,
193 b.path as target_path,
194 b.line_number as target_line,
195 b.context as target_context,
196 r.args as args
197 """
198 with self.driver.session() as session:
199 rows = session.run(query, repo_path=repo_path).data()
200 return summarize_kotlin_call_ambiguity(rows, limit=limit)
201
202 def format_query(self, find_by: Literal["Class", "Function"], fuzzy_search:bool, repo_path: Optional[str] = None) -> str:
203 """Format the search query based on the search type and fuzzy search settings."""
204 repo_filter = "AND node.path STARTS WITH $repo_path" if repo_path else ""
205 if self._lacks_native_fulltext:
206 # FalkorDB does not support CALL db.idx.fulltext.queryNodes.
207 # Fall back to a pure Cypher CONTAINS/toLower match on node name.
208 name_filter = "toLower(node.name) CONTAINS toLower($search_term)"
209 return f"""
210 MATCH (node:{find_by})
211 WHERE {name_filter} {repo_filter}
212 RETURN node.name as name, node.path as path, node.line_number as line_number,
213 node.source as source, node.docstring as docstring, node.is_dependency as is_dependency
214 ORDER BY node.is_dependency ASC, node.name
215 LIMIT 20
216 """
217 return f"""

Calls

no outgoing calls