prompt-toolkit / prompt-toolkit/python-prompt-toolkit
Document.translate_row_col_to_index does not clamp negative rows to zero
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 10.6k
- Forks
- 815
- PR merge metrics
- No merged PRs in 30d
Description
Document.translate_row_col_to_index() documents that negative row and column values are turned into zero. Negative rows within the list's indexing range currently select lines from the end of the document.
Reproducer:
from prompt_toolkit.document import Document
document = Document('one\ntwo\nthree')
for row in (-1, -2, -3, -4):
print(row, document.translate_row_col_to_index(row, 0))
Actual output:
-1 8
-2 4
-3 0
-4 0
Expected output from the documented clamping behavior:
-1 0
-2 0
-3 0
-4 0
The behavior is inconsistent across negative row values because _line_start_indexes[row] accepts negative list indices, and the zero-clamping path is reached only after an IndexError. The same issue occurs with a positive column: translate_row_col_to_index(-1, 1) returns 9 where the documented result is 1.
Verified on the current main commit 583b3412c792a5cc9f01adde603679f3824a88f3 (3.0.53), Windows, Python 3.13.13.
AI assistance: Codex assisted with investigation and drafting; the examples above were executed against a clean checkout.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating Document.translate_row_col_to_index() and run the issue's reproducer. Check negative row handling against the documented clamping behavior, including positive columns, and verify that every negative row returns the expected zero-based result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- cli
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100