Skip to content

Commit a803fcd

Browse files
author
Yoshihiro Takahara
committed
feat(models): add FileRange and FileRanges models for handling line ranges
- Add FileRange pydantic model for representing file line ranges - Add FileRanges model for managing multiple ranges in a file - Add comprehensive test cases for new models - Use Field descriptors for better documentation
1 parent f941065 commit a803fcd

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/mcp_text_editor/models.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,21 @@ class EditTextFileContentsRequest(BaseModel):
8080
"""
8181

8282
files: List[EditFileOperation] = Field(..., description="List of file operations")
83+
84+
85+
class FileRange(BaseModel):
86+
"""Represents a line range in a file."""
87+
88+
start: int = Field(..., description="Starting line number (1-based)")
89+
end: Optional[int] = Field(
90+
None, description="Ending line number (null for end of file)"
91+
)
92+
93+
94+
class FileRanges(BaseModel):
95+
"""Represents a file and its line ranges."""
96+
97+
file_path: str = Field(..., description="Path to the text file")
98+
ranges: List[FileRange] = Field(
99+
..., description="List of line ranges to read from the file"
100+
)

tests/test_models.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
EditPatch,
99
EditResult,
1010
EditTextFileContentsRequest,
11+
FileRange,
12+
FileRanges,
1113
GetTextFileContentsRequest,
1214
GetTextFileContentsResponse,
1315
)
@@ -186,3 +188,43 @@ def test_edit_result_to_dict():
186188
"hash": "currenthash123",
187189
"content": "current content",
188190
}
191+
192+
193+
def test_file_range():
194+
"""Test FileRange model."""
195+
# Test with only required field
196+
range_ = FileRange(start=1)
197+
assert range_.start == 1
198+
assert range_.end is None # Default value
199+
200+
# Test with all fields
201+
range_ = FileRange(start=5, end=10)
202+
assert range_.start == 5
203+
assert range_.end == 10
204+
205+
# Test validation error - missing required field
206+
with pytest.raises(ValidationError):
207+
FileRange()
208+
209+
210+
def test_file_ranges():
211+
"""Test FileRanges model."""
212+
ranges = [
213+
FileRange(start=1),
214+
FileRange(start=5, end=10),
215+
]
216+
file_ranges = FileRanges(file_path="/path/to/file.txt", ranges=ranges)
217+
assert file_ranges.file_path == "/path/to/file.txt"
218+
assert len(file_ranges.ranges) == 2
219+
assert file_ranges.ranges[0].start == 1
220+
assert file_ranges.ranges[0].end is None
221+
assert file_ranges.ranges[1].start == 5
222+
assert file_ranges.ranges[1].end == 10
223+
224+
# Test validation error - missing required fields
225+
with pytest.raises(ValidationError):
226+
FileRanges()
227+
228+
# Test validation error - invalid ranges type
229+
with pytest.raises(ValidationError):
230+
FileRanges(file_path="/path/to/file.txt", ranges="invalid")

0 commit comments

Comments
 (0)