read
Extract specific lines from a text file using start and end line numbers. Returns a dictionary with line numbers and content, simplifying targeted text selection for editing.
Instructions
Read lines from the current file from start line to end line, returning them in a dictionary like {"lines":[[1,"text on first line"],[2,"text on second line"]]}. This makes it easier to find the precise lines to select for editing.
Args: start (int, optional): Start line number end (int, optional): End line number
Returns: dict: lines, start_line, end_line
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end | Yes | ||
| start | Yes |
Implementation Reference
- src/text_editor/server.py:455-499 (handler)The handler function that implements the 'read' tool. It reads a range of lines (start to end) from the current file set via set_file, formats them as a list of [line_number, content] pairs, and returns them along with start/end info. Includes validation and error handling.async def read(start: int, end: int) -> Dict[str, Any]: """ Read lines from the current file from start line to end line, returning them in a dictionary like {"lines":[[1,"text on first line"],[2,"text on second line"]]}. This makes it easier to find the precise lines to select for editing. Args: start (int, optional): Start line number end (int, optional): End line number Returns: dict: lines, start_line, end_line """ result = {} if self.current_file_path is None: return {"error": "No file path is set. Use set_file first."} try: with open(self.current_file_path, "r", encoding="utf-8") as file: lines = file.readlines() if start < 1: return {"error": "start must be at least 1"} if end > len(lines): end = len(lines) if start > end: return { "error": f"{start=} cannot be greater than {end=}. {len(lines)=}" } selected_lines = lines[start - 1 : end] formatted_lines = [] for i, line in enumerate(selected_lines, start): formatted_lines.append((i, line.rstrip())) result["lines"] = formatted_lines result["start_line"] = start result["end_line"] = end return result except Exception as e: return {"error": f"Error reading file: {str(e)}"}