diff --git a/coder.py b/coder.py index ced405abf..671342e11 100755 --- a/coder.py +++ b/coder.py @@ -16,7 +16,7 @@ from dotenv import load_dotenv from tqdm import tqdm from pathlib import Path -from utils import replace_most_similar_chunk +import utils import os import git @@ -418,11 +418,14 @@ class Coder: content = fname.read_text() - if not before_text and not content: - # first populating an empty file - new_content = after_text + if not before_text: + 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) + new_content = utils.replace_most_similar_chunk(content, before_text, after_text) if not new_content: self.console.print(f"[red]Failed to apply edit to {fname}") return diff --git a/utils.py b/utils.py index 7a1831c7c..5e539f5aa 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,7 @@ import math from difflib import SequenceMatcher +from pathlib import Path # from dump import dump @@ -41,3 +42,22 @@ def replace_most_similar_chunk(whole, part, replace): ) modified_whole = "\n".join(modified_whole) return modified_whole + + +def strip_quoted_wrapping(res, fname=None): + if not res: + return res + + res = res.splitlines() + + if fname and res[0].strip().endswith(Path(fname).name): + res = res[1:] + + if res[0].startswith("```") and res[-1].startswith("```"): + res = res[1:-1] + + res = "\n".join(res) + if res and res[-1] != "\n": + res += "\n" + + return res