Skip to content

Commit

Permalink
Made some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
RajWorking committed Nov 18, 2024
1 parent 182079c commit 6cd591b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ipython_config.py
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
poetry.lock
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
Expand Down
33 changes: 15 additions & 18 deletions openhands_aci/editor/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,8 @@ def __call__(
raise EditorToolParameterMissingError(command, 'new_str')
return self.insert(_path, insert_line, new_str, enable_linting)
elif command == 'delete':
if not lines_range:
raise EditorToolParameterMissingError(command, 'lines_range')
return self.delete(_path, lines_range)
elif command == 'move_code_block':
if not lines_range:
raise EditorToolParameterMissingError(command, 'lines_range')
if insert_line is None:
raise EditorToolParameterMissingError(command, 'insert_line')
if not dst_path:
Expand Down Expand Up @@ -268,20 +264,17 @@ def insert(
success_message += 'Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary.'
return CLIResult(output=success_message)

def delete(self, path: Path, lines_range: list[int]) -> CLIResult:
def delete(self, path: Path, lines_range: list[int] | None = None) -> CLIResult:
"""
Deletes text content in file from the given range.
"""
try:
file_text = self.read_file(path)
except Exception as e:
raise ToolError(f'Ran into {e} while trying to read {path}') from None

file_text = file_text.expandtabs()

file_text = self.read_file(path).expandtabs()
file_text_lines = file_text.split('\n')
num_lines = len(file_text_lines)

if not lines_range:
lines_range = [0, num_lines - 1] # Delete all lines

self._validate_range(lines_range, num_lines)

start_line, end_line = lines_range # inclusive
Expand Down Expand Up @@ -312,24 +305,28 @@ def delete(self, path: Path, lines_range: list[int]) -> CLIResult:
return CLIResult(output=success_message)

def move_code_block(
self, from_file: Path, from_range: list[int], dst_file: Path, insert_line: int
self, path: Path, from_range: list[int] | None, dst_path: Path, insert_line: int
) -> CLIResult:
"""
Move a block of code from one file to another.
"""
file_content = self.read_file(from_file)
file_content_lines = file_content.split('\n')
file_content_lines = self.read_file(path).expandtabs().split('\n')
num_lines = len(file_content_lines)

if not from_range:
from_range = [0, num_lines - 1] # Delete all lines

start_line, end_line = from_range
code_block = '\n'.join(
file_content_lines[start_line:]
if end_line == -1
else file_content_lines[start_line: end_line + 1]
)
delete_result = self.delete(from_file, from_range)
insert_result = self.insert(dst_file, insert_line, code_block, True)
delete_result = self.delete(path, from_range)
insert_result = self.insert(dst_path, insert_line, code_block, True)

return CLIResult(
output=f'Code block moved from {from_file} to {dst_file}.\n{delete_result.output}\n{insert_result.output}'
output=f'Code block moved from {path} to {dst_path}.\n{delete_result.output}\n{insert_result.output}'
)

def validate_path(self, command: Command, path: Path) -> None:
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/test_file_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ def test_insert_with_linting(editor):
Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary."""
)

def test_delete_block(editor):
editor, test_file = editor
test_file.write_text(
'This is a test file.\nThis file is for testing purposes.\nfoo\nbar\nbaz'
)
result = editor(
command='delete',
path=str(test_file),
lines_range=[2, 3],
)
assert isinstance(result, CLIResult)
assert 'foo' not in test_file.read_text()
assert 'bar' not in test_file.read_text()
print(result.output)
assert (
result.output
== f"""The file {test_file} has been edited. Here's the result of running `cat -n` on a snippet of the edited file:
1\tThis is a test file.
2\tThis file is for testing purposes.
3\tbaz
Review the changes and make sure they are as expected (correct indentation, no duplicate lines, etc). Edit the file again if necessary.""")


def test_move_code_block(editor):
editor, test_file = editor
Expand Down

0 comments on commit 6cd591b

Please sign in to comment.