Implement the insert command, which inserts new_str at the specified line in the file content.
(self, path: Path, insert_line: int, new_str: str)
| 641 | pre_edit_lint = flake8(str(path)) |
| 642 | except Exception as e: # noqa: BLE001 — linting must never block an edit |
| 643 | self.logs.append(f"Warning: Failed to run pre-edit linter on {path}: {e}") |
| 644 | |
| 645 | # Replace old_str with new_str |
| 646 | new_file_content = file_content.replace(old_str, new_str) |
| 647 | |
| 648 | # Write the new content to the file |
| 649 | self.write_file(path, new_file_content) |
| 650 | |
| 651 | post_edit_lint = "" |
| 652 | if USE_LINTER: |
| 653 | try: |
| 654 | post_edit_lint = flake8(str(path)) |
| 655 | except Exception as e: # noqa: BLE001 — linting must never block an edit |
| 656 | self.logs.append(f"Warning: Failed to run post-edit linter on {path}: {e}") |
| 657 | |
| 658 | epilogue = "" |
| 659 | if post_edit_lint: |
| 660 | replacement_window_start_line = file_content.split(old_str)[0].count("\n") + 1 |
| 661 | replacement_lines = len(new_str.split("\n")) |
| 662 | replacement_window_end_line = replacement_window_start_line + replacement_lines - 1 |
| 663 | replacement_window = (replacement_window_start_line, replacement_window_end_line) |
| 664 | errors = format_flake8_output( |
| 665 | post_edit_lint, |
| 666 | previous_errors_string=pre_edit_lint, |
| 667 | replacement_window=replacement_window, |
| 668 | replacement_n_lines=replacement_lines, |
| 669 | ) |
| 670 | if errors.strip(): |
| 671 | epilogue = LINT_WARNING_TEMPLATE.format(errors=errors) |
| 672 | |
| 673 | # Save the content to history |
| 674 | self._file_history[path].append(file_content) |
| 675 | |
| 676 | # Create a snippet of the edited section |
| 677 | replacement_line = file_content.split(old_str)[0].count("\n") |
| 678 | start_line = max(1, replacement_line - SNIPPET_LINES) |
| 679 | end_line = min( |
| 680 | replacement_line + SNIPPET_LINES + new_str.count("\n"), |
| 681 | len(new_file_content.splitlines()), |
| 682 | ) |
no test coverage detected