Document.translate_row_col_to_index does not clamp negative rows to zero

Author: emme1tCreated Sep 12, 2026Updated Sep 12, 2026

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:

python
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.

Source: prompt-toolkit/python-prompt-toolkit