Skip to content

Return an empty character before the document start - #2096

Open
emme1t wants to merge 1 commit into
prompt-toolkit:mainfrom
emme1t:fix/document-character-before-start
Open

emme1t wants to merge 1 commit into
prompt-toolkit:mainfrom
emme1t:fix/document-character-before-start

Conversation

@emme1t

@emme1t emme1t commented Sep 12, 2026

Copy link
Copy Markdown

Document('abc', cursor_position=0).char_before_cursor currently returns 'c'. The property documents an empty string when there is no preceding character, so the expected result is ''.

Check the absolute character index before indexing the text. Python's negative indices otherwise make a lookup before the beginning wrap to the end. The existing handling for positions at the end of the document remains in place.

The regression cases cover the beginning, middle, and end of text, an empty document, a trailing newline, and a Unicode character.

Validation on Windows with Python 3.13.13:

  • New boundary cases before the fix: 3 failed, 5 passed.
  • python -m pytest tests/test_document.py -q: 20 passed.
  • python -m pytest tests -q: 155 passed, 5 skipped, 4 failed. All four failures also reproduce in a clean worktree at the base commit (147 passed, 5 skipped, the same 4 failed): two history-input tests and two tests raising NoConsoleScreenBufferError in this console-free environment.
  • Repository-wide Ruff lint passes; formatting passes for both changed files.
  • mypy --strict src/ passes for win32, linux, and darwin (145 files each).

AI assistance: Prepared with Codex. The reproduction, regression tests, and clean-base comparison above were executed locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant