Skip to content

Commit bcd2da1

Browse files
authored
fix: extra new line when using view command with view_range (#112)
* fix extra new line * bump to 0.2.10 * fix test
1 parent 5ef3dcd commit bcd2da1

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

openhands_aci/editor/editor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,9 @@ def view(self, path: Path, view_range: list[int] | None = None) -> CLIResult:
335335
file_content = self.read_file(path, start_line=start_line, end_line=end_line)
336336

337337
# Get the detected encoding
338-
output = self._make_output(file_content, str(path), start_line)
338+
output = self._make_output(
339+
'\n'.join(file_content.splitlines()), str(path), start_line
340+
) # Remove extra newlines
339341

340342
return CLIResult(
341343
path=str(path),
@@ -618,7 +620,7 @@ def _make_output(
618620
snippet_content = '\n'.join(
619621
[
620622
f'{i + start_line:6}\t{line}'
621-
for i, line in enumerate(snippet_content.splitlines())
623+
for i, line in enumerate(snippet_content.split('\n'))
622624
]
623625
)
624626
return (

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "openhands-aci"
3-
version = "0.2.9"
3+
version = "0.2.10"
44
description = "An Agent-Computer Interface (ACI) designed for software development agents OpenHands."
55
authors = ["OpenHands"]
66
license = "MIT"

tests/integration/editor/test_basic_operations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ def test_file_editor_with_xml_tag_parsing(temp_file):
102102
5\t re.DOTALL,
103103
6\t)
104104
7\t...More text here.
105+
8\t
105106
"""
106107
)
107108

tests/integration/test_oh_editor.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def test_view_file(editor):
4040
assert f"Here's the result of running `cat -n` on {test_file}:" in result.output
4141
assert '1\tThis is a test file.' in result.output
4242
assert '2\tThis file is for testing purposes.' in result.output
43+
assert '3\t' not in result.output # No extra line
4344

4445

4546
def test_view_directory(editor):
@@ -54,6 +55,30 @@ def test_view_directory(editor):
5455
)
5556

5657

58+
def test_view_with_a_specific_range(editor):
59+
editor, test_file = editor
60+
61+
# Replace the current content with content: Line {line_number}
62+
_ = editor(
63+
command='str_replace',
64+
path=str(test_file),
65+
old_str='This is a test file.\nThis file is for testing purposes.',
66+
new_str='',
67+
)
68+
for i in range(0, 200):
69+
_ = editor(
70+
command='insert', path=str(test_file), insert_line=i, new_str=f'Line {i+1}'
71+
)
72+
73+
# View file in range 50-100
74+
result = editor(command='view', path=str(test_file), view_range=[50, 100])
75+
assert f"Here's the result of running `cat -n` on {test_file}:" in result.output
76+
assert ' 49\tLine 49' not in result.output
77+
assert ' 50\tLine 50' in result.output
78+
assert ' 100\tLine 100' in result.output
79+
assert '101' not in result.output
80+
81+
5782
def test_create_file(editor):
5883
editor, test_file = editor
5984
new_file = test_file.parent / 'new_file.txt'
@@ -73,6 +98,11 @@ def test_create_with_empty_string(editor):
7398
assert new_file.read_text() == ''
7499
assert 'File created successfully' in result.output
75100

101+
# Test the view command showing an empty line
102+
result = editor(command='view', path=str(new_file))
103+
assert f"Here's the result of running `cat -n` on {new_file}:" in result.output
104+
assert '1\t' in result.output # Check for empty line
105+
76106

77107
def test_create_with_none_file_text(editor):
78108
editor, test_file = editor
@@ -100,7 +130,6 @@ def test_str_replace_no_linting(editor):
100130
2\tThis file is for testing purposes.
101131
Review the changes and make sure they are as expected. Edit the file again if necessary."""
102132
)
103-
print(result.output)
104133

105134
# Test that the file content has been updated
106135
assert 'This is a sample file.' in test_file.read_text()
@@ -265,7 +294,6 @@ def test_insert_no_linting(editor):
265294
)
266295
assert isinstance(result, CLIResult)
267296
assert 'Inserted line' in test_file.read_text()
268-
print(result.output)
269297
assert (
270298
result.output
271299
== f"""The file {test_file} has been edited. Here's the result of running `cat -n` on a snippet of the edited file:
@@ -287,7 +315,6 @@ def test_insert_with_linting(editor):
287315
)
288316
assert isinstance(result, CLIResult)
289317
assert 'Inserted line' in test_file.read_text()
290-
print(result.output)
291318
assert (
292319
result.output
293320
== f"""The file {test_file} has been edited. Here's the result of running `cat -n` on a snippet of the edited file:

0 commit comments

Comments
 (0)