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 def str_replace(self, path: Path, old_str: str, new_str: str | None):
642 """Implement the str_replace command, which replaces old_str with new_str in the file content"""
643 # Read the file content
644 history_content = self.read_file(path)
645 file_content = history_content.expandtabs()
646 old_str = old_str.expandtabs()
647 new_str = new_str.expandtabs() if new_str is not None else ""
648
649 # Check if old_str is unique in the file
650 occurrences = file_content.count(old_str)
651 if occurrences == 0:
652 self.logs.append(
653 f"No replacement was performed, old_str `{old_str}` did not appear verbatim in {self._get_display_path(path)}."
654 )
655 return
656 elif occurrences > 1:
657 file_content_lines = file_content.split("\n")
658 lines = [idx + 1 for idx, line in enumerate(file_content_lines) if old_str in line]
659 self.logs.append(
660 f"No replacement was performed. Multiple occurrences of old_str `{old_str}` in lines {lines}. Please ensure it is unique"
661 )
662 return
663
664 if new_str == old_str:
665 self.logs.append(
666 f"No replacement was performed, old_str `{old_str}` is the same as new_str `{new_str}`."
667 )
668 return
669
670 pre_edit_lint = ""
671 if USE_LINTER:
672 try:
673 pre_edit_lint = flake8(str(path))
674 except Exception as e: # noqa: BLE001 — linting must never block an edit
675 self.logs.append(f"Warning: Failed to run pre-edit linter on {path}: {e}")
676
677 # Replace old_str with new_str
678 new_file_content = file_content.replace(old_str, new_str)
679
680 # Write the new content to the file
681 if not self.write_file(path, new_file_content):
682 return

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