From fb12b739751af1aa2f70350aa57308995d239fb2 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 9 May 2023 07:31:26 -0700 Subject: [PATCH] WIP: Refactor do_replace method to utils module and update references in coder module. --- coder.py | 34 +++------------------------------- utils.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/coder.py b/coder.py index 8123fe2a7..642f0ba5b 100755 --- a/coder.py +++ b/coder.py @@ -377,41 +377,13 @@ class Coder: self.fnames[path] = 0 edited.add(path) - if self.do_replace(path, original, updated): - self.console.print(f"[red]Applied edit to {fname}") + if utils.do_replace(path, original, updated): + self.console.print(f"[red]Applied edit to {path}") else: - self.console.print(f"[red]Failed to apply edit to {fname}") + self.console.print(f"[red]Failed to apply edit to {path}") return edited - def do_replace(self, fname, before_text, after_text): - before_text = utils.strip_quoted_wrapping(before_text, fname) - after_text = utils.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(): - print("Creating empty file:", fname) - fname.touch() - - content = fname.read_text() - - if not before_text.strip(): - if content: - new_content = content + after_text - else: - # first populating an empty file - new_content = after_text - else: - new_content = utils.replace_most_similar_chunk( - content, before_text, after_text - ) - if not new_content: - return - - fname.write_text(new_content) - return True - def commit(self, history=None, prefix=None, ask=False): repo = self.repo if not repo: diff --git a/utils.py b/utils.py index 13a486415..e82e307ab 100644 --- a/utils.py +++ b/utils.py @@ -70,3 +70,30 @@ def strip_quoted_wrapping(res, fname=None): res += "\n" return res +def do_replace(fname, before_text, after_text): + 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(): + print("Creating empty file:", fname) + fname.touch() + + content = fname.read_text() + + if not before_text.strip(): + if content: + new_content = content + after_text + else: + # first populating an empty file + new_content = after_text + else: + new_content = replace_most_similar_chunk( + content, before_text, after_text + ) + if not new_content: + return + + fname.write_text(new_content) + return True