MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / insert

Method insert

codewiki/src/be/agent_tools/str_replace_editor.py:643–679  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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 )

Callers 5

__call__Method · 0.95
run_web_app.pyFile · 0.80
_namespace_forMethod · 0.80
smoke_test_mcp.pyFile · 0.80

Calls 4

read_fileMethod · 0.95
write_fileMethod · 0.95
_get_display_pathMethod · 0.95
_make_outputMethod · 0.95

Tested by

no test coverage detected