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

Class CodeFinder

src/codegraphcontext/tools/code_finder.py:144–1430  ·  view source on GitHub ↗

Module for finding relevant code snippets and analyzing relationships.

Source from the content-addressed store, hash-verified

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

Calls

no outgoing calls