Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify undo_edit command #6

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 4 additions & 19 deletions openhands_aci/editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ def str_replace(self, path: Path, old_str: str, new_str: str | None) -> CLIResul
"""
Implement the str_replace command, which replaces old_str with new_str in the file content.
"""
self._populate_file_history_if_having_content_before_edit(path)

file_content = self.read_file(path)
old_str = old_str.expandtabs()
new_str = new_str.expandtabs() if new_str is not None else ''
Expand Down Expand Up @@ -112,7 +110,7 @@ def str_replace(self, path: Path, old_str: str, new_str: str | None) -> CLIResul
self.write_file(path, new_file_content)

# Save the content to history
self._file_history[path].append(new_file_content)
self._file_history[path].append(file_content)

# Create a snippet of the edited section
replacement_line = file_content.split(old_str)[0].count('\n')
Expand Down Expand Up @@ -204,8 +202,6 @@ def insert(self, path: Path, insert_line: int, new_str: str) -> CLIResult:
"""
Implement the insert command, which inserts new_str at the specified line in the file content.
"""
self._populate_file_history_if_having_content_before_edit(path)

try:
file_text = self.read_file(path)
except Exception as e:
Expand Down Expand Up @@ -241,7 +237,7 @@ def insert(self, path: Path, insert_line: int, new_str: str) -> CLIResult:
snippet = '\n'.join(snippet_lines)

self.write_file(path, new_file_text)
self._file_history[path].append(new_file_text)
self._file_history[path].append(file_text)

success_message = f'The file {path} has been edited. '
success_message += self._make_output(
Expand Down Expand Up @@ -284,25 +280,14 @@ def validate_path(self, command: Command, path: Path) -> None:
f'The path {path} is a directory and only the `view` command can be used on directories.',
)

def _populate_file_history_if_having_content_before_edit(self, path: Path) -> None:
"""
Populate the file history with the current file content.
"""
# Check if the file exists or history is not empty
if len(self._file_history[path]) > 0 or not path.exists():
return

self._file_history[path].append(self.read_file(path))

def undo_edit(self, path: Path) -> CLIResult:
"""
Implement the undo_edit command.
"""
if not self._file_history[path] or len(self._file_history[path]) <= 1:
if not self._file_history[path]:
raise ToolError(f'No edit history found for {path}.')

self._file_history[path].pop()
old_text = self._file_history[path][-1]
old_text = self._file_history[path].pop()
self.write_file(path, old_text)

return CLIResult(
Expand Down