buddy / utils /code_tools.py
hotboxxgenn's picture
Rename code_tools.py to utils/code_tools.py
5983d6e verified
raw
history blame contribute delete
948 Bytes
import os,re
def detect_language(filename,content=''):
ext=os.path.splitext(filename)[1].lower()
if ext in ('.py',): return 'python'
if ext in ('.js',): return 'javascript'
if ext in ('.html',): return 'html'
if 'def ' in content: return 'python'
return 'text'
def analyze_code(content):
lines=content.splitlines(); total=len(lines); empty=sum(1 for l in lines if not l.strip())
funcs=sum(1 for l in lines if re.search(r'\bdef\s+\w+|\bfunction\s+\w+',l))
classes=sum(1 for l in lines if re.search(r'\bclass\s+\w+',l))
return {'lines':total,'functions':funcs,'classes':classes,'empty':empty,'summary':f'{total} lines, {funcs} functions, {classes} classes'}
def suggest_fixes(lang,content):
fixes=[]
if lang=='python':
if 'print ' in content and '()' not in content: fixes.append('Use print(...) for Python 3')
if 'TODO' in content: fixes.append('Address TODO comments')
return fixes