Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94da54f

Browse files
committed
Expose auto_suggest.resume_hinting, fix resume on backspace
1 parent 4db24c3 commit 94da54f

4 files changed

Lines changed: 22 additions & 11 deletions

File tree

IPython/terminal/interactiveshell.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
from .prompts import Prompts, ClassicPrompts, RichPromptDisplayHook
5252
from .ptutils import IPythonPTCompleter, IPythonPTLexer
5353
from .shortcuts import (
54+
KEY_BINDINGS,
5455
create_ipython_shortcuts,
5556
create_identifier,
5657
RuntimeBinding,
@@ -491,8 +492,8 @@ def _merge_shortcuts(self, user_shortcuts):
491492
# for now we only allow adding shortcuts for commands which are already
492493
# registered; this is a security precaution.
493494
known_commands = {
494-
create_identifier(binding.handler): binding.handler
495-
for binding in key_bindings.bindings
495+
create_identifier(binding.command): binding.command
496+
for binding in KEY_BINDINGS
496497
}
497498
shortcuts_to_skip = []
498499
shortcuts_to_add = []
@@ -513,11 +514,11 @@ def _merge_shortcuts(self, user_shortcuts):
513514
)
514515
matching = [
515516
binding
516-
for binding in key_bindings.bindings
517+
for binding in KEY_BINDINGS
517518
if (
518519
(old_filter is None or binding.filter == old_filter)
519520
and (old_keys is None or [k for k in binding.keys] == old_keys)
520-
and create_identifier(binding.handler) == command_id
521+
and create_identifier(binding.command) == command_id
521522
)
522523
]
523524

@@ -542,7 +543,7 @@ def _merge_shortcuts(self, user_shortcuts):
542543
}
543544
if len(matching) == 0:
544545
raise ValueError(
545-
f"No shortcuts matching {specification} found in {key_bindings.bindings}"
546+
f"No shortcuts matching {specification} found in {KEY_BINDINGS}"
546547
)
547548
elif len(matching) > 1:
548549
raise ValueError(

IPython/terminal/shortcuts/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ def create_identifier(handler: Callable):
253253
["backspace"],
254254
"has_suggestion & default_buffer_focused",
255255
),
256+
Binding(
257+
auto_suggest.resume_hinting,
258+
["right"],
259+
# For now this binding is inactive (the filter includes `never`).
260+
# TODO: remove `never` if we reach a consensus in #13991
261+
# TODO: use `emacs_like_insert_mode` once #13991 is in
262+
"never & default_buffer_focused & ((vi_insert_mode & ebivim) | emacs_insert_mode)",
263+
),
256264
]
257265

258266

IPython/terminal/shortcuts/auto_suggest.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,13 @@ def _update_hint(buffer: Buffer):
252252

253253
def backspace_and_resume_hint(event: KeyPressEvent):
254254
"""Resume autosuggestions after deleting last character"""
255-
current_buffer = event.current_buffer
255+
nc.backward_delete_char(event)
256+
_update_hint(event.current_buffer)
256257

257-
def resume_hinting(buffer: Buffer):
258-
_update_hint(buffer)
259-
current_buffer.on_text_changed.remove_handler(resume_hinting)
260258

261-
current_buffer.on_text_changed.add_handler(resume_hinting)
262-
nc.backward_delete_char(event)
259+
def resume_hinting(event: KeyPressEvent):
260+
"""Resume autosuggestions"""
261+
return _update_hint(event.current_buffer)
263262

264263

265264
def up_and_update_hint(event: KeyPressEvent):

IPython/terminal/shortcuts/filters.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from prompt_toolkit.filters import has_focus as has_focus_impl
1818
from prompt_toolkit.filters import (
1919
Always,
20+
Never,
2021
has_selection,
2122
has_suggestion,
2223
vi_insert_mode,
@@ -174,6 +175,8 @@ def is_windows_os():
174175

175176
KEYBINDING_FILTERS = {
176177
"always": Always(),
178+
# never is used for exposing commands which have no default keybindings
179+
"never": Never(),
177180
"has_line_below": has_line_below,
178181
"has_line_above": has_line_above,
179182
"has_selection": has_selection,

0 commit comments

Comments
 (0)