refactor: rename and enhance get_ai_comments to track line numbers and bang status

This commit is contained in:
Paul Gauthier (aider) 2024-12-01 07:48:35 -08:00
parent a131d5ad35
commit 21dffa26b9

View file

@ -173,17 +173,9 @@ class FileWatcher:
# Refresh all AI comments from tracked files
ai_comments = {}
for fname in self.coder.abs_fnames:
#ai update this too!
comment_lines, has_bang = self.get_ai_comment(fname):
ai_comments[fname] = comment_lines
# ai this logic should move into get_ai_comments()
has_bangs = any(
comment.strip().endswith("!")
for comments in ai_comments.values()
if comments
for comment in comments
)
comments, line_nums, has_bang = self.get_ai_comments(fname)
ai_comments[fname] = comments
has_bangs = has_bang
if not has_bangs:
return ""
@ -200,22 +192,26 @@ class FileWatcher:
dump(res)
return res
#ai change this to get_ai_comments() with an s
#ai return a list of the line numbers which have matching comments
# also return a bool indicating if any of the lines end with !
def get_ai_comment(self, filepath):
"""Extract all AI comments from a file"""
def get_ai_comments(self, filepath):
"""Extract all AI comments from a file, returning comments, line numbers and bang status"""
comments = []
line_nums = []
has_bang = False
try:
content = self.io.read_text(filepath)
for line in content.splitlines():
for i, line in enumerate(content.splitlines(), 1):
if match := self.ai_comment_pattern.search(line):
comment = match.group(0).strip()
if comment:
comments.append(comment)
line_nums.append(i)
if comment.strip().endswith('!'):
has_bang = True
except Exception:
return None
return comments if comments else None
return None, None, False
if not comments:
return None, None, False
return comments, line_nums, has_bang
def main():