prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.28k stars 715 forks source link

Completer triggered by the Backspace key #491

Open quarxpl opened 7 years ago

quarxpl commented 7 years ago

Currently, in CommandLineInterface used by the prompt a completer is only called when text is inserted. I want to update auto-completions also when the Backspace key is pressed. I'm working on an "AJAX" search and without support for the Backspace key, it feels awkward. Could you please add an option to trigger a completer on any buffer change, not just inserts?

The only way I can fix it for now is with a monkey patch like this:

from prompt_toolkit.interface import CommandLineInterface
_add_buffer = CommandLineInterface.add_buffer
def add_buffer(self, name, buffer, focus = False):
        _add_buffer(self, name, buffer, focus)
        buffer.on_text_changed += lambda _: self._async_completers[name]()
CommandLineInterface.add_buffer = add_buffer
asmodehn commented 2 years ago

I recently add the same issue with prompt-toolkit 3.0.29 The completer is triggered on text inserted, not on text changed, with complete_while_typing=True

In my usecase, my completer is dynamic and depends on each character in the input. The user would expect backspace to trigger completion based on the text before the cursor. This monkey patch does work in a recent version, sadly. I didn't come up with a solution yet, I will post here if I do.