Skip to content

fix(buffer): enable history navigation when accept_handler keeps text (#1185) - #2095

Open
aoright wants to merge 3 commits into
prompt-toolkit:mainfrom
aoright:fix-history-navigation-accept-handler-keep-text
Open

aoright wants to merge 3 commits into
prompt-toolkit:mainfrom
aoright:fix-history-navigation-accept-handler-keep-text

Conversation

@aoright

@aoright aoright commented Sep 11, 2026

Copy link
Copy Markdown

Problem

Fixes #1185.
When accept_handler returns True (keep_text), the buffer was previously bypassed from calling self.reset(). While this succeeded in preserving the text in the buffer, it had severe unintended side effects:

  1. _load_history_task was never reset to None, so newly appended history entries from self.append_to_history() were never loaded into self._working_lines.
  2. As a result, subsequent calls to history_backward() / history_forward() (using the Up/Down arrow keys) failed to navigate any history entries.
  3. Transient states such as validation errors and selection states were not cleared.

Solution

  1. In validate_and_handle, when keep_text is True, call self.reset(document=Document(self.text, self.cursor_position)). This resets transient states and clears _load_history_task so history can reload on the next render.
  2. In load_history, avoid duplicating the working line when it is already identical to the most recent history item.
  3. Added an async unit test in tests/test_buffer.py to verify that history navigation works as expected across multiple accepts with keep_text=True.

Fixes prompt-toolkit#1185. When accept_handler returns True (keep_text), the buffer was previously not reset at all, which left _load_history_task unchanged and prevented newly appended history entries from being loaded into _working_lines.

Reset the buffer with the current document to properly reset transient state and trigger history reloading, while avoiding duplicate working lines when the latest history item matches the working line.
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.

accept_handler return True crashes history in full-screen app

1 participant