From bb1b3fdca1b76c81d8a8d244c1f5ed1079a6551d Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Sun, 1 Dec 2024 07:22:12 -0800 Subject: [PATCH] refactor: replace file operations with io.read_text() --- aider/watch.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/aider/watch.py b/aider/watch.py index 5cd473030..0f042a9b9 100644 --- a/aider/watch.py +++ b/aider/watch.py @@ -102,14 +102,12 @@ class FileWatcher: # Check if file contains AI markers try: - with open(path_abs, encoding=self.encoding, errors="ignore") as f: - content = f.read() - - res = bool(re.search(r"(?:#|//) *ai\b", content, re.IGNORECASE)) - if self.verbose: - dump(res) - return res - except (IOError, UnicodeDecodeError) as err: + content = self.io.read_text(str(path_abs)) + res = bool(re.search(r"(?:#|//) *ai\b", content, re.IGNORECASE)) + if self.verbose: + dump(res) + return res + except Exception as err: if self.verbose: print("error") dump(err) @@ -198,13 +196,13 @@ class FileWatcher: """Extract all AI comments from a file""" comments = [] try: - with open(filepath, encoding=encoding, errors="ignore") as f: - for line in f: - if match := re.search(r"(?:#|//) *(ai\b.*|ai)", line, re.IGNORECASE): - comment = match.group(0).strip() - if comment: - comments.append(comment) - except (IOError, UnicodeDecodeError): + content = self.io.read_text(filepath) + for line in content.splitlines(): + if match := re.search(r"(?:#|//) *(ai\b.*|ai)", line, re.IGNORECASE): + comment = match.group(0).strip() + if comment: + comments.append(comment) + except Exception: return None return comments if comments else None