-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsearch_engine.py
More file actions
404 lines (333 loc) · 15.7 KB
/
search_engine.py
File metadata and controls
404 lines (333 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
Semantic code search engine using tree-sitter parsing.
Provides sophisticated pattern matching beyond simple text search,
leveraging syntax tree structure for accurate code understanding.
"""
from typing import List, Optional, Dict, Any, Set, Tuple
from pathlib import Path
import os
import fnmatch
from tree_sitter import Node, Query
from tree_sitter_language_pack import get_parser, get_language
from .models import SearchResult, SearchParameters
from .file_reader import get_file_content
from .languages import get_language_for_file
class SearchEngine:
"""
Core search engine that executes tree-sitter queries against code files.
Supports caching of parsed ASTs and compiled queries for performance.
"""
def __init__(self):
self._ast_cache: Dict[str, Any] = {} # file_hash -> parsed_tree
self._query_cache: Dict[str, Query] = {} # (lang, pattern) -> compiled_query
def search_file(self, file_path: str, params: SearchParameters) -> List[SearchResult]:
"""Search a single file for the specified pattern."""
try:
# Get language
lang_name = params.language or get_language_for_file(file_path)
if lang_name == 'text':
return [] # Skip unsupported languages
# Get file content
source_code = get_file_content(file_path, params.git_revision)
if not source_code.strip():
return []
# Get or create parser
parser = get_parser(lang_name)
tree = parser.parse(source_code.encode('utf-8'))
# Route to appropriate search method
if params.search_type == "function-calls":
return self._search_function_calls(file_path, source_code, tree, params, lang_name)
elif params.search_type == "symbol-definitions":
return self._search_symbol_definitions(file_path, source_code, tree, params, lang_name)
return []
except Exception as e:
# Log error but don't crash
print(f"Error searching {file_path}: {e}")
return []
def search_directory(self, directory_path: str, params: SearchParameters) -> List[SearchResult]:
"""Search all matching files in a directory tree."""
try:
dir_path = Path(directory_path)
if not dir_path.exists() or not dir_path.is_dir():
print(f"Directory not found or not a directory: {directory_path}")
return []
# Get all matching files
matching_files = self._find_matching_files(dir_path, params)
if len(matching_files) > params.max_files:
print(f"Found {len(matching_files)} files, limiting to {params.max_files}")
matching_files = matching_files[:params.max_files]
# Search each file and aggregate results
all_results = []
for file_path in matching_files:
try:
file_results = self.search_file(str(file_path), params)
all_results.extend(file_results)
# Check if we've hit the max results limit
if len(all_results) >= params.max_results:
all_results = all_results[:params.max_results]
break
except Exception as e:
print(f"Error searching file {file_path}: {e}")
continue
# Deduplicate and sort results
return self._deduplicate_results(all_results)
except Exception as e:
print(f"Error searching directory {directory_path}: {e}")
return []
def _search_function_calls(self, file_path: str, source_code: str, tree: Any,
params: SearchParameters, lang_name: str) -> List[SearchResult]:
"""Search for function calls in the parsed tree."""
results = []
# Define query patterns for different languages
patterns = {
'python': '''
; Method calls like obj.method()
(call
function: (attribute
(identifier) @module
(identifier) @function
)
) @call
; Simple function calls like func()
(call
function: (identifier) @simple_function
) @simple_call
''',
'javascript': '''
; Method calls like obj.method()
(call_expression
function: (member_expression
object: (identifier) @module
property: (property_identifier) @function
)
) @call
; Simple function calls like func()
(call_expression
function: (identifier) @simple_function
) @simple_call
''',
'typescript': '''
; Method calls like obj.method()
(call_expression
function: (member_expression
object: (identifier) @module
property: (property_identifier) @function
)
) @call
; Simple function calls like func()
(call_expression
function: (identifier) @simple_function
) @simple_call
'''
}
pattern = patterns.get(lang_name)
if not pattern:
return []
# Compile and execute query
query = self._get_compiled_query(lang_name, pattern)
captures = query.captures(tree.root_node)
source_lines = source_code.splitlines()
for node, capture_name in captures:
if capture_name in ['call', 'simple_call']:
# Check if this matches our target
call_text = source_code[node.start_byte:node.end_byte]
if params.target in call_text:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
# Get context lines
context_before = []
context_after = []
if params.include_context:
start_ctx = max(0, start_line - 1 - params.context_lines)
end_ctx = min(len(source_lines), end_line + params.context_lines)
context_before = source_lines[start_ctx:start_line-1]
context_after = source_lines[end_line:end_ctx]
result = SearchResult(
file_path=file_path,
start_line=start_line,
end_line=end_line,
match_text=call_text,
context_before=context_before,
context_after=context_after,
metadata={"search_type": params.search_type, "target": params.target},
language=lang_name
)
results.append(result)
if len(results) >= params.max_results:
break
return results
def _search_symbol_definitions(self, file_path: str, source_code: str, tree: Any,
params: SearchParameters, lang_name: str) -> List[SearchResult]:
"""Search for symbol definitions (classes, functions, variables) in the parsed tree."""
results = []
# Define query patterns for different languages
patterns = {
'python': '''
; Function definitions
(function_definition
name: (identifier) @function_name
) @function_def
; Class definitions
(class_definition
name: (identifier) @class_name
) @class_def
; Variable assignments
(assignment
left: (identifier) @variable_name
) @variable_def
''',
'javascript': '''
; Function declarations
(function_declaration
name: (identifier) @function_name
) @function_def
; Class declarations
(class_declaration
name: (identifier) @class_name
) @class_def
; Variable declarations
(variable_declaration
(variable_declarator
name: (identifier) @variable_name
)
) @variable_def
; Const declarations
(lexical_declaration
(variable_declarator
name: (identifier) @variable_name
)
) @const_def
''',
'typescript': '''
; Function declarations
(function_declaration
name: (identifier) @function_name
) @function_def
; Class declarations
(class_declaration
name: (identifier) @class_name
) @class_def
; Interface declarations
(interface_declaration
name: (type_identifier) @interface_name
) @interface_def
; Type alias declarations
(type_alias_declaration
name: (type_identifier) @type_name
) @type_def
; Variable declarations
(variable_declaration
(variable_declarator
name: (identifier) @variable_name
)
) @variable_def
; Const declarations
(lexical_declaration
(variable_declarator
name: (identifier) @variable_name
)
) @const_def
'''
}
pattern = patterns.get(lang_name)
if not pattern:
return []
# Compile and execute query
query = self._get_compiled_query(lang_name, pattern)
captures = query.captures(tree.root_node)
source_lines = source_code.splitlines()
for node, capture_name in captures:
if capture_name.endswith('_def'):
# Check if this symbol name matches our target
symbol_text = source_code[node.start_byte:node.end_byte]
# For symbol definitions, we want to check if the target appears in the symbol
if params.target in symbol_text:
start_line = node.start_point[0] + 1
end_line = node.end_point[0] + 1
# Get context lines
context_before = []
context_after = []
if params.include_context:
start_ctx = max(0, start_line - 1 - params.context_lines)
end_ctx = min(len(source_lines), end_line + params.context_lines)
context_before = source_lines[start_ctx:start_line-1]
context_after = source_lines[end_line:end_ctx]
# Determine symbol type from capture name
symbol_type = capture_name.replace('_def', '').replace('_name', '')
result = SearchResult(
file_path=file_path,
start_line=start_line,
end_line=end_line,
match_text=symbol_text,
context_before=context_before,
context_after=context_after,
metadata={
"search_type": params.search_type,
"target": params.target,
"symbol_type": symbol_type
},
language=lang_name
)
results.append(result)
if len(results) >= params.max_results:
break
return results
def _get_compiled_query(self, language: str, pattern: str) -> Query:
"""Get or compile a tree-sitter query."""
cache_key = f"{language}:{hash(pattern)}"
if cache_key not in self._query_cache:
language_obj = get_language(language)
self._query_cache[cache_key] = language_obj.query(pattern)
return self._query_cache[cache_key]
def _find_matching_files(self, dir_path: Path, params: SearchParameters) -> List[Path]:
"""Find all files in directory that match the search criteria."""
matching_files = []
try:
# Use rglob to recursively find files
if params.follow_symlinks:
all_files = [f for f in dir_path.rglob("*") if f.is_file()]
else:
all_files = [f for f in dir_path.rglob("*") if f.is_file() and not f.is_symlink()]
for file_path in all_files:
# Check if file matches include patterns
if not self._matches_patterns(file_path.name, params.file_patterns):
continue
# Check if file matches exclude patterns
if self._matches_patterns(str(file_path.relative_to(dir_path)), params.exclude_patterns):
continue
# Skip binary files
if self._is_binary_file(file_path):
continue
matching_files.append(file_path)
except PermissionError as e:
print(f"Permission denied accessing {dir_path}: {e}")
except Exception as e:
print(f"Error finding files in {dir_path}: {e}")
return matching_files
def _matches_patterns(self, file_path: str, patterns: List[str]) -> bool:
"""Check if file path matches any of the given patterns."""
for pattern in patterns:
if fnmatch.fnmatch(file_path, pattern):
return True
return False
def _is_binary_file(self, file_path: Path) -> bool:
"""Check if file is binary by reading a small sample."""
try:
with open(file_path, 'rb') as f:
chunk = f.read(1024)
return b'\0' in chunk
except Exception:
return True # If we can't read it, treat as binary
def _deduplicate_results(self, results: List[SearchResult]) -> List[SearchResult]:
"""Remove duplicate results and sort by relevance."""
seen: Set[Tuple[str, int, str]] = set()
unique_results = []
for result in results:
# Create a unique key based on file, line, and match text
key = (result.file_path, result.start_line, result.match_text.strip())
if key not in seen:
seen.add(key)
unique_results.append(result)
# Sort by file path, then by line number
unique_results.sort(key=lambda r: (r.file_path, r.start_line))
return unique_results