##// END OF EJS Templates
Add pass-through filter for shortcuts
krassowski -
Show More
@@ -279,7 +279,8 b' AUTO_SUGGEST_BINDINGS = ['
279 279 ["right"],
280 280 "is_cursor_at_the_end_of_line"
281 281 " & default_buffer_focused"
282 " & emacs_like_insert_mode",
282 " & emacs_like_insert_mode"
283 " & pass_through",
283 284 ),
284 285 ]
285 286
@@ -20,6 +20,8 b' from prompt_toolkit.layout.processors import ('
20 20 from IPython.core.getipython import get_ipython
21 21 from IPython.utils.tokenutil import generate_tokens
22 22
23 from .filters import pass_through
24
23 25
24 26 def _get_query(document: Document):
25 27 return document.lines[document.cursor_position_row]
@@ -267,7 +269,10 b' def backspace_and_resume_hint(event: KeyPressEvent):'
267 269
268 270 def resume_hinting(event: KeyPressEvent):
269 271 """Resume autosuggestions"""
270 return _update_hint(event.current_buffer)
272 pass_through.reply(event)
273 # Order matters: if update happened first and event reply second, the
274 # suggestion would be auto-accepted if both actions are bound to same key.
275 _update_hint(event.current_buffer)
271 276
272 277
273 278 def up_and_update_hint(event: KeyPressEvent):
@@ -13,7 +13,8 b' from typing import Callable, Dict, Union'
13 13
14 14 from prompt_toolkit.application.current import get_app
15 15 from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER
16 from prompt_toolkit.filters import Condition, emacs_insert_mode, has_completions
16 from prompt_toolkit.key_binding import KeyPressEvent
17 from prompt_toolkit.filters import Condition, Filter, emacs_insert_mode, has_completions
17 18 from prompt_toolkit.filters import has_focus as has_focus_impl
18 19 from prompt_toolkit.filters import (
19 20 Always,
@@ -175,6 +176,36 b' def is_windows_os():'
175 176 return sys.platform == "win32"
176 177
177 178
179 class PassThrough(Filter):
180 """A filter allowing to implement pass-through behaviour of keybindings.
181
182 Prompt toolkit key processor dispatches only one event per binding match,
183 which means that adding a new shortcut will suppress the old shortcut
184 if the keybindings are the same (unless one is filtered out).
185
186 To stop a shortcut binding from suppressing other shortcuts:
187 - add the `pass_through` filter to list of filter, and
188 - call `pass_through.reply(event)` in the shortcut handler.
189 """
190
191 def __init__(self):
192 self._is_replying = False
193
194 def reply(self, event: KeyPressEvent):
195 self._is_replying = True
196 try:
197 event.key_processor.reset()
198 event.key_processor.feed_multiple(event.key_sequence)
199 event.key_processor.process_keys()
200 finally:
201 self._is_replying = False
202
203 def __call__(self):
204 return not self._is_replying
205
206
207 pass_through = PassThrough()
208
178 209 # these one is callable and re-used multiple times hence needs to be
179 210 # only defined once beforhand so that transforming back to human-readable
180 211 # names works well in the documentation.
@@ -248,6 +279,7 b' KEYBINDING_FILTERS = {'
248 279 "followed_by_single_quote": following_text("^'"),
249 280 "navigable_suggestions": navigable_suggestions,
250 281 "cursor_in_leading_ws": cursor_in_leading_ws,
282 "pass_through": pass_through,
251 283 }
252 284
253 285
General Comments 0
You need to be logged in to leave comments. Login now