(self, edits, dry_run=False)
| 39 | return self.apply_edits(edits, dry_run=True) |
| 40 | |
| 41 | def apply_edits(self, edits, dry_run=False): |
| 42 | failed = [] |
| 43 | passed = [] |
| 44 | updated_edits = [] |
| 45 | |
| 46 | for edit in edits: |
| 47 | path, original, updated = edit |
| 48 | full_path = self.abs_root_path(path) |
| 49 | new_content = None |
| 50 | |
| 51 | if Path(full_path).exists(): |
| 52 | content = self.io.read_text(full_path) |
| 53 | new_content = do_replace(full_path, content, original, updated, self.fence) |
| 54 | |
| 55 | # If the edit failed, and |
| 56 | # this is not a "create a new file" with an empty original... |
| 57 | # https://github.com/Aider-AI/aider/issues/2258 |
| 58 | if not new_content and original.strip(): |
| 59 | # try patching any of the other files in the chat |
| 60 | for full_path in self.abs_fnames: |
| 61 | content = self.io.read_text(full_path) |
| 62 | new_content = do_replace(full_path, content, original, updated, self.fence) |
| 63 | if new_content: |
| 64 | path = self.get_rel_fname(full_path) |
| 65 | break |
| 66 | |
| 67 | updated_edits.append((path, original, updated)) |
| 68 | |
| 69 | if new_content: |
| 70 | if not dry_run: |
| 71 | self.io.write_text(full_path, new_content) |
| 72 | passed.append(edit) |
| 73 | else: |
| 74 | failed.append(edit) |
| 75 | |
| 76 | if dry_run: |
| 77 | return updated_edits |
| 78 | |
| 79 | if not failed: |
| 80 | return |
| 81 | |
| 82 | blocks = "block" if len(failed) == 1 else "blocks" |
| 83 | |
| 84 | res = f"# {len(failed)} SEARCH/REPLACE {blocks} failed to match!\n" |
| 85 | for edit in failed: |
| 86 | path, original, updated = edit |
| 87 | |
| 88 | full_path = self.abs_root_path(path) |
| 89 | content = self.io.read_text(full_path) |
| 90 | |
| 91 | res += f""" |
| 92 | ## SearchReplaceNoExactMatch: This SEARCH block failed to exactly match lines in {path} |
| 93 | <<<<<<< SEARCH |
| 94 | {original}======= |
| 95 | {updated}>>>>>>> REPLACE |
| 96 | |
| 97 | """ |
| 98 | did_you_mean = find_similar_lines(original, content) |
no test coverage detected