diff --git a/coder.py b/coder.py index 84362be3f..4a44f893f 100755 --- a/coder.py +++ b/coder.py @@ -296,13 +296,14 @@ class Coder: return ''.join(resp) - pattern = re.compile(r'(\S+)\s+<<<<<<< ORIGINAL\n(.*?)\n=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL) + pattern = re.compile(r'(\S+)\s+(```)?<<<<<<< ORIGINAL\n(.*?\n?)=======\n(.*?\n?)>>>>>>> UPDATED', re.MULTILINE | re.DOTALL) def update_files(self, content, inp): edited = set() for match in self.pattern.finditer(content): - path, original, updated = match.groups() + path, _, original, updated = match.groups() + edited.add(path) if self.do_replace(path, original, updated): continue @@ -317,17 +318,22 @@ class Coder: fname = Path(fname) content = fname.read_text().splitlines() - before_lines = [l.strip() for l in before_text.splitlines()] - stripped_content = [l.strip() for l in content] - where = find_index(stripped_content, before_lines) - if where < 0: - return + if not before_text and not content: + # first populating an empty file + new_content = after_text + else: + before_lines = [l.strip() for l in before_text.splitlines()] + stripped_content = [l.strip() for l in content] + where = find_index(stripped_content, before_lines) - new_content = content[:where] - new_content += after_text.splitlines() - new_content += content[where+len(before_lines):] - new_content = '\n'.join(new_content) + '\n' + if where < 0: + return + + new_content = content[:where] + new_content += after_text.splitlines() + new_content += content[where+len(before_lines):] + new_content = '\n'.join(new_content) + '\n' fname.write_text(new_content) print('Applied edit to', fname) diff --git a/prompts.py b/prompts.py index 32f290b41..b4385a577 100644 --- a/prompts.py +++ b/prompts.py @@ -28,6 +28,7 @@ some/dir/names/example.py >>>>>>> UPDATED Be sure to include the correct path and filename for each edit, exactly as specified by the user. +Don't use ``` to demarcate code blocks! LEADING WHITESPACE IS IMPORTANT IN ALL CODE! NEVER RETURN THE ENTIRE SOURCE FILE. ONLY RETURN CODE IN ORIGINAL/UPDATED FORMAT CHANGES! '''