Skip to content

Commit a4cb911

Browse files
committed
fix(text_editor.py): optimize hash calculation by storing file content and hash in variables to avoid redundant calculations
1 parent af2df77 commit a4cb911

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/mcp_text_editor/text_editor.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,17 @@ async def read_multiple_ranges(
9898
with open(file_path, "r", encoding="utf-8") as f:
9999
lines = f.readlines()
100100
total_lines = len(lines)
101+
file_content = "".join(lines)
102+
file_hash = self.calculate_hash(file_content)
101103

102104
for range_spec in file_range["ranges"]:
103105
# Adjust line numbers to 0-based index
104106
line_start = max(1, range_spec["start"]) - 1
107+
end_value = range_spec.get("end")
105108
line_end = (
106109
total_lines
107-
if range_spec["end"] is None
108-
else min(range_spec["end"], total_lines)
110+
if end_value is None
111+
else min(end_value, total_lines)
109112
)
110113

111114
if line_start >= total_lines:
@@ -125,7 +128,7 @@ async def read_multiple_ranges(
125128
"content": content,
126129
"start_line": line_start + 1,
127130
"end_line": line_end,
128-
"hash": self.calculate_hash(content),
131+
"hash": file_hash,
129132
"total_lines": total_lines,
130133
"content_size": len(content),
131134
}

0 commit comments

Comments
 (0)