From 6dc655898df34ad424dfc467a8b276fdf31bd791 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 5 Jun 2024 06:45:15 +0200 Subject: Merging upstream version 3.0.46. Signed-off-by: Daniel Baumann --- .../key_binding/bindings/auto_suggest.py | 1 + src/prompt_toolkit/key_binding/bindings/basic.py | 18 ++++---- .../key_binding/bindings/completion.py | 1 + src/prompt_toolkit/key_binding/bindings/emacs.py | 16 ++++--- .../key_binding/bindings/named_commands.py | 3 +- .../key_binding/bindings/open_in_editor.py | 1 + .../key_binding/bindings/page_navigation.py | 1 + src/prompt_toolkit/key_binding/bindings/scroll.py | 1 + src/prompt_toolkit/key_binding/bindings/search.py | 1 + src/prompt_toolkit/key_binding/bindings/vi.py | 51 +++++++++++++--------- src/prompt_toolkit/key_binding/defaults.py | 1 + src/prompt_toolkit/key_binding/digraphs.py | 1 + src/prompt_toolkit/key_binding/key_bindings.py | 13 +++--- src/prompt_toolkit/key_binding/key_processor.py | 7 +-- 14 files changed, 69 insertions(+), 47 deletions(-) (limited to 'src/prompt_toolkit/key_binding') diff --git a/src/prompt_toolkit/key_binding/bindings/auto_suggest.py b/src/prompt_toolkit/key_binding/bindings/auto_suggest.py index 3d8a843..b487f14 100644 --- a/src/prompt_toolkit/key_binding/bindings/auto_suggest.py +++ b/src/prompt_toolkit/key_binding/bindings/auto_suggest.py @@ -1,6 +1,7 @@ """ Key bindings for auto suggestion (for fish-style auto suggestion). """ + from __future__ import annotations import re diff --git a/src/prompt_toolkit/key_binding/bindings/basic.py b/src/prompt_toolkit/key_binding/bindings/basic.py index 084548d..ad18df9 100644 --- a/src/prompt_toolkit/key_binding/bindings/basic.py +++ b/src/prompt_toolkit/key_binding/bindings/basic.py @@ -29,6 +29,16 @@ def if_no_repeat(event: E) -> bool: return not event.is_repeat +@Condition +def has_text_before_cursor() -> bool: + return bool(get_app().current_buffer.text) + + +@Condition +def in_quoted_insert() -> bool: + return get_app().quoted_insert + + def load_basic_bindings() -> KeyBindings: key_bindings = KeyBindings() insert_mode = vi_insert_mode | emacs_insert_mode @@ -171,10 +181,6 @@ def load_basic_bindings() -> KeyBindings: # CTRL keys. - @Condition - def has_text_before_cursor() -> bool: - return bool(get_app().current_buffer.text) - handle("c-d", filter=has_text_before_cursor & insert_mode)( get_by_name("delete-char") ) @@ -240,10 +246,6 @@ def load_basic_bindings() -> KeyBindings: event.current_buffer.insert_text(data) - @Condition - def in_quoted_insert() -> bool: - return get_app().quoted_insert - @handle(Keys.Any, filter=in_quoted_insert, eager=True) def _insert_text(event: E) -> None: """ diff --git a/src/prompt_toolkit/key_binding/bindings/completion.py b/src/prompt_toolkit/key_binding/bindings/completion.py index 016821f..8c5e005 100644 --- a/src/prompt_toolkit/key_binding/bindings/completion.py +++ b/src/prompt_toolkit/key_binding/bindings/completion.py @@ -1,6 +1,7 @@ """ Key binding handlers for displaying completions. """ + from __future__ import annotations import asyncio diff --git a/src/prompt_toolkit/key_binding/bindings/emacs.py b/src/prompt_toolkit/key_binding/bindings/emacs.py index 80a66fd..207afba 100644 --- a/src/prompt_toolkit/key_binding/bindings/emacs.py +++ b/src/prompt_toolkit/key_binding/bindings/emacs.py @@ -33,6 +33,16 @@ __all__ = [ E = KeyPressEvent +@Condition +def is_returnable() -> bool: + return get_app().current_buffer.is_returnable + + +@Condition +def is_arg() -> bool: + return get_app().key_processor.arg == "-" + + def load_emacs_bindings() -> KeyBindingsBase: """ Some e-macs extensions. @@ -134,7 +144,7 @@ def load_emacs_bindings() -> KeyBindingsBase: if event._arg is None: event.append_to_arg_count("-") - @handle("-", filter=Condition(lambda: get_app().key_processor.arg == "-")) + @handle("-", filter=is_arg) def _dash(event: E) -> None: """ When '-' is typed again, after exactly '-' has been given as an @@ -142,10 +152,6 @@ def load_emacs_bindings() -> KeyBindingsBase: """ event.app.key_processor.arg = "-" - @Condition - def is_returnable() -> bool: - return get_app().current_buffer.is_returnable - # Meta + Enter: always accept input. handle("escape", "enter", filter=insert_mode & is_returnable)( get_by_name("accept-line") diff --git a/src/prompt_toolkit/key_binding/bindings/named_commands.py b/src/prompt_toolkit/key_binding/bindings/named_commands.py index d836116..8ea8dd0 100644 --- a/src/prompt_toolkit/key_binding/bindings/named_commands.py +++ b/src/prompt_toolkit/key_binding/bindings/named_commands.py @@ -3,6 +3,7 @@ Key bindings which are also known by GNU Readline by the given names. See: http://www.delorie.com/gnu/docs/readline/rlman_13.html """ + from __future__ import annotations from typing import Callable, TypeVar, Union, cast @@ -58,7 +59,7 @@ def get_by_name(name: str) -> Binding: try: return _readline_commands[name] except KeyError as e: - raise KeyError("Unknown Readline command: %r" % name) from e + raise KeyError(f"Unknown Readline command: {name!r}") from e # diff --git a/src/prompt_toolkit/key_binding/bindings/open_in_editor.py b/src/prompt_toolkit/key_binding/bindings/open_in_editor.py index d156424..26b8685 100644 --- a/src/prompt_toolkit/key_binding/bindings/open_in_editor.py +++ b/src/prompt_toolkit/key_binding/bindings/open_in_editor.py @@ -1,6 +1,7 @@ """ Open in editor key bindings. """ + from __future__ import annotations from prompt_toolkit.filters import emacs_mode, has_selection, vi_navigation_mode diff --git a/src/prompt_toolkit/key_binding/bindings/page_navigation.py b/src/prompt_toolkit/key_binding/bindings/page_navigation.py index 3918e14..c490425 100644 --- a/src/prompt_toolkit/key_binding/bindings/page_navigation.py +++ b/src/prompt_toolkit/key_binding/bindings/page_navigation.py @@ -2,6 +2,7 @@ Key bindings for extra page navigation: bindings for up/down scrolling through long pages, like in Emacs or Vi. """ + from __future__ import annotations from prompt_toolkit.filters import buffer_has_focus, emacs_mode, vi_mode diff --git a/src/prompt_toolkit/key_binding/bindings/scroll.py b/src/prompt_toolkit/key_binding/bindings/scroll.py index 83a4be1..13e44ed 100644 --- a/src/prompt_toolkit/key_binding/bindings/scroll.py +++ b/src/prompt_toolkit/key_binding/bindings/scroll.py @@ -5,6 +5,7 @@ This are separate bindings, because GNU readline doesn't have them, but they are very useful for navigating through long multiline buffers, like in Vi, Emacs, etc... """ + from __future__ import annotations from prompt_toolkit.key_binding.key_processor import KeyPressEvent diff --git a/src/prompt_toolkit/key_binding/bindings/search.py b/src/prompt_toolkit/key_binding/bindings/search.py index ba5e117..a57c52e 100644 --- a/src/prompt_toolkit/key_binding/bindings/search.py +++ b/src/prompt_toolkit/key_binding/bindings/search.py @@ -1,6 +1,7 @@ """ Search related key bindings. """ + from __future__ import annotations from prompt_toolkit import search diff --git a/src/prompt_toolkit/key_binding/bindings/vi.py b/src/prompt_toolkit/key_binding/bindings/vi.py index 5cc74b4..d68a31f 100644 --- a/src/prompt_toolkit/key_binding/bindings/vi.py +++ b/src/prompt_toolkit/key_binding/bindings/vi.py @@ -371,6 +371,35 @@ def create_operator_decorator( return operator_decorator +@Condition +def is_returnable() -> bool: + return get_app().current_buffer.is_returnable + + +@Condition +def in_block_selection() -> bool: + buff = get_app().current_buffer + return bool( + buff.selection_state and buff.selection_state.type == SelectionType.BLOCK + ) + + +@Condition +def digraph_symbol_1_given() -> bool: + return get_app().vi_state.digraph_symbol1 is not None + + +@Condition +def search_buffer_is_empty() -> bool: + "Returns True when the search buffer is empty." + return get_app().current_buffer.text == "" + + +@Condition +def tilde_operator() -> bool: + return get_app().vi_state.tilde_operator + + def load_vi_bindings() -> KeyBindingsBase: """ Vi extensions. @@ -410,7 +439,7 @@ def load_vi_bindings() -> KeyBindingsBase: (("g", "~"), Always(), lambda string: string.swapcase()), ( ("~",), - Condition(lambda: get_app().vi_state.tilde_operator), + tilde_operator, lambda string: string.swapcase(), ), ] @@ -528,10 +557,6 @@ def load_vi_bindings() -> KeyBindingsBase: """ event.current_buffer.cancel_completion() - @Condition - def is_returnable() -> bool: - return get_app().current_buffer.is_returnable - # In navigation mode, pressing enter will always return the input. handle("enter", filter=vi_navigation_mode & is_returnable)( get_by_name("accept-line") @@ -681,13 +706,6 @@ def load_vi_bindings() -> KeyBindingsBase: ) ) - @Condition - def in_block_selection() -> bool: - buff = get_app().current_buffer - return bool( - buff.selection_state and buff.selection_state.type == SelectionType.BLOCK - ) - @handle("I", filter=in_block_selection & ~is_read_only) def insert_in_block_selection(event: E, after: bool = False) -> None: """ @@ -2071,10 +2089,6 @@ def load_vi_bindings() -> KeyBindingsBase: """ event.app.vi_state.waiting_for_digraph = True - @Condition - def digraph_symbol_1_given() -> bool: - return get_app().vi_state.digraph_symbol1 is not None - @handle(Keys.Any, filter=vi_digraph_mode & ~digraph_symbol_1_given) def _digraph1(event: E) -> None: """ @@ -2180,11 +2194,6 @@ def load_vi_search_bindings() -> KeyBindingsBase: handle = key_bindings.add from . import search - @Condition - def search_buffer_is_empty() -> bool: - "Returns True when the search buffer is empty." - return get_app().current_buffer.text == "" - # Vi-style forward search. handle( "/", diff --git a/src/prompt_toolkit/key_binding/defaults.py b/src/prompt_toolkit/key_binding/defaults.py index 166da8d..6c26571 100644 --- a/src/prompt_toolkit/key_binding/defaults.py +++ b/src/prompt_toolkit/key_binding/defaults.py @@ -4,6 +4,7 @@ Default key bindings.:: key_bindings = load_key_bindings() app = Application(key_bindings=key_bindings) """ + from __future__ import annotations from prompt_toolkit.filters import buffer_has_focus diff --git a/src/prompt_toolkit/key_binding/digraphs.py b/src/prompt_toolkit/key_binding/digraphs.py index 1e8a432..f0152dc 100644 --- a/src/prompt_toolkit/key_binding/digraphs.py +++ b/src/prompt_toolkit/key_binding/digraphs.py @@ -6,6 +6,7 @@ pressing Control-K followed by to normal characters. Taken from Neovim and translated to Python: https://raw.githubusercontent.com/neovim/neovim/master/src/nvim/digraph.c """ + from __future__ import annotations __all__ = [ diff --git a/src/prompt_toolkit/key_binding/key_bindings.py b/src/prompt_toolkit/key_binding/key_bindings.py index 62530f2..854da80 100644 --- a/src/prompt_toolkit/key_binding/key_bindings.py +++ b/src/prompt_toolkit/key_binding/key_bindings.py @@ -34,6 +34,7 @@ been assigned, through the `key_binding` decorator.:: # Later, add it to the key bindings. kb.add(Keys.A, my_key_binding) """ + from __future__ import annotations from abc import ABCMeta, abstractmethod, abstractproperty @@ -140,10 +141,8 @@ class Binding: event.app.invalidate() def __repr__(self) -> str: - return "{}(keys={!r}, handler={!r})".format( - self.__class__.__name__, - self.keys, - self.handler, + return ( + f"{self.__class__.__name__}(keys={self.keys!r}, handler={self.handler!r})" ) @@ -226,9 +225,9 @@ class KeyBindings(KeyBindingsBase): def __init__(self) -> None: self._bindings: list[Binding] = [] - self._get_bindings_for_keys_cache: SimpleCache[ - KeysTuple, list[Binding] - ] = SimpleCache(maxsize=10000) + self._get_bindings_for_keys_cache: SimpleCache[KeysTuple, list[Binding]] = ( + SimpleCache(maxsize=10000) + ) self._get_bindings_starting_with_keys_cache: SimpleCache[ KeysTuple, list[Binding] ] = SimpleCache(maxsize=1000) diff --git a/src/prompt_toolkit/key_binding/key_processor.py b/src/prompt_toolkit/key_binding/key_processor.py index 4c4f0d1..e2070a1 100644 --- a/src/prompt_toolkit/key_binding/key_processor.py +++ b/src/prompt_toolkit/key_binding/key_processor.py @@ -5,6 +5,7 @@ the input in the :class:`~prompt_toolkit.inputstream.InputStream` instance. The `KeyProcessor` will according to the implemented keybindings call the correct callbacks when new key presses are feed through `feed`. """ + from __future__ import annotations import weakref @@ -450,11 +451,7 @@ class KeyPressEvent: self._app = get_app() def __repr__(self) -> str: - return "KeyPressEvent(arg={!r}, key_sequence={!r}, is_repeat={!r})".format( - self.arg, - self.key_sequence, - self.is_repeat, - ) + return f"KeyPressEvent(arg={self.arg!r}, key_sequence={self.key_sequence!r}, is_repeat={self.is_repeat!r})" @property def data(self) -> str: -- cgit v1.2.3