forked from RenjiYuusei/CursorFocus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
202 lines (174 loc) · 6.69 KB
/
config.py
File metadata and controls
202 lines (174 loc) · 6.69 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
import os
import json
import sys
import re
def load_config():
"""Load configuration from config.json."""
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, 'config.json')
# Get the latest version information
default_config = get_default_config()
latest_version = default_config.get("version")
# Load existing config
config = None
if os.path.exists(config_path):
with open(config_path, 'r') as f:
if config_path.endswith('.json'):
config = json.load(f)
if not config:
config = default_config
else:
# Update version in config to match the latest version
config["version"] = latest_version
# Save the updated config with the correct version
try:
with open(config_path, 'w') as f:
json.dump(config, f, indent=4)
except Exception:
pass
return config
except Exception as e:
print(f"Error loading config: {e}")
return None
def get_default_config():
"""Get default configuration settings."""
# Try to get version from environment or .version file
version = "1.0.0" # Default fallback version
script_dir = os.path.dirname(os.path.abspath(__file__))
# Check for .version file
version_file = os.path.join(script_dir, '.version')
if os.path.exists(version_file):
try:
with open(version_file, 'r') as f:
version = f.read().strip()
except Exception:
pass
# Check executable name for version info
try:
executable_path = os.path.basename(sys.executable)
if 'CursorFocus_' in executable_path:
version_match = re.search(r'CursorFocus_(\d+\.\d+\.\d+)', executable_path)
if version_match:
version = version_match.group(1)
except Exception:
pass
return {
"version": version,
"project_path": "",
"update_interval": 60,
"max_depth": 3,
"ignored_directories": [
"__pycache__",
"node_modules",
"venv",
".git",
".idea",
".vscode",
"dist",
"build",
"coverage"
],
"ignored_files": [
".DS_Store",
"Thumbs.db",
"*.pyc",
"*.pyo",
"package-lock.json",
"yarn.lock"
],
"binary_extensions": [
".png", ".jpg", ".jpeg", ".gif", ".ico", ".pdf", ".exe", ".bin"
],
"file_length_standards": {
".py": 400, # Python
".js": 300, # JavaScript
".ts": 300, # TypeScript
".tsx": 300, # TypeScript/React
".kt": 300, # Kotlin
".php": 400, # PHP
".swift": 400, # Swift
".cpp": 500, # C++
".c": 500, # C
".h": 300, # C/C++ Header
".hpp": 300, # C++ Header
".cs": 400, # C#
".csx": 400, # C# Script
"default": 300
}
}
def save_config(config):
"""Save configuration to config.json file.
Args:
config (dict): The configuration dictionary to save
"""
try:
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, 'config.json')
with open(config_path, 'w') as f:
json.dump(config, f, indent=4)
return True
except Exception as e:
print(f"Error saving config: {e}")
return False
# Load configuration once at module level
_config = load_config()
# Binary file extensions that should be ignored
BINARY_EXTENSIONS = set(_config.get('binary_extensions', []))
# Documentation and text files that shouldn't be analyzed for functions
NON_CODE_EXTENSIONS = {
'.md', '.txt', '.log', '.json', '.yml', '.toml', '.ini', '.cfg',
'.conf', '.config', '.markdown', '.rst', '.rdoc', '.csv', '.tsv'
}
# Extensions that should be analyzed for code
CODE_EXTENSIONS = {
'.py', # Python
'.js', # JavaScript
'.ts', # TypeScript
'.tsx', # TypeScript/React
'.kt', # Kotlin
'.php', # PHP
'.swift', # Swift
'.cpp', # C++
'.c', # C
'.h', # C/C++ Header
'.hpp', # C++ Header
'.cs', # C#
'.csx', # C# Script
}
# Regex patterns for function detection
FUNCTION_PATTERNS = {
# Python
'python_function': r'def\s+([a-zA-Z_]\w*)\s*\(',
'python_class': r'class\s+([a-zA-Z_]\w*)\s*[:\(]',
# JavaScript/TypeScript
'js_function': r'(?:^|\s+)(?:function\s+([a-zA-Z_]\w*)|(?:const|let|var)\s+([a-zA-Z_]\w*)\s*=\s*(?:async\s*)?function)',
'js_arrow': r'(?:^|\s+)(?:const|let|var)\s+([a-zA-Z_]\w*)\s*=\s*(?:async\s*)?(?:\([^)]*\)|[^=])\s*=>',
'js_method': r'\b([a-zA-Z_]\w*)\s*:\s*(?:async\s*)?function',
'js_class_method': r'(?:^|\s+)(?:async\s+)?([a-zA-Z_]\w*)\s*\([^)]*\)\s*{',
# PHP
'php_function': r'(?:public\s+|private\s+|protected\s+)?function\s+([a-zA-Z_]\w*)\s*\(',
# C/C++
'cpp_function': r'(?:virtual\s+)?(?:static\s+)?(?:inline\s+)?(?:const\s+)?(?:\w+(?:::\w+)*\s+)?([a-zA-Z_]\w*)\s*\([^)]*\)(?:\s*const)?(?:\s*noexcept)?(?:\s*override)?(?:\s*final)?(?:\s*=\s*0)?(?:\s*=\s*default)?(?:\s*=\s*delete)?(?:{|;)',
# C#
'csharp_method': r'(?:public|private|protected|internal|static|virtual|override|abstract|sealed|async)\s+(?:\w+(?:<[^>]+>)?)\s+([a-zA-Z_]\w*)\s*\([^)]*\)',
# Kotlin
'kotlin_function': r'(?:fun\s+)?([a-zA-Z_]\w*)\s*(?:<[^>]+>)?\s*\([^)]*\)(?:\s*:\s*[^{]+)?\s*{',
# Swift
'swift_function': r'(?:func\s+)([a-zA-Z_]\w*)\s*(?:<[^>]+>)?\s*\([^)]*\)(?:\s*->\s*[^{]+)?\s*{'
}
# Keywords that should not be treated as function names
IGNORED_KEYWORDS = {
'if', 'switch', 'while', 'for', 'catch', 'finally', 'else', 'return',
'break', 'continue', 'case', 'default', 'to', 'from', 'import', 'as',
'try', 'except', 'raise', 'with', 'async', 'await', 'yield', 'assert',
'pass', 'del', 'print', 'in', 'is', 'not', 'and', 'or', 'lambda',
'global', 'nonlocal', 'class', 'def'
}
# Names of files and directories that should be ignored
IGNORED_NAMES = set(_config.get('ignored_directories', []))
FILE_LENGTH_STANDARDS = _config.get('file_length_standards', {})
def get_file_length_limit(file_path):
"""Get the recommended line limit for a given file type."""
ext = os.path.splitext(file_path)[1].lower()
return FILE_LENGTH_STANDARDS.get(ext, FILE_LENGTH_STANDARDS.get('default', 300))