prompt-toolkit / prompt-toolkit/python-prompt-toolkit

Unable to redo after undo

Open
#1,703 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
10.6k
Forks
815
PR merge metrics
No merged PRs in 30d

Description

Hello!

While tweaking my ipython REPL keybindings, I discovered that buf.redo() never works.

Here is a little script that shows the issue:

#!/usr/bin/env python

from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings


def dyn_prompt(buf):
    def inner():
        return f"undo: {len(buf._undo_stack)} | redo: {len(buf._redo_stack)}"
    return inner


def main():
    kb = KeyBindings()

    @kb.add("escape", "u")
    def _(event):
        event.current_buffer.undo()

    @kb.add("escape", "r")
    def _(event):
        event.current_buffer.redo()

    print("Try to type some text, then try: Alt-u to undo | Alt-r to redo")
    s = PromptSession("> ", key_bindings=kb)
    s.rprompt = dyn_prompt(s.app.current_buffer)
    try:
        s.prompt()
    except (EOFError, KeyboardInterrupt):
        pass


if __name__ == "__main__":
    main()
  1. Input abcdef
  2. RPrompt is now undo: 6 | redo: 0
  3. Type Alt-u to undo (remove f)
  4. RPrompt is now undo: 5 | redo: 1
  5. Type Alt-u to undo again (remove e)
  6. RPrompt is now undo: 4 | redo: 1 (redo didn't change!)
  7. Type Alt-r to try to redo (add e)
  8. ⚠️ e is not added, redo didn't work!
  9. RPrompt is now undo: 5 | redo: 0

Is that the right way to try to use undo/redo ?
Is that a bug on your side maybe?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the provided PromptSession and KeyBindings script, then inspect the buffer undo() and redo() entry points and their undo/redo stack handling. Done means repeated undo operations preserve the expected redo history and Alt-r restores the removed character, with regression coverage for this sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.