-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_context_analyzer.py
More file actions
379 lines (304 loc) · 15.2 KB
/
code_context_analyzer.py
File metadata and controls
379 lines (304 loc) · 15.2 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
# code_context_analyzer.py (FIXED)
"""
Code Context Analyzer for Wolfkit Code Review Enhancement
Builds comprehensive project context for multi-file analysis
"""
import os
from pathlib import Path
from typing import List, Dict, Set, Optional, Any
from dataclasses import dataclass, field
from collections import defaultdict
from dependency_mapper import DependencyMapper, FileAnalysis, ImportInfo, ExportInfo
@dataclass
class ProjectContext:
"""
Comprehensive context about a project or module for AI analysis
"""
project_path: str
analysis_scope: str # 'single', 'module', 'project'
target_files: List[str] = field(default_factory=list)
all_files: List[str] = field(default_factory=list)
# File analysis results
file_analyses: Dict[str, FileAnalysis] = field(default_factory=dict)
# Dependency information
dependency_graph: Dict[str, Set[str]] = field(default_factory=dict)
global_symbols: Dict[str, List[str]] = field(default_factory=dict)
missing_imports: Dict[str, List[Dict]] = field(default_factory=dict)
# Project structure
project_structure: Dict[str, Any] = field(default_factory=dict)
external_dependencies: Set[str] = field(default_factory=set)
internal_dependencies: Set[str] = field(default_factory=set)
# Framework detection
detected_framework: Optional[str] = None
framework_files: List[str] = field(default_factory=list)
class CodeContextAnalyzer:
"""
Analyzes project structure and builds comprehensive context for AI analysis
"""
def __init__(self):
self.dependency_mapper = DependencyMapper()
# File extensions to analyze
self.source_extensions = {
'.py', '.js', '.ts', '.jsx', '.tsx', '.html', '.css',
'.json', '.md', '.txt', '.yml', '.yaml'
}
# Directories to skip
self.skip_dirs = {
'node_modules', 'venv', 'env', '.git', '__pycache__',
'.pytest_cache', 'dist', 'build', '.vscode', '.idea',
'coverage', '.coverage', 'htmlcov', '.tox', 'migrations'
}
# Framework detection patterns
self.framework_patterns = {
'fastapi': ['fastapi', 'FastAPI', '@app.get', '@app.post'],
'flask': ['flask', 'Flask', '@app.route', 'request.'],
'django': ['django', 'Django', 'models.Model', 'HttpResponse'],
'react': ['react', 'React', 'useState', 'useEffect', 'jsx'],
'vue': ['vue', 'Vue', 'createApp', 'ref', 'reactive'],
'express': ['express', 'app.get', 'app.post', 'req.', 'res.'],
'nextjs': ['next', 'Next', 'getServerSideProps', 'getStaticProps']
}
def analyze_project_structure(self, project_path: str) -> Dict[str, Any]:
"""
Analyze entire project structure for comprehensive context
Returns dictionary format expected by MultiFileAnalyzer
Args:
project_path: Path to the project root
Returns:
Dictionary with complete project analysis (not ProjectContext object)
"""
# Get the full ProjectContext
context = self._analyze_project_structure_full(project_path)
# Convert to dictionary format expected by MultiFileAnalyzer
return {
'framework': context.detected_framework,
'database': getattr(context, 'database_type', None),
'framework_files': context.framework_files,
'project_structure': context.project_structure,
'file_structure': context.project_structure, # Alias
'dependency_graph': context.dependency_graph,
'global_symbols': context.global_symbols,
'missing_imports': context.missing_imports,
'external_dependencies': list(context.external_dependencies),
'internal_dependencies': list(context.internal_dependencies),
'total_files': len(context.all_files),
'target_files': len(context.target_files)
}
def _analyze_project_structure_full(self, project_path: str) -> ProjectContext:
"""
Internal method that returns full ProjectContext object
"""
project_path = Path(project_path).resolve()
# Scan all source files
all_files = self._scan_source_files(project_path)
# Build context
context = ProjectContext(
project_path=str(project_path),
analysis_scope='project',
target_files=all_files,
all_files=all_files
)
# Analyze all files
context.file_analyses = self._analyze_all_files(all_files)
# Build dependency information
dependency_info = self.dependency_mapper.resolve_cross_file_references(all_files)
context.dependency_graph = dependency_info['dependency_graph']
context.global_symbols = dependency_info['global_symbols']
context.missing_imports = dependency_info['missing_imports']
# Analyze project structure
context.project_structure = self._build_project_structure(project_path, all_files)
# Get dependency summary
import_summary = self.dependency_mapper.get_import_summary(all_files)
context.external_dependencies = set(import_summary['external_dependencies'])
context.internal_dependencies = set(import_summary['internal_dependencies'])
# Detect framework
context.detected_framework = self._detect_framework(all_files, context.file_analyses)
context.framework_files = self._find_framework_files(all_files, context.detected_framework)
return context
def build_context_for_files(self, file_paths: List[str]) -> ProjectContext:
"""
Build context for a specific set of files (module analysis)
Args:
file_paths: List of files to analyze as a module
Returns:
ProjectContext focused on the specified files
"""
if not file_paths:
raise ValueError("No files provided for analysis")
# Determine project root (common parent directory)
project_path = Path(os.path.commonpath(file_paths)).resolve()
if project_path.is_file():
project_path = project_path.parent
# Scan broader context (nearby files)
all_files = self._scan_source_files(project_path)
# Build context
context = ProjectContext(
project_path=str(project_path),
analysis_scope='module',
target_files=file_paths,
all_files=all_files
)
# Analyze all files (for context) but focus on target files
context.file_analyses = self._analyze_all_files(all_files)
# Build dependency information
dependency_info = self.dependency_mapper.resolve_cross_file_references(all_files)
context.dependency_graph = dependency_info['dependency_graph']
context.global_symbols = dependency_info['global_symbols']
context.missing_imports = dependency_info['missing_imports']
# Focus on target files for structure analysis
context.project_structure = self._build_focused_structure(file_paths, all_files)
# Get dependency summary for target files
import_summary = self.dependency_mapper.get_import_summary(file_paths)
context.external_dependencies = set(import_summary['external_dependencies'])
context.internal_dependencies = set(import_summary['internal_dependencies'])
# Detect framework
context.detected_framework = self._detect_framework(file_paths, context.file_analyses)
context.framework_files = self._find_framework_files(file_paths, context.detected_framework)
return context
def analyze_file_relationships(self, file_paths: List[str]) -> Dict[str, Any]:
"""
Analyze relationships between files (expected by MultiFileAnalyzer)
Args:
file_paths: List of file paths to analyze
Returns:
Context dictionary with file relationship information
"""
# This is a wrapper around the existing build_context_for_files method
context = self.build_context_for_files(file_paths)
# Extract the relevant information that MultiFileAnalyzer expects
# Convert ProjectContext object to dictionary format
return {
'framework': context.detected_framework,
'database': getattr(context, 'database_type', None), # Safe access
'framework_files': context.framework_files,
'project_structure': context.project_structure,
'file_structure': context.project_structure, # Alias for compatibility
'file_relationships': context.dependency_graph,
'global_symbols': context.global_symbols,
'missing_imports': context.missing_imports,
'external_dependencies': list(context.external_dependencies),
'internal_dependencies': list(context.internal_dependencies)
}
def _scan_source_files(self, project_path: Path) -> List[str]:
"""Scan directory for source files"""
source_files = []
try:
for root, dirs, files in os.walk(project_path):
# Skip unwanted directories
dirs[:] = [d for d in dirs if d not in self.skip_dirs]
for file in files:
file_path = Path(root) / file
if file_path.suffix.lower() in self.source_extensions:
source_files.append(str(file_path))
# Limit depth to prevent excessive scanning
if len(source_files) > 500: # Reasonable limit
break
except PermissionError:
# Skip directories we can't access
pass
return sorted(source_files)
def _analyze_all_files(self, file_paths: List[str]) -> Dict[str, FileAnalysis]:
"""Analyze all files using dependency mapper"""
analyses = {}
for file_path in file_paths:
try:
analyses[file_path] = self.dependency_mapper.analyze_file(file_path)
except Exception as e:
# Create empty analysis for problematic files
analyses[file_path] = FileAnalysis(
file_path=file_path,
imports=[],
exports=[],
local_definitions=[],
dependencies=set()
)
return analyses
def _build_project_structure(self, project_path: Path, all_files: List[str]) -> Dict[str, Any]:
"""Build project structure information"""
structure = {
'root': str(project_path),
'total_files': len(all_files),
'by_extension': defaultdict(int),
'by_directory': defaultdict(int),
'key_files': [],
'directory_tree': {}
}
# Analyze file distribution
for file_path in all_files:
path = Path(file_path)
# Count by extension
ext = path.suffix.lower()
structure['by_extension'][ext] += 1
# Count by directory
rel_dir = path.relative_to(project_path).parent
structure['by_directory'][str(rel_dir)] += 1
# Identify key files
if path.name in ['main.py', 'app.py', 'index.js', 'index.html', 'package.json', 'requirements.txt']:
structure['key_files'].append(str(path))
# Convert defaultdicts to regular dicts
structure['by_extension'] = dict(structure['by_extension'])
structure['by_directory'] = dict(structure['by_directory'])
return structure
def _build_focused_structure(self, target_files: List[str], all_files: List[str]) -> Dict[str, Any]:
"""Build structure focused on target files"""
structure = {
'target_files': len(target_files),
'total_context_files': len(all_files),
'target_extensions': defaultdict(int),
'related_files': []
}
# Analyze target files
for file_path in target_files:
path = Path(file_path)
ext = path.suffix.lower()
structure['target_extensions'][ext] += 1
# Find related files (same directory or similar names)
target_dirs = {Path(f).parent for f in target_files}
for file_path in all_files:
if file_path not in target_files:
path = Path(file_path)
if path.parent in target_dirs:
structure['related_files'].append(str(path))
structure['target_extensions'] = dict(structure['target_extensions'])
return structure
def _detect_framework(self, file_paths: List[str], analyses: Dict[str, FileAnalysis]) -> Optional[str]:
"""Detect the primary framework used"""
framework_scores = defaultdict(int)
for file_path in file_paths:
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read().lower()
# Check for framework patterns
for framework, patterns in self.framework_patterns.items():
for pattern in patterns:
if pattern.lower() in content:
framework_scores[framework] += content.count(pattern.lower())
# Also check imports
analysis = analyses.get(file_path)
if analysis:
for import_info in analysis.imports:
module_lower = import_info.module.lower()
for framework, patterns in self.framework_patterns.items():
if any(pattern.lower() in module_lower for pattern in patterns):
framework_scores[framework] += 5 # Higher weight for imports
except Exception:
continue
if framework_scores:
return max(framework_scores, key=framework_scores.get)
return None
def _find_framework_files(self, file_paths: List[str], framework: Optional[str]) -> List[str]:
"""Find files that are specific to the detected framework"""
if not framework:
return []
framework_files = []
patterns = self.framework_patterns.get(framework, [])
for file_path in file_paths:
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Check if file contains framework-specific code
if any(pattern in content for pattern in patterns):
framework_files.append(file_path)
except Exception:
continue
return framework_files