refactor: simplify get_ai_comments to return only line numbers and bang status

This commit is contained in:
Paul Gauthier (aider) 2024-12-01 07:49:14 -08:00
parent cd922e919e
commit 96c6d408fb

View file

@ -173,9 +173,13 @@ class FileWatcher:
# Refresh all AI comments from tracked files # Refresh all AI comments from tracked files
ai_comments = {} ai_comments = {}
for fname in self.coder.abs_fnames: for fname in self.coder.abs_fnames:
comments, line_nums, has_bang = self.get_ai_comments(fname) line_nums, has_bang = self.get_ai_comments(fname)
ai_comments[fname] = comments if line_nums:
has_bangs = has_bang content = self.io.read_text(fname)
lines = content.splitlines()
comments = [lines[i-1].strip() for i in line_nums]
ai_comments[fname] = comments
has_bangs = has_bang
if not has_bangs: if not has_bangs:
return "" return ""
@ -192,10 +196,8 @@ class FileWatcher:
dump(res) dump(res)
return res return res
#ai don't return the comments, just line nums and has bang!
def get_ai_comments(self, filepath): def get_ai_comments(self, filepath):
"""Extract all AI comments from a file, returning comments, line numbers and bang status""" """Extract AI comment line numbers and bang status from a file"""
comments = []
line_nums = [] line_nums = []
has_bang = False has_bang = False
try: try:
@ -204,15 +206,14 @@ class FileWatcher:
if match := self.ai_comment_pattern.search(line): if match := self.ai_comment_pattern.search(line):
comment = match.group(0).strip() comment = match.group(0).strip()
if comment: if comment:
comments.append(comment)
line_nums.append(i) line_nums.append(i)
if comment.strip().endswith("!"): if comment.strip().endswith("!"):
has_bang = True has_bang = True
except Exception: except Exception:
return None, None, False return None, False
if not comments: if not line_nums:
return None, None, False return None, False
return comments, line_nums, has_bang return line_nums, has_bang
def main(): def main():