From 21dffa26b9bc7213e94054b89c5d1a5b217f2d10 Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Sun, 1 Dec 2024 07:48:35 -0800 Subject: [PATCH] refactor: rename and enhance get_ai_comments to track line numbers and bang status --- aider/watch.py | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/aider/watch.py b/aider/watch.py index e19c4ce40..c05ba345d 100644 --- a/aider/watch.py +++ b/aider/watch.py @@ -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():