144144from prompt_toolkit .formatted_text import ANSI
145145from prompt_toolkit .history import InMemoryHistory
146146from prompt_toolkit .input import DummyInput
147+ from prompt_toolkit .key_binding import KeyBindings
147148from prompt_toolkit .output import DummyOutput
148149from prompt_toolkit .patch_stdout import patch_stdout
149150from prompt_toolkit .shortcuts import CompleteStyle , PromptSession , set_title
@@ -389,9 +390,19 @@ def __init__(
389390
390391 # Key used for tab completion
391392 self .completekey = completekey
393+ key_bindings = None
392394 if self .completekey != self .DEFAULT_COMPLETEKEY :
393- # TODO(T or K): Configure prompt_toolkit `KeyBindings` with the custom key for completion # noqa: FIX002, TD003
394- pass
395+ # Configure prompt_toolkit `KeyBindings` with the custom key for completion
396+ key_bindings = KeyBindings ()
397+
398+ @key_bindings .add (self .completekey )
399+ def _ (event : Any ) -> None :
400+ """Trigger completion."""
401+ b = event .current_buffer
402+ if b .complete_state :
403+ b .complete_next ()
404+ else :
405+ b .start_completion (select_first = False )
395406
396407 # Attributes which should NOT be dynamically settable via the set command at runtime
397408 self .default_to_shell = False # Attempt to run unrecognized commands as shell commands
@@ -448,6 +459,7 @@ def __init__(
448459 complete_style = CompleteStyle .COLUMN ,
449460 complete_in_thread = True ,
450461 complete_while_typing = False ,
462+ key_bindings = key_bindings ,
451463 )
452464 except (NoConsoleScreenBufferError , AttributeError , ValueError ):
453465 # Fallback to dummy input/output if PromptSession initialization fails.
@@ -461,6 +473,7 @@ def __init__(
461473 complete_style = CompleteStyle .COLUMN ,
462474 complete_in_thread = True ,
463475 complete_while_typing = False ,
476+ key_bindings = key_bindings ,
464477 )
465478
466479 # Commands to exclude from the history command
0 commit comments