diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py index d287368..ad4dc39 100644 --- a/IPython/terminal/shortcuts/__init__.py +++ b/IPython/terminal/shortcuts/__init__.py @@ -343,8 +343,9 @@ def create_ipython_shortcuts(shell, for_all_platforms: bool = False) -> KeyBindi kb.add("c-right", filter=has_suggestion & has_focus(DEFAULT_BUFFER))( auto_suggest.accept_token ) - from functools import partial - + kb.add("escape", filter=has_suggestion & has_focus(DEFAULT_BUFFER), eager=True)( + auto_suggest.discard + ) kb.add("up", filter=has_suggestion & has_focus(DEFAULT_BUFFER))( auto_suggest.swap_autosuggestion_up(shell.auto_suggest) ) diff --git a/IPython/terminal/shortcuts/auto_suggest.py b/IPython/terminal/shortcuts/auto_suggest.py index a8e1d7b..733a46d 100644 --- a/IPython/terminal/shortcuts/auto_suggest.py +++ b/IPython/terminal/shortcuts/auto_suggest.py @@ -167,6 +167,12 @@ def accept(event: KeyPressEvent): nc.forward_char(event) +def discard(event: KeyPressEvent): + """Discard autosuggestion""" + buffer = event.current_buffer + buffer.suggestion = None + + def accept_word(event: KeyPressEvent): """Fill partial autosuggestion by word""" buffer = event.current_buffer diff --git a/IPython/terminal/tests/test_shortcuts.py b/IPython/terminal/tests/test_shortcuts.py index 9f15843..a43a4ba 100644 --- a/IPython/terminal/tests/test_shortcuts.py +++ b/IPython/terminal/tests/test_shortcuts.py @@ -6,6 +6,7 @@ from IPython.terminal.shortcuts.auto_suggest import ( accept_character, accept_word, accept_and_keep_cursor, + discard, NavigableAutoSuggestFromHistory, swap_autosuggestion_up, swap_autosuggestion_down, @@ -49,6 +50,22 @@ def test_accept(text, suggestion, expected): @pytest.mark.parametrize( + "text, suggestion", + [ + ("", "def out(tag: str, n=50):"), + ("def ", "out(tag: str, n=50):"), + ], +) +def test_discard(text, suggestion): + event = make_event(text, len(text), suggestion) + buffer = event.current_buffer + buffer.insert_text = Mock() + discard(event) + assert not buffer.insert_text.called + assert buffer.suggestion is None + + +@pytest.mark.parametrize( "text, cursor, suggestion, called", [ ("123456", 6, "123456789", True),