Skip to content

Auto suggestions not called after text deletion #2016

@Maxwellfire

Description

@Maxwellfire

The autosuggester for a buffer does not seem to be called after deleting text from the buffer. Instead, it is only called when inserting text.

Demonstration:

from prompt_toolkit.auto_suggest import AutoSuggest, Suggestion
from prompt_toolkit import PromptSession

def main():  
      session = PromptSession(
          auto_suggest=AutoSuggestFoo(),
      )
  
      while True:
          try:
              text = session.prompt("Say something: ")
          except KeyboardInterrupt:
              pass  # Ctrl-C pressed. Try again.
          else:
              break
  
      print(f"You said: {text}")

class AutoSuggestFoo(AutoSuggest):
    """
    Give suggestions based on the lines in the history.
    """

    def get_suggestion(self, buffer, document):
        return Suggestion(" example completion")

Expected behavior:

example completion should be shown at the end of the prompt at all times, since the completer is not conditional and it always returns example completion

Actual behavior:

example completion is only shown at the end of the prompt when we are typing text. If we delete text, the suggestion goes away until we start typing again

Diagnosis:

It seems like the completion is applied in

self.control = BufferControl(
buffer=self.buffer,
lexer=DynamicLexer(lambda: self.lexer),
input_processors=[
ConditionalProcessor(
AppendAutoSuggestion(), has_focus(self.buffer) & ~is_done
),
ConditionalProcessor(
processor=PasswordProcessor(), filter=to_filter(password)
),
BeforeInput(prompt, style="class:text-area.prompt"),
]

Does the buffer somehow lose focus or is_done become true after deleting?

Are input_processor in a buffer only applied to newly typed text? Does this mean that all formatting doesn't get reapplied on deletion?

Is

I'm happy to trace this more, but if someone can say whether this is intentional behavior (and if so is it explicitly implemented or a consequence of the architecture) that would be very helpful. Debugging async code to determine why this the input_processor isn't being called is a bit hard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions