forked from RenjiYuusei/CursorFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_generator.py
More file actions
713 lines (599 loc) · 30.4 KB
/
rules_generator.py
File metadata and controls
713 lines (599 loc) · 30.4 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
import os
import json
from typing import Dict, Any, List
from datetime import datetime
import google.generativeai as genai
import re
from rules_analyzer import RulesAnalyzer
from dotenv import load_dotenv
from patterns_analyzer import PatternsAnalyzer
class RulesGenerator:
def __init__(self, project_path: str):
self.project_path = project_path
self.analyzer = RulesAnalyzer(project_path)
# Initialize pattern analyzer
patterns_analyzer = PatternsAnalyzer()
self.compiled_patterns = patterns_analyzer.compiled_patterns
self.get_language_from_ext = patterns_analyzer.get_language_from_ext
# Load environment variables from .env
load_dotenv()
# Initialize Gemini AI
try:
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
raise ValueError("GEMINI_API_KEY is required")
genai.configure(api_key=api_key)
# Get model name from environment or use default
model_name = os.environ.get("GEMINI_MODEL", "gemini-2.5-pro-exp-03-25")
self.model = genai.GenerativeModel(
model_name=model_name,
)
self.chat_session = self.model.start_chat(history=[])
except Exception as e:
print(f"\n⚠️ Error when initializing Gemini AI: {e}")
raise
def _get_timestamp(self) -> str:
"""Get current timestamp in standard format."""
return datetime.now().strftime('%B %d, %Y at %I:%M %p')
def _analyze_project_structure(self) -> Dict[str, Any]:
"""Analyze project structure and collect detailed information."""
structure = {
'files': [],
'dependencies': {},
'frameworks': [],
'languages': {},
'config_files': [],
'code_contents': {},
'directory_structure': {}, # Track directory hierarchy
'language_stats': {}, # Track language statistics by directory
'patterns': {
'classes': [],
'functions': [],
'imports': [],
'error_handling': [],
'configurations': [],
'naming_patterns': {},
'code_organization': [],
'variable_patterns': [],
'function_patterns': [],
'class_patterns': [],
'error_patterns': [],
'performance_patterns': [],
'suggest_patterns': [],
'directory_patterns': [] # Track directory organization patterns
}
}
# Track directory statistics
dir_stats = {}
# Analyze each file
for root, dirs, files in os.walk(self.project_path):
# Skip ignored directories
dirs[:] = [d for d in dirs if not any(x in d for x in ['node_modules', 'venv', '.git', '__pycache__', 'build', 'dist'])]
rel_root = os.path.relpath(root, self.project_path)
if rel_root == '.':
rel_root = ''
# Initialize directory statistics
dir_stats[rel_root] = {
'total_files': 0,
'code_files': 0,
'languages': {},
'frameworks': set(),
'patterns': {
'classes': 0,
'functions': 0,
'imports': 0
}
}
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, self.project_path)
# Update directory statistics
dir_stats[rel_root]['total_files'] += 1
# Analyze code files
file_ext = os.path.splitext(file)[1].lower()
if file_ext in ['.py', '.js', '.ts', '.tsx', '.kt', '.php', '.swift', '.cpp', '.c', '.h', '.hpp', '.cs', '.csx', '.java', '.rb', '.objc']:
structure['files'].append(rel_path)
dir_stats[rel_root]['code_files'] += 1
# Update language statistics
lang = self.get_language_from_ext(file_ext)
dir_stats[rel_root]['languages'][lang] = dir_stats[rel_root]['languages'].get(lang, 0) + 1
structure['languages'][lang] = structure['languages'].get(lang, 0) + 1
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
structure['code_contents'][rel_path] = content
# Analyze based on file type
self._analyze_file(content, rel_path, structure, lang)
except Exception as e:
print(f"⚠️ Error reading file {rel_path}: {e}")
continue
# Classify config files
elif file.endswith(('.json', '.ini', '.conf')):
structure['config_files'].append(rel_path)
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
structure['patterns']['configurations'].append({
'file': rel_path,
'content': content
})
except Exception as e:
print(f"⚠️ Error reading config file {rel_path}: {e}")
continue
# Add directory structure information
if rel_root:
structure['directory_structure'][rel_root] = {
'stats': dir_stats[rel_root],
'parent': os.path.dirname(rel_root) or None
}
# Analyze directory patterns
self._analyze_directory_patterns(structure, dir_stats)
return structure
def _analyze_file(self, content: str, rel_path: str, structure: Dict[str, Any], language: str) -> None:
"""Generic file analyzer that handles all languages."""
# Map language to pattern group
pattern_groups = {
'python': 'python',
'javascript': 'web',
'typescript': 'web',
'csharp': 'system',
'cpp': 'system',
'c': 'system',
'php': 'system',
'kotlin': 'system',
'swift': 'system',
'java': 'web',
'ruby': 'web',
'objc': 'system',
}
pattern_group = pattern_groups.get(language, 'system')
# Find patterns using named groups
for pattern_type in ['import', 'class', 'function']:
pattern = self.compiled_patterns[pattern_type][pattern_group]
matches = pattern.finditer(content)
for match in matches:
try:
info = {}
# Get all named groups
groups = match.groupdict()
# Handle imports
if pattern_type == 'import':
module = next((v for k, v in groups.items() if v and k.startswith('module')), None)
if module:
structure['dependencies'][module] = True
structure['patterns']['imports'].append(module)
continue
# Handle classes and functions
name = next((v for k, v in groups.items() if v and (k == 'name' or k == 'n')), None)
if not name:
continue
info['name'] = name
info['file'] = rel_path
info['type'] = pattern_type
# Add parameters/base class if present
if 'params' in groups and groups['params']:
info['parameters'] = groups['params']
if 'base' in groups and groups['base']:
info['base'] = groups['base'].strip()
if 'return' in groups and groups['return']:
info['return_type'] = groups['return'].strip()
# Add to appropriate pattern list
pattern_key = f'{pattern_type}_patterns'
structure['patterns'][pattern_key].append(info)
except Exception as e:
continue # Skip on any error
# Handle web-specific patterns
if language in ['typescript', 'javascript']:
self._analyze_web_patterns(content, rel_path, structure)
# Handle Unity-specific patterns for C#
if language == 'csharp' and any(x in content for x in ['UnityEngine', 'MonoBehaviour', 'ScriptableObject']):
self._analyze_unity_patterns(content, rel_path, structure)
def _analyze_directory_patterns(self, structure: Dict[str, Any], dir_stats: Dict[str, Any]):
"""Analyze directory organization patterns."""
for dir_path, stats in dir_stats.items():
if not dir_path: # Skip root directory
continue
# Analyze directory naming convention
dir_name = os.path.basename(dir_path)
if dir_name.islower():
pattern = 'lowercase'
elif dir_name.isupper():
pattern = 'uppercase'
elif '_' in dir_name:
pattern = 'snake_case'
elif '-' in dir_name:
pattern = 'kebab-case'
else:
pattern = 'mixed'
# Analyze directory purpose
purpose = []
if any(x in dir_name.lower() for x in ['test', 'spec', 'mock']):
purpose.append('testing')
if any(x in dir_name.lower() for x in ['util', 'helper', 'common', 'shared']):
purpose.append('utilities')
if any(x in dir_name.lower() for x in ['model', 'entity', 'domain']):
purpose.append('domain')
if any(x in dir_name.lower() for x in ['controller', 'handler', 'service']):
purpose.append('business_logic')
if any(x in dir_name.lower() for x in ['view', 'template', 'component']):
purpose.append('presentation')
# Add directory pattern
structure['patterns']['directory_patterns'].append({
'path': dir_path,
'name_pattern': pattern,
'purpose': purpose,
'languages': stats['languages'],
'total_files': stats['total_files'],
'code_files': stats['code_files'],
'code_metrics': stats['patterns']
})
def _generate_ai_rules(self, project_info: Dict[str, Any]) -> Dict[str, Any]:
"""Generate rules using Gemini AI based on project analysis."""
try:
# Analyze project
project_structure = self._analyze_project_structure()
# Create detailed prompt
prompt = f"""As an AI assistant working in Cursor IDE, analyze this project to understand how you should behave and generate code that perfectly matches the project's patterns and standards.
Project Overview:
Language: {project_info.get('language', 'unknown')}
Framework: {project_info.get('framework', 'none')}
Type: {project_info.get('type', 'generic')}
Description: {project_info.get('description', 'Generic Project')}
Primary Purpose: Code generation and project analysis
Project Metrics:
- Files & Structure:
- Total Files: {len(project_structure['files'])}
- Config Files: {len(project_structure['config_files'])}
- Dependencies:
- Frameworks: {', '.join(project_structure['frameworks']) or 'none'}
- Core Dependencies: {', '.join(list(project_structure['dependencies'].keys())[:10])}
- Total Dependencies: {len(project_structure['dependencies'])}
Project Ecosystem:
1. Development Environment:
- Project Structure:
{chr(10).join([f"- {f}" for f in project_structure['files'] if f.endswith(('.json', '.md', '.env', '.gitignore'))][:5])}
- IDE Configuration:
{chr(10).join([f"- {f}" for f in project_structure['files'] if '.vscode' in f or '.idea' in f][:5])}
- Build System:
{chr(10).join([f"- {f}" for f in project_structure['files'] if f in ['setup.py', 'requirements.txt', 'package.json', 'Makefile', 'composer.json', 'Gemfile', 'CMakeLists.txt', 'build.gradle', 'pom.xml', 'webpack.config.js']])}
2. Project Components:
- Core Modules:
{chr(10).join([f"- {f}: {sum(1 for p in project_structure['patterns']['function_patterns'] if p['file'] == f)} functions" for f in project_structure['files'] if f.endswith('.py, .js, .ts, .tsx, .kt, .php, .swift, .cpp, .c, .h, .hpp, .cs, .csx') and not any(x in f.lower() for x in ['setup', 'config'])][:5])}
- Support Modules:
{chr(10).join([f"- {f}" for f in project_structure['files'] if any(x in f.lower() for x in ['util', 'helper', 'common', 'shared'])][:5])}
- Templates:
{chr(10).join([f"- {f}" for f in project_structure['files'] if 'template' in f.lower()][:5])}
3. Module Organization Analysis:
- Core Module Functions:
{chr(10).join([f"- {f}: Primary module handling {f.split('_')[0].title()} functionality" for f in project_structure['files'] if f.endswith('.py, .js, .ts, .tsx, .kt, .php, .swift, .cpp, .c, .h, .hpp, .cs, .csx') and not any(x in f.lower() for x in ['setup', 'config'])][:5])}
- Module Dependencies:
{chr(10).join([f"- {f} depends on: {', '.join(list(set([imp.split('.')[0] for imp in project_structure['patterns']['imports'] if imp in f])))}" for f in project_structure['files'] if f.endswith('.py, .js, .ts, .tsx, .kt, .php, .swift, .cpp, .c, .h, .hpp, .cs, .csx')][:5])}
- Module Responsibilities:
Please analyze each module's code and describe its core responsibilities based on:
1. Function and class names
2. Import statements
3. Code patterns and structures
4. Documentation strings
5. Variable names and usage
6. Error handling patterns
7. Performance optimization techniques
- Module Organization Rules:
Based on the codebase analysis, identify and describe:
1. Module organization patterns
2. Dependency management approaches
3. Code structure conventions
4. Naming conventions
5. Documentation practices
6. Error handling strategies
7. Performance optimization patterns
Code Sample Analysis:
{chr(10).join(f"File: {file}:{chr(10)}{content[:10000]}..." for file, content in list(project_structure['code_contents'].items())[:50])}
Based on this detailed analysis, create behavior rules for AI to:
1. Replicate the project's exact code style and patterns
2. Match naming conventions precisely
3. Follow identical error handling patterns
4. Copy performance optimization techniques
5. Maintain documentation consistency
6. Keep current code organization
7. Preserve module boundaries
8. Use established logging methods
9. Follow configuration patterns
Return a JSON object defining AI behavior rules:
{{"ai_behavior": {{
"code_generation": {{
"style": {{
"prefer": [],
"avoid": []
}},
"error_handling": {{
"prefer": [],
"avoid": []
}},
"performance": {{
"prefer": [],
"avoid": []
}},
"suggest_patterns": {{
"improve": [],
"avoid": []
}},
"module_organization": {{
"structure": [], # Analyze and describe the current module structure
"dependencies": [], # Analyze actual dependencies between modules
"responsibilities": {{}}, # Analyze and describe each module's core responsibilities
"rules": [], # Extract rules from actual code organization patterns
"naming": {{}} # Extract naming conventions from actual code
}}
}}
}}}}
Critical Guidelines for AI:
1. NEVER deviate from existing code patterns
2. ALWAYS match the project's exact style
3. MAINTAIN the current complexity level
4. COPY the existing skill level approach
5. PRESERVE all established practices
6. REPLICATE the project's exact style
7. UNDERSTAND pattern purposes"""
# Get AI response
response = self.chat_session.send_message(prompt)
# Extract JSON
json_match = re.search(r'({[\s\S]*})', response.text)
if not json_match:
print("⚠️ No JSON found in AI response")
raise ValueError("Invalid AI response format")
json_str = json_match.group(1)
try:
ai_rules = json.loads(json_str)
if not isinstance(ai_rules, dict) or 'ai_behavior' not in ai_rules:
print("⚠️ Invalid JSON structure in AI response")
raise ValueError("Invalid AI rules structure")
return ai_rules
except json.JSONDecodeError as e:
print(f"⚠️ Error parsing AI response JSON: {e}")
raise
except Exception as e:
print(f"⚠️ Error generating AI rules: {e}")
raise
def _generate_project_description(self, project_structure: Dict[str, Any]) -> str:
"""Generate project description using AI based on project analysis."""
try:
# Analyze core modules
core_modules = []
for file in project_structure.get('files', []):
if file.endswith('.py') and not any(x in file.lower() for x in ['setup', 'config', 'test']):
module_info = {
'name': file,
'classes': [c for c in project_structure['patterns']['class_patterns'] if c['file'] == file],
'functions': [f for f in project_structure['patterns']['function_patterns'] if f['file'] == file],
'imports': [imp for imp in project_structure['patterns']['imports'] if imp in file]
}
core_modules.append(module_info)
# Analyze main patterns
main_patterns = {
'error_handling': project_structure.get('patterns', {}).get('error_patterns', []),
'performance': project_structure.get('patterns', {}).get('performance_patterns', []),
'code_organization': project_structure.get('patterns', {}).get('code_organization', [])
}
# Create detailed prompt for AI
prompt = f"""Analyze this project structure and create a detailed description (2-3 sentences) that captures its essence:
Project Overview:
1. Core Modules Analysis:
{chr(10).join([f"- {m['name']}: {len(m['classes'])} classes, {len(m['functions'])} functions" for m in core_modules])}
2. Module Responsibilities:
{chr(10).join([f"- {m['name']}: Main purpose indicated by {', '.join([c['name'] for c in m['classes'][:2]])}" for m in core_modules if m['classes']])}
3. Technical Implementation:
- Error Handling: {len(main_patterns['error_handling'])} patterns found
- Performance Optimizations: {len(main_patterns['performance'])} patterns found
- Code Organization: {len(main_patterns['code_organization'])} patterns found
4. Project Architecture:
- Total Files: {len(project_structure.get('files', []))}
- Core Python Modules: {len(core_modules)}
- External Dependencies: {len(project_structure.get('dependencies', {}))}
Based on this analysis, create a description that covers:
1. The project's main purpose and functionality
2. Key technical features and implementation approach
3. Target users and primary use cases
4. Unique characteristics or innovations
Format: Return a clear, concise description focusing on what makes this project unique.
Do not include technical metrics in the description."""
# Get AI response
response = self.chat_session.send_message(prompt)
description = response.text.strip()
# Validate description length and content
if len(description.split()) > 100: # Length limit
description = ' '.join(description.split()[:100]) + '...'
return description
except Exception as e:
print(f"⚠️ Error generating project description: {e}")
return "A software project with automated analysis and rule generation capabilities."
def _generate_markdown_rules(self, project_info: Dict[str, Any], ai_rules: Dict[str, Any]) -> str:
"""Generate rules in markdown format."""
timestamp = self._get_timestamp()
description = project_info.get('description', 'A software project with automated analysis and rule generation capabilities.')
markdown = f"""# Project Rules
## Project Information
- **Version**: {project_info.get('version', '1.0')}
- **Last Updated**: {timestamp}
- **Name**: {project_info.get('name', 'Unknown')}
- **Language**: {project_info.get('language', 'unknown')}
- **Framework**: {project_info.get('framework', 'none')}
- **Type**: {project_info.get('type', 'application')}
## Project Description
{description}
## AI Behavior Rules
### Code Generation Style
#### Preferred Patterns
"""
# Add preferred code generation patterns
for pattern in ai_rules['ai_behavior']['code_generation']['style']['prefer']:
markdown += f"- {pattern}\n"
markdown += "\n#### Patterns to Avoid\n"
for pattern in ai_rules['ai_behavior']['code_generation']['style']['avoid']:
markdown += f"- {pattern}\n"
markdown += "\n### Error Handling\n#### Preferred Patterns\n"
for pattern in ai_rules['ai_behavior']['code_generation']['error_handling']['prefer']:
markdown += f"- {pattern}\n"
markdown += "\n#### Patterns to Avoid\n"
for pattern in ai_rules['ai_behavior']['code_generation']['error_handling']['avoid']:
markdown += f"- {pattern}\n"
markdown += "\n### Performance\n#### Preferred Patterns\n"
for pattern in ai_rules['ai_behavior']['code_generation']['performance']['prefer']:
markdown += f"- {pattern}\n"
markdown += "\n#### Patterns to Avoid\n"
for pattern in ai_rules['ai_behavior']['code_generation']['performance']['avoid']:
markdown += f"- {pattern}\n"
markdown += "\n### Module Organization\n#### Structure\n"
for item in ai_rules['ai_behavior']['code_generation']['module_organization']['structure']:
markdown += f"- {item}\n"
markdown += "\n#### Dependencies\n"
for dep in ai_rules['ai_behavior']['code_generation']['module_organization']['dependencies']:
markdown += f"- {dep}\n"
markdown += "\n#### Module Responsibilities\n"
for module, resp in ai_rules['ai_behavior']['code_generation']['module_organization']['responsibilities'].items():
markdown += f"- **{module}**: {resp}\n"
markdown += "\n#### Rules\n"
for rule in ai_rules['ai_behavior']['code_generation']['module_organization']['rules']:
markdown += f"- {rule}\n"
markdown += "\n#### Naming Conventions\n"
for category, convention in ai_rules['ai_behavior']['code_generation']['module_organization']['naming'].items():
markdown += f"- **{category}**: {convention}\n"
return markdown
def generate_rules_file(self, project_info: Dict[str, Any] = None, format: str = 'json') -> str:
"""Generate the .cursorrules file based on project analysis and AI suggestions."""
try:
# Use analyzer if no project_info provided
if project_info is None:
project_info = self.analyzer.analyze_project_for_rules()
# Analyze project structure
project_structure = self._analyze_project_structure()
# Generate AI rules
ai_rules = self._generate_ai_rules(project_info)
# Generate project description
description = self._generate_project_description(project_structure)
project_info['description'] = description
# Create rules file path
rules_file = os.path.join(self.project_path, '.cursorrules')
if format.lower() == 'markdown':
content = self._generate_markdown_rules(project_info, ai_rules)
with open(rules_file, 'w', encoding='utf-8') as f:
f.write(content)
else: # JSON format
rules = {
"version": "1.0",
"last_updated": self._get_timestamp(),
"project": {
**project_info,
"description": description
},
"ai_behavior": ai_rules['ai_behavior']
}
with open(rules_file, 'w', encoding='utf-8') as f:
json.dump(rules, f, indent=2)
return rules_file
except Exception as e:
print(f"❌ Failed to generate rules: {e}")
raise
def _analyze_web_patterns(self, content: str, rel_path: str, structure: Dict[str, Any]) -> None:
"""Analyze React/Next.js specific patterns."""
# Find interfaces and types
for match in self.compiled_patterns['common']['interface'].finditer(content):
structure['patterns']['class_patterns'].append({
'name': match.group(1),
'type': 'interface/type',
'inheritance': match.group(2).strip() if match.group(2) else '',
'file': rel_path
})
# Find React components
for match in self.compiled_patterns['common']['jsx_component'].finditer(content):
component_name = match.group(1)
if component_name[0].isupper(): # React components start with uppercase
structure['patterns']['class_patterns'].append({
'name': component_name,
'type': 'react_component',
'file': rel_path
})
# Find React hooks
for hook in re.finditer(self.compiled_patterns['common']['react_hook'], content):
structure['patterns']['function_patterns'].append({
'name': hook.group(0),
'type': 'react_hook',
'file': rel_path
})
# Find Next.js specific patterns
if any(x in rel_path for x in ['pages/', 'app/']):
# Check for Next.js data fetching methods
for method in re.finditer(self.compiled_patterns['common']['next_api'], content):
structure['patterns']['function_patterns'].append({
'name': method.group(0),
'type': 'next_data_fetching',
'file': rel_path
})
# Analyze page/route structure
page_match = re.search(self.compiled_patterns['common']['next_page'], rel_path)
if page_match:
structure['patterns']['code_organization'].append({
'type': 'next_page',
'route': page_match.group('route'),
'nested': page_match.group('nested'),
'file': rel_path
})
# Check for layouts
if re.search(self.compiled_patterns['common']['next_layout'], rel_path):
structure['patterns']['code_organization'].append({
'type': 'next_layout',
'file': rel_path
})
# Find styled-components patterns
for match in re.finditer(self.compiled_patterns['common']['styled_component'], content):
structure['patterns']['code_organization'].append({
'type': 'styled_component',
'element': match.group('element') if match.group('element') else 'css',
'file': rel_path
})
def _analyze_unity_patterns(self, content: str, rel_path: str, structure: Dict[str, Any]) -> None:
"""Analyze Unity-specific patterns in C# scripts."""
# Find MonoBehaviour and ScriptableObject components
for match in self.compiled_patterns['unity']['component'].finditer(content):
structure['patterns']['class_patterns'].append({
'name': match.group(0),
'type': 'unity_component',
'file': rel_path
})
# Find Unity lifecycle methods
for match in self.compiled_patterns['unity']['lifecycle'].finditer(content):
structure['patterns']['function_patterns'].append({
'name': match.group(0),
'type': 'unity_lifecycle',
'file': rel_path
})
# Find Unity attributes
for match in self.compiled_patterns['unity']['attribute'].finditer(content):
structure['patterns']['code_organization'].append({
'type': 'unity_attribute',
'name': match.group(0),
'parameters': match.group('params') if match.group('params') else '',
'file': rel_path
})
# Find Unity types
for match in self.compiled_patterns['unity']['type'].finditer(content):
structure['patterns']['class_patterns'].append({
'name': match.group(0),
'type': 'unity_type',
'file': rel_path
})
# Find Unity events
for match in self.compiled_patterns['unity']['event'].finditer(content):
structure['patterns']['code_organization'].append({
'type': 'unity_event',
'event_type': match.group('type'),
'name': match.group('name'),
'file': rel_path
})
# Find Unity serialized fields
for match in self.compiled_patterns['unity']['field'].finditer(content):
structure['patterns']['code_organization'].append({
'type': 'unity_field',
'field_type': match.group(1),
'name': match.group(2),
'file': rel_path
})