From b46dffbb201b119f3a3bdbf8a3fcc3967e1f9ee6 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 21:18:19 -0700 Subject: [PATCH] refactor to use io.write_text --- aider/coders/base_coder.py | 4 ++-- aider/coders/editblock_coder.py | 14 ++++++-------- aider/coders/editblock_func_coder.py | 5 ++++- aider/coders/wholefile_coder.py | 4 ++-- aider/io.py | 4 ++-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index db3de98cf..e29731b16 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -924,7 +924,7 @@ class Coder: if full_path in self.abs_fnames: if not self.dry_run and write_content: - Path(full_path).write_text(write_content) + self.io.write_text(full_path, write_content) return full_path if not Path(full_path).exists(): @@ -950,7 +950,7 @@ class Coder: self.repo.git.add(full_path) if not self.dry_run and write_content: - Path(full_path).write_text(write_content) + self.io.write_text(full_path, write_content) return full_path diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index 148aeffd2..8384671e3 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -27,7 +27,10 @@ class EditBlockCoder(Coder): if not full_path: continue content = self.io.read_text(full_path) - if do_replace(full_path, content, original, updated, self.dry_run): + content = do_replace(full_path, content, original, updated) + if content: + if not self.dry_run: + self.io.write_text(full_path, content) edited.add(path) continue self.io.tool_error(f"Failed to apply edit to {path}") @@ -212,7 +215,7 @@ def strip_quoted_wrapping(res, fname=None): return res -def do_replace(fname, content, before_text, after_text, dry_run=False): +def do_replace(fname, content, before_text, after_text): before_text = strip_quoted_wrapping(before_text, fname) after_text = strip_quoted_wrapping(after_text, fname) fname = Path(fname) @@ -230,13 +233,8 @@ def do_replace(fname, content, before_text, after_text, dry_run=False): new_content = content + after_text else: new_content = replace_most_similar_chunk(content, before_text, after_text) - if not new_content: - return - if not dry_run: - fname.write_text(new_content) - - return True + return new_content ORIGINAL = "<<<<<<< ORIGINAL" diff --git a/aider/coders/editblock_func_coder.py b/aider/coders/editblock_func_coder.py index dfe4175a6..62ac34050 100644 --- a/aider/coders/editblock_func_coder.py +++ b/aider/coders/editblock_func_coder.py @@ -136,7 +136,10 @@ class EditBlockFunctionCoder(Coder): if not full_path: continue content = self.io.read_text(full_path) - if do_replace(full_path, content, original, updated, self.dry_run): + content = do_replace(full_path, content, original, updated) + if content: + if not self.dry_run: + self.io.write_text(full_path, content) edited.add(path) continue self.io.tool_error(f"Failed to apply edit to {path}") diff --git a/aider/coders/wholefile_coder.py b/aider/coders/wholefile_coder.py index 2c77d00fa..7cca9cb5e 100644 --- a/aider/coders/wholefile_coder.py +++ b/aider/coders/wholefile_coder.py @@ -66,7 +66,7 @@ class WholeFileCoder(Coder): edited.add(fname) if not self.dry_run: new_lines = "".join(new_lines) - full_path.write_text(new_lines) + self.io.write_text(full_path, new_lines) fname = None new_lines = [] @@ -125,6 +125,6 @@ class WholeFileCoder(Coder): edited.add(fname) if not self.dry_run: new_lines = "".join(new_lines) - Path(full_path).write_text(new_lines) + self.io.write_text(full_path, new_lines) return edited diff --git a/aider/io.py b/aider/io.py index 0631e82ed..495f80cca 100644 --- a/aider/io.py +++ b/aider/io.py @@ -137,14 +137,14 @@ class InputOutput: def read_text(self, filename): try: - with open(filename, "r", encoding=self.encoding) as f: + with open(str(filename), "r", encoding=self.encoding) as f: return f.read() except (FileNotFoundError, UnicodeError) as e: self.tool_error(f"{filename}: {e}") return def write_text(self, filename, content): - with open(filename, "w", encoding=self.encoding) as f: + with open(str(filename), "w", encoding=self.encoding) as f: f.write(content) def get_input(self, root, rel_fnames, addable_rel_fnames, commands):