forked from RenjiYuusei/CursorFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzers.py
More file actions
64 lines (54 loc) · 2.17 KB
/
analyzers.py
File metadata and controls
64 lines (54 loc) · 2.17 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
import os
import re
from config import (
BINARY_EXTENSIONS,
IGNORED_NAMES,
NON_CODE_EXTENSIONS,
CODE_EXTENSIONS,
FUNCTION_PATTERNS,
IGNORED_KEYWORDS
)
import logging
def is_binary_file(filename):
"""Check if a file is binary or non-code based on its extension."""
ext = os.path.splitext(filename)[1].lower()
# Binary extensions
if ext in BINARY_EXTENSIONS:
return True
# Documentation and text files that shouldn't be analyzed for functions
return ext in NON_CODE_EXTENSIONS
def should_ignore_file(name):
"""Check if a file or directory should be ignored."""
return name in IGNORED_NAMES or name.startswith('.')
def analyze_file_content(file_path):
"""Analyze file content for functions and their descriptions."""
try:
# Skip binary and non-code files
if is_binary_file(file_path):
return [], 0
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# Skip files that don't look like actual code files
ext = os.path.splitext(file_path)[1].lower()
if ext not in CODE_EXTENSIONS:
return [], 0
functions = []
# Use patterns for function detection
for pattern_name, pattern in FUNCTION_PATTERNS.items():
try:
matches = re.finditer(pattern, content, re.MULTILINE | re.DOTALL)
for match in matches:
func_name = next(filter(None, match.groups()), None)
if not func_name or func_name.lower() in IGNORED_KEYWORDS:
continue
functions.append((func_name, "Function detected"))
except re.error as e:
logging.debug(f"Invalid regex pattern {pattern_name} for {file_path}: {e}")
continue
except Exception as e:
logging.debug(f"Error analyzing pattern {pattern_name} for {file_path}: {e}")
continue
return functions, len(content.split('\n'))
except Exception as e:
print(f"Error analyzing file {file_path}: {e}")
return [], 0