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.
This commit is contained in:
Paul Gauthier 2023-07-16 14:12:01 -03:00
parent 668b8841e2
commit 6244df8352

View file

@ -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: