feat: add AI comment extraction from changed files

This commit is contained in:
Paul Gauthier (aider) 2024-10-25 16:00:55 -07:00
parent 7a9091fcae
commit 150cc56b35

View file

@ -129,9 +129,23 @@ def watch_source_files(
for changes in watch(root, watch_filter=filter_func, stop_event=stop_event):
# Convert the changes to a set of unique file paths
changed_files = {str(Path(change[1])) for change in changes}
yield changed_files
result = {}
for file in changed_files:
if comment := get_ai_comment(file, encoding=encoding):
result[file] = comment
yield result
def get_ai_comment(filepath, encoding="utf-8"):
"""Extract AI comment from a file if present"""
try:
with open(filepath, encoding=encoding, errors="ignore") as f:
for line in f:
if match := re.search(r"(?:#|//) *ai:? *(.*)", line, re.IGNORECASE):
return match.group(1).strip()
except (IOError, UnicodeDecodeError):
return None
return None
# ai: for each changed_file, return the filename and the contents of the "ai" comment (all the way to end of line)
def main():
"""Example usage of the file watcher"""
import argparse