From 4c220ff63fd411c230d64418054e84ee7bc05353 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 20:31:22 -0700 Subject: [PATCH] refactor to use io.read_text --- aider/coders/editblock_coder.py | 55 +++++++++++++++------------- aider/coders/editblock_func_coder.py | 3 +- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index b63f577a2..eb7fc0c0e 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -26,7 +26,8 @@ class EditBlockCoder(Coder): full_path = self.allowed_to_edit(path) if not full_path: continue - if do_replace(full_path, original, updated, self.dry_run): + content = self.io.read_text(full_path) + if do_replace(full_path, content, original, updated, self.dry_run): edited.add(path) continue self.io.tool_error(f"Failed to apply edit to {path}") @@ -34,6 +35,33 @@ class EditBlockCoder(Coder): return edited +def do_replace(fname, content, before_text, after_text, dry_run=False): + before_text = strip_quoted_wrapping(before_text, fname) + after_text = strip_quoted_wrapping(after_text, fname) + fname = Path(fname) + + # does it want to make a new file? + if not fname.exists() and not before_text.strip(): + fname.touch() + content = "" + + if content is None: + return + + if not before_text.strip(): + # append to existing file, or start a new file + 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 + + def try_dotdotdots(whole, part, replace): """ See if the edit block has ... lines. @@ -211,31 +239,6 @@ def strip_quoted_wrapping(res, fname=None): return res -def do_replace(fname, before_text, after_text, dry_run=False): - before_text = strip_quoted_wrapping(before_text, fname) - after_text = strip_quoted_wrapping(after_text, fname) - fname = Path(fname) - - # does it want to make a new file? - if not fname.exists() and not before_text.strip(): - fname.touch() - - content = fname.read_text() - - if not before_text.strip(): - # append to existing file, or start a new file - 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 - - ORIGINAL = "<<<<<<< ORIGINAL" DIVIDER = "=======" UPDATED = ">>>>>>> UPDATED" diff --git a/aider/coders/editblock_func_coder.py b/aider/coders/editblock_func_coder.py index 676952938..dfe4175a6 100644 --- a/aider/coders/editblock_func_coder.py +++ b/aider/coders/editblock_func_coder.py @@ -135,7 +135,8 @@ class EditBlockFunctionCoder(Coder): full_path = self.allowed_to_edit(path) if not full_path: continue - if do_replace(full_path, original, updated, self.dry_run): + content = self.io.read_text(full_path) + if do_replace(full_path, content, original, updated, self.dry_run): edited.add(path) continue self.io.tool_error(f"Failed to apply edit to {path}")