feat: collect all AI comments from files instead of just first one

This commit is contained in:
Paul Gauthier (aider) 2024-10-25 16:03:39 -07:00
parent 05daab24a2
commit aa86f02b1a

View file

@ -131,21 +131,23 @@ def watch_source_files(
changed_files = {str(Path(change[1])) for change in changes} changed_files = {str(Path(change[1])) for change in changes}
result = {} result = {}
for file in changed_files: for file in changed_files:
if comment := get_ai_comment(file, encoding=encoding): if comments := get_ai_comment(file, encoding=encoding):
result[file] = comment result[file] = comments
yield result yield result
#ai return a list of all the ai comments in each file
def get_ai_comment(filepath, encoding="utf-8"): def get_ai_comment(filepath, encoding="utf-8"):
"""Extract AI comment from a file if present""" """Extract all AI comments from a file"""
comments = []
try: try:
with open(filepath, encoding=encoding, errors="ignore") as f: with open(filepath, encoding=encoding, errors="ignore") as f:
for line in f: for line in f:
if match := re.search(r"(?:#|//) *ai\b(.*)", line, re.IGNORECASE): if match := re.search(r"(?:#|//) *ai\b(.*)", line, re.IGNORECASE):
return match.group(1).strip() comment = match.group(1).strip()
if comment:
comments.append(comment)
except (IOError, UnicodeDecodeError): except (IOError, UnicodeDecodeError):
return None return None
return None return comments if comments else None
def main(): def main():