Skip to content

Commit dbd6994

Browse files
committed
fix(text_editor.py): update error messages for clarity regarding file handling
feat(text_editor.py): add support for append mode when line_start exceeds total lines test(text_editor.py): add unit test for appending content in append mode
1 parent 3e9d8bc commit dbd6994

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/mcp_text_editor/text_editor.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,20 +253,18 @@ async def edit_file_contents(
253253
current_content = ""
254254
current_hash = ""
255255
lines = []
256-
elif current_hash != expected_hash:
257-
lines = []
258256
elif current_content and expected_hash == "":
259257
return {
260258
"result": "error",
261-
"reason": "Unexpected error",
259+
"reason": "Unexpected error - Cannot treat existing file as new",
262260
"file_hash": None,
263261
"content": None,
264262
}
265263
elif current_hash != expected_hash:
266264
return {
267265
"result": "error",
268266
"reason": "Hash mismatch - file has been modified",
269-
"hash": None,
267+
"file_hash": None,
270268
"content": current_content,
271269
}
272270
else:
@@ -358,9 +356,12 @@ async def edit_file_contents(
358356
if not os.path.exists(file_path) or not current_content:
359357
# New file or empty file - treat as insertion
360358
is_insertion = True
359+
elif line_start_zero >= len(lines):
360+
# Append mode - line_start exceeds total lines
361+
is_insertion = True
361362
else:
362363
# For existing files:
363-
# range_hash is required for all modifications
364+
# range_hash is required for modifications
364365
if not expected_range_hash:
365366
return {
366367
"result": "error",

tests/test_text_editor.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,3 +737,34 @@ async def test_invalid_line_range(editor, tmp_path):
737737

738738
assert result["result"] == "error"
739739
assert "End line must be greater than or equal to start line" in result["reason"]
740+
741+
742+
@pytest.mark.asyncio
743+
async def test_append_mode(editor, tmp_path):
744+
"""Test appending content when line_start exceeds total lines."""
745+
# Create a test file
746+
test_file = tmp_path / "test_append.txt"
747+
original_content = "Line 1\nLine 2\nLine 3\n"
748+
test_file.write_text(original_content)
749+
750+
# Read the content and get hash
751+
content, start, end, file_hash, total_lines, size = await editor.read_file_contents(
752+
str(test_file)
753+
)
754+
755+
# Attempt to append content with line_start > total_lines
756+
append_content = "Appended Line\n"
757+
result = await editor.edit_file_contents(
758+
str(test_file),
759+
file_hash,
760+
[
761+
{
762+
"line_start": total_lines + 1, # Start beyond current line count
763+
"contents": append_content,
764+
# No line_end or range_hash needed for append mode
765+
}
766+
],
767+
)
768+
769+
assert result["result"] == "ok"
770+
assert test_file.read_text() == original_content + append_content

0 commit comments

Comments
 (0)