Skip to content

Commit d7f00c0

Browse files
committed
fix(text_editor.py): handle out of bounds start line by returning empty content instead of raising an error
test(text_editor.py): add unit tests for reading ranges with out of bounds start lines to ensure correct behavior
1 parent f4f7c30 commit d7f00c0

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/mcp_text_editor/text_editor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,18 @@ async def read_multiple_ranges(
112112
)
113113

114114
if line_start >= total_lines:
115-
raise ValueError(
116-
f"Start line {line_start + 1} exceeds file length {total_lines}"
115+
# Return empty content for out of bounds start line
116+
result[file_path].append(
117+
{
118+
"content": "",
119+
"start_line": line_start + 1,
120+
"end_line": line_start + 1,
121+
"hash": file_hash,
122+
"total_lines": total_lines,
123+
"content_size": 0,
124+
}
117125
)
126+
continue
118127
if line_end < line_start:
119128
raise ValueError(
120129
"End line must be greater than or equal to start line"
@@ -172,7 +181,7 @@ async def read_file_contents(
172181
line_end = len(lines) if line_end is None else min(line_end, len(lines))
173182

174183
if line_start >= len(lines):
175-
raise ValueError("Start line exceeds file length")
184+
return "", line_start, line_start, file_hash, len(lines), 0
176185
if line_end < line_start:
177186
raise ValueError("End line must be greater than or equal to start line")
178187

tests/test_text_editor.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,35 @@ async def test_read_multiple_ranges(editor, test_file):
309309
assert second_range["content"] == "Line 2\nLine 3\nLine 4\n"
310310
assert second_range["start_line"] == 2
311311
assert second_range["end_line"] == 4
312+
313+
314+
@pytest.mark.asyncio
315+
async def test_read_multiple_ranges_out_of_bounds_start(editor, test_file):
316+
"""Test reading ranges where start line exceeds file length."""
317+
ranges = [
318+
{
319+
"file_path": test_file,
320+
"ranges": [
321+
{"start": 1000}, # Way beyond file end
322+
{"start": 6}, # Just beyond file end
323+
],
324+
}
325+
]
326+
327+
result = await editor.read_multiple_ranges(ranges)
328+
329+
# Check first range (start line way beyond file end)
330+
first_range = result[test_file][0]
331+
assert first_range["content"] == ""
332+
assert first_range["start_line"] == 1000
333+
assert first_range["end_line"] == 1000
334+
assert first_range["total_lines"] == 5
335+
assert first_range["content_size"] == 0
336+
337+
# Check second range (start line just beyond file end)
338+
second_range = result[test_file][1]
339+
assert second_range["content"] == ""
340+
assert second_range["start_line"] == 6
341+
assert second_range["end_line"] == 6
312342
assert second_range["total_lines"] == 5
343+
assert second_range["content_size"] == 0

0 commit comments

Comments
 (0)