From 6244df835257f448c9fda37c02ba090ad651b40b Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 16 Jul 2023 14:12:01 -0300 Subject: [PATCH] aider: `get_input` now treats control+enter as a newline with prompt toolkit KeyBindings. # Aider chat conversation: USER: can we have `get_input` treat control+enter as a newline with prompt toolkit KeyBindings ASSISTANT: To implement this feature, we need to modify the `get_input` method in the `InputOutput` class in the `aider/io.py` file. We will use the `KeyBindings` class from `prompt_toolkit.keys` to bind the `Control+Enter` key combination to insert a newline. Here are the changes: ```python aider/io.py <<<<<<< ORIGINAL from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, prompt ======= from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, prompt from prompt_toolkit.keys import Keys from prompt_toolkit.key_binding import KeyBindings >>>>>>> UPDATED ```python aider/io.py <<<<<<< ORIGINAL session = PromptSession(**session_kwargs) ======= kb = KeyBindings() @kb.add(Keys.ControlJ, eager=True) def _(event): event.current_buffer.insert_text('\n') session = PromptSession(key_bindings=kb, **session_kwargs) >>>>>>> UPDATED ``` This code creates a new `KeyBindings` instance and adds a binding for `Control+Enter` (represented by `Keys.ControlJ`) to insert a newline. Then, it passes this key bindings instance to the `PromptSession` constructor. --- aider/io.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/aider/io.py b/aider/io.py index c96f65d9a..43c4eb931 100644 --- a/aider/io.py +++ b/aider/io.py @@ -7,6 +7,8 @@ from prompt_toolkit.completion import Completer, Completion from prompt_toolkit.history import FileHistory from prompt_toolkit.lexers import PygmentsLexer from prompt_toolkit.shortcuts import CompleteStyle, PromptSession, prompt +from prompt_toolkit.keys import Keys +from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.styles import Style from pygments.lexers import MarkdownLexer, guess_lexer_for_filename from pygments.token import Token @@ -203,7 +205,13 @@ class InputOutput: if self.input_history_file is not None: session_kwargs["history"] = FileHistory(self.input_history_file) - session = PromptSession(**session_kwargs) + kb = KeyBindings() + + @kb.add(Keys.ControlJ, eager=True) + def _(event): + event.current_buffer.insert_text('\n') + + session = PromptSession(key_bindings=kb, **session_kwargs) line = session.prompt() if line and line[0] == "{" and not multiline_input: