From 94da54f4e7871fe1aa79189637d3d40e0d0c7e02 2023-03-26 15:38:13 From: krassowski <5832902+krassowski@users.noreply.github.com> Date: 2023-03-26 15:38:13 Subject: [PATCH] Expose `auto_suggest.resume_hinting`, fix resume on backspace --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 96a9687..75cf25e 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -51,6 +51,7 @@ from .pt_inputhooks import get_inputhook_name_and_func from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook from .ptutils import IPythonPTCompleter, IPythonPTLexer from .shortcuts import ( + KEY_BINDINGS, create_ipython_shortcuts, create_identifier, RuntimeBinding, @@ -491,8 +492,8 @@ class TerminalInteractiveShell(InteractiveShell): # for now we only allow adding shortcuts for commands which are already # registered; this is a security precaution. known_commands = { - create_identifier(binding.handler): binding.handler - for binding in key_bindings.bindings + create_identifier(binding.command): binding.command + for binding in KEY_BINDINGS } shortcuts_to_skip = [] shortcuts_to_add = [] @@ -513,11 +514,11 @@ class TerminalInteractiveShell(InteractiveShell): ) matching = [ binding - for binding in key_bindings.bindings + for binding in KEY_BINDINGS if ( (old_filter is None or binding.filter == old_filter) and (old_keys is None or [k for k in binding.keys] == old_keys) - and create_identifier(binding.handler) == command_id + and create_identifier(binding.command) == command_id ) ] @@ -542,7 +543,7 @@ class TerminalInteractiveShell(InteractiveShell): } if len(matching) == 0: raise ValueError( - f"No shortcuts matching {specification} found in {key_bindings.bindings}" + f"No shortcuts matching {specification} found in {KEY_BINDINGS}" ) elif len(matching) > 1: raise ValueError( diff --git a/IPython/terminal/shortcuts/__init__.py b/IPython/terminal/shortcuts/__init__.py index 41533c8..adb8f37 100644 --- a/IPython/terminal/shortcuts/__init__.py +++ b/IPython/terminal/shortcuts/__init__.py @@ -253,6 +253,14 @@ AUTO_SUGGEST_BINDINGS = [ ["backspace"], "has_suggestion & default_buffer_focused", ), + Binding( + auto_suggest.resume_hinting, + ["right"], + # For now this binding is inactive (the filter includes `never`). + # TODO: remove `never` if we reach a consensus in #13991 + # TODO: use `emacs_like_insert_mode` once #13991 is in + "never & default_buffer_focused & ((vi_insert_mode & ebivim) | emacs_insert_mode)", + ), ] diff --git a/IPython/terminal/shortcuts/auto_suggest.py b/IPython/terminal/shortcuts/auto_suggest.py index 0c193d9..2521c64 100644 --- a/IPython/terminal/shortcuts/auto_suggest.py +++ b/IPython/terminal/shortcuts/auto_suggest.py @@ -252,14 +252,13 @@ def _update_hint(buffer: Buffer): def backspace_and_resume_hint(event: KeyPressEvent): """Resume autosuggestions after deleting last character""" - current_buffer = event.current_buffer + nc.backward_delete_char(event) + _update_hint(event.current_buffer) - def resume_hinting(buffer: Buffer): - _update_hint(buffer) - current_buffer.on_text_changed.remove_handler(resume_hinting) - current_buffer.on_text_changed.add_handler(resume_hinting) - nc.backward_delete_char(event) +def resume_hinting(event: KeyPressEvent): + """Resume autosuggestions""" + return _update_hint(event.current_buffer) def up_and_update_hint(event: KeyPressEvent): diff --git a/IPython/terminal/shortcuts/filters.py b/IPython/terminal/shortcuts/filters.py index a4a6213..a1e1441 100644 --- a/IPython/terminal/shortcuts/filters.py +++ b/IPython/terminal/shortcuts/filters.py @@ -17,6 +17,7 @@ from prompt_toolkit.filters import Condition, emacs_insert_mode, has_completions from prompt_toolkit.filters import has_focus as has_focus_impl from prompt_toolkit.filters import ( Always, + Never, has_selection, has_suggestion, vi_insert_mode, @@ -174,6 +175,8 @@ default_buffer_focused = has_focus(DEFAULT_BUFFER) KEYBINDING_FILTERS = { "always": Always(), + # never is used for exposing commands which have no default keybindings + "never": Never(), "has_line_below": has_line_below, "has_line_above": has_line_above, "has_selection": has_selection,