Skip to content

Commit c437600

Browse files
authored
Merge pull request #21 from KavyaSree2610/kkaitepalli/fix-bounds
fix: Improve line range handling in _viewcode method
2 parents 275a968 + a073db0 commit c437600

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/tools/project.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,18 @@ def _viewcode(self, ref: str, path: str, startline: int, endline: int) -> str:
102102
content = file.data_stream.read().decode("utf-8", errors="ignore")
103103
lines = content.split("\n")
104104
ret = []
105-
if endline > len(lines):
106-
startline -= endline - len(lines)
105+
if not lines:
106+
return "This file is empty.\n"
107+
if startline > endline:
108+
startline, endline = endline, startline
109+
startline = max(1, startline)
110+
if startline > len(lines):
111+
ret.append(
112+
f"This file only has {len(lines)} lines. Showing full file.\n"
113+
)
114+
startline = 1
115+
endline = len(lines)
116+
elif endline > len(lines):
107117
endline = len(lines)
108118
ret.append(
109119
f"This file only has {len(lines)} lines. Here are lines {startline} through {endline}.\n"

0 commit comments

Comments
 (0)