From 7b3041ad90891aa92b2e47b94b7a571333c6c64e Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Tue, 14 Jan 2025 11:38:17 +0100 Subject: [PATCH] Add ability to change the autosuggestion class. Mostly up for discussion;but while working with multiline completion and LLM, I realised that in IPython it is likely Ii also want to handle suggestion when on the last line, so I think i'd like to ed the autocompltion class to a no-op one. Ideally I think it woudl make sens to completly control the full conditional processor; so mostly openeing that for discussion. --- src/prompt_toolkit/shortcuts/prompt.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/prompt_toolkit/shortcuts/prompt.py b/src/prompt_toolkit/shortcuts/prompt.py index d0732bc13..ec2ee2e33 100644 --- a/src/prompt_toolkit/shortcuts/prompt.py +++ b/src/prompt_toolkit/shortcuts/prompt.py @@ -328,6 +328,10 @@ class PromptSession(Generic[_T]): there is a keyboard interrupt (control-c keypress). :param eof_exception: The exception type that will be raised when there is an end-of-file/exit event (control-d keypress). + :param append_autosuggestion_class: The class of the `Processor` that will + be used to append autosuggestions to the current buffer. Default to + `AppendAutoSuggestion`, that can only handle single line suggestions to + the last line of the current buffer """ _fields = ( @@ -416,6 +420,7 @@ def __init__( output: Output | None = None, interrupt_exception: type[BaseException] = KeyboardInterrupt, eof_exception: type[BaseException] = EOFError, + append_autosuggestion_class=AppendAutoSuggestion, ) -> None: history = history or InMemoryHistory() clipboard = clipboard or InMemoryClipboard() @@ -472,7 +477,9 @@ def __init__( self.history = history self.default_buffer = self._create_default_buffer() self.search_buffer = self._create_search_buffer() - self.layout = self._create_layout() + self.layout = self._create_layout( + append_autosuggestion_class=append_autosuggestion_class + ) self.app = self._create_application(editing_mode, erase_when_done) def _dyncond(self, attr_name: str) -> Condition: @@ -533,7 +540,7 @@ def accept(buff: Buffer) -> bool: def _create_search_buffer(self) -> Buffer: return Buffer(name=SEARCH_BUFFER) - def _create_layout(self) -> Layout: + def _create_layout(self, append_autosuggestion_class) -> Layout: """ Create `Layout` for this prompt. """ @@ -559,7 +566,7 @@ def display_placeholder() -> bool: HighlightIncrementalSearchProcessor(), HighlightSelectionProcessor(), ConditionalProcessor( - AppendAutoSuggestion(), has_focus(default_buffer) & ~is_done + append_autosuggestion_class(), has_focus(default_buffer) & ~is_done ), ConditionalProcessor(PasswordProcessor(), dyncond("is_password")), DisplayMultipleCursors(),