From 04de1a56b36eed7780c0d12171926ba368601a97 Mon Sep 17 00:00:00 2001 From: Kavya Sree Kaitepalli Date: Thu, 26 Feb 2026 06:08:30 +0000 Subject: [PATCH 1/2] fix: Improve line range handling in _viewcode method --- src/tools/project.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/tools/project.py b/src/tools/project.py index c8b6694..cceb493 100644 --- a/src/tools/project.py +++ b/src/tools/project.py @@ -102,9 +102,14 @@ def _viewcode(self, ref: str, path: str, startline: int, endline: int) -> str: content = file.data_stream.read().decode("utf-8", errors="ignore") lines = content.split("\n") ret = [] - if endline > len(lines): - startline -= endline - len(lines) - endline = len(lines) + if not lines: + return "This file is empty.\n" + if startline > endline: + startline, endline = endline, startline + clamped = endline > len(lines) + startline = max(1, min(startline, len(lines))) + endline = min(endline, len(lines)) + if clamped: ret.append( f"This file only has {len(lines)} lines. Here are lines {startline} through {endline}.\n" ) From a073db085233084c95cd5422da5b2476e2edc43f Mon Sep 17 00:00:00 2001 From: Kavya Sree Kaitepalli Date: Thu, 26 Feb 2026 06:21:07 +0000 Subject: [PATCH 2/2] fix: Enhance line range validation in _viewcode method --- src/tools/project.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/tools/project.py b/src/tools/project.py index cceb493..cafe581 100644 --- a/src/tools/project.py +++ b/src/tools/project.py @@ -106,10 +106,15 @@ def _viewcode(self, ref: str, path: str, startline: int, endline: int) -> str: return "This file is empty.\n" if startline > endline: startline, endline = endline, startline - clamped = endline > len(lines) - startline = max(1, min(startline, len(lines))) - endline = min(endline, len(lines)) - if clamped: + startline = max(1, startline) + if startline > len(lines): + ret.append( + f"This file only has {len(lines)} lines. Showing full file.\n" + ) + startline = 1 + endline = len(lines) + elif endline > len(lines): + endline = len(lines) ret.append( f"This file only has {len(lines)} lines. Here are lines {startline} through {endline}.\n" )