Skip to content

Commit 79bb681

Browse files
Use load_key_bindings instead of KeyBindingsManager. (For the latest prompt_toolkit.)
1 parent 45c37bb commit 79bb681

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

ptpython/history_browser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from prompt_toolkit.document import Document
1313
from prompt_toolkit.enums import DEFAULT_BUFFER
1414
from prompt_toolkit.filters import Condition, HasFocus, InFocusStack
15-
from prompt_toolkit.key_binding.manager import KeyBindingManager
15+
from prompt_toolkit.key_binding.defaults import load_key_bindings
1616
from prompt_toolkit.keys import Keys
1717
from prompt_toolkit.layout.containers import HSplit, VSplit, Window, FloatContainer, Float, ConditionalContainer, Container, ScrollOffsets
1818
from prompt_toolkit.layout.controls import BufferControl, FillControl
@@ -420,10 +420,10 @@ def create_key_bindings(python_input, history_mapping):
420420
"""
421421
Key bindings.
422422
"""
423-
manager = KeyBindingManager(
423+
registry = load_key_bindings(
424424
enable_search=True,
425425
enable_extra_page_navigation=True)
426-
handle = manager.registry.add_binding
426+
handle = registry.add_binding
427427

428428
@handle(' ', filter=HasFocus(HISTORY_BUFFER))
429429
def _(event):
@@ -517,7 +517,7 @@ def _(event):
517517
" Suspend to background. "
518518
event.cli.suspend_to_background()
519519

520-
return manager.registry
520+
return registry
521521

522522

523523
def create_history_application(python_input, original_document):

ptpython/key_bindings.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from prompt_toolkit.enums import DEFAULT_BUFFER
55
from prompt_toolkit.filters import HasSelection, IsMultiline, Filter, HasFocus, Condition, ViInsertMode, EmacsInsertMode
66
from prompt_toolkit.key_binding.vi_state import InputMode
7+
from prompt_toolkit.key_binding.registry import Registry
78
from prompt_toolkit.keys import Keys
89

910
__all__ = (
@@ -29,12 +30,14 @@ def __call__(self, cli):
2930
return bool(b.text and (not before_cursor or before_cursor.isspace()))
3031

3132

32-
def load_python_bindings(key_bindings_manager, python_input):
33+
def load_python_bindings(python_input):
3334
"""
3435
Custom key bindings.
3536
"""
37+
registry = Registry()
38+
3639
sidebar_visible = Condition(lambda cli: python_input.show_sidebar)
37-
handle = key_bindings_manager.registry.add_binding
40+
handle = registry.add_binding
3841
has_selection = HasSelection()
3942
vi_mode_enabled = Condition(lambda cli: python_input.vi_mode)
4043

@@ -128,12 +131,16 @@ def _(event):
128131
"""
129132
python_input.show_exit_confirmation = True
130133

134+
return registry
135+
131136

132-
def load_sidebar_bindings(key_bindings_manager, python_input):
137+
def load_sidebar_bindings(python_input):
133138
"""
134139
Load bindings for the navigation in the sidebar.
135140
"""
136-
handle = key_bindings_manager.registry.add_binding
141+
registry = Registry()
142+
143+
handle = registry.add_binding
137144
sidebar_visible = Condition(lambda cli: python_input.show_sidebar)
138145

139146
@handle(Keys.Up, filter=sidebar_visible)
@@ -176,12 +183,16 @@ def _(event):
176183
" Hide sidebar. "
177184
python_input.show_sidebar = False
178185

186+
return registry
179187

180-
def load_confirm_exit_bindings(key_bindings_manager, python_input):
188+
189+
def load_confirm_exit_bindings(python_input):
181190
"""
182191
Handle yes/no key presses when the exit confirmation is shown.
183192
"""
184-
handle = key_bindings_manager.registry.add_binding
193+
registry = Registry()
194+
195+
handle = registry.add_binding
185196
confirmation_visible = Condition(lambda cli: python_input.show_exit_confirmation)
186197

187198
@handle('y', filter=confirmation_visible)
@@ -200,6 +211,8 @@ def _(event):
200211
"""
201212
python_input.show_exit_confirmation = False
202213

214+
return registry
215+
203216

204217
def auto_newline(buffer):
205218
r"""

ptpython/layout.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def get_continuation_prompt(cli, width):
290290
show_numbers=Condition(lambda cli: python_input.show_line_numbers))
291291

292292

293-
def status_bar(key_bindings_manager, python_input):
293+
def status_bar(python_input):
294294
"""
295295
Create the `Layout` for the status bar.
296296
"""
@@ -474,7 +474,7 @@ def extra_condition(cli):
474474
filter=visible)
475475

476476

477-
def create_layout(python_input, key_bindings_manager,
477+
def create_layout(python_input,
478478
lexer=PythonLexer,
479479
extra_body=None, extra_toolbars=None,
480480
extra_buffer_processors=None, input_buffer_height=None):
@@ -592,7 +592,7 @@ def menu_position(cli):
592592
]),
593593
] + extra_toolbars + [
594594
VSplit([
595-
status_bar(key_bindings_manager, python_input),
595+
status_bar(python_input),
596596
show_sidebar_button_info(python_input),
597597
])
598598
])

ptpython/python_input.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
from prompt_toolkit.filters import Condition, Always
1818
from prompt_toolkit.history import FileHistory, InMemoryHistory
1919
from prompt_toolkit.interface import CommandLineInterface, Application, AcceptAction
20-
from prompt_toolkit.key_binding.manager import KeyBindingManager
20+
from prompt_toolkit.key_binding.defaults import load_key_bindings_for_prompt, load_mouse_bindings
2121
from prompt_toolkit.key_binding.vi_state import InputMode
22+
from prompt_toolkit.key_binding.registry import MergedRegistry, ConditionalRegistry
2223
from prompt_toolkit.layout.lexers import PygmentsLexer
2324
from prompt_toolkit.shortcuts import create_output
2425
from prompt_toolkit.styles import DynamicStyle
@@ -226,22 +227,25 @@ def __init__(self,
226227
# Code signatures. (This is set asynchronously after a timeout.)
227228
self.signatures = []
228229

229-
# Use a KeyBindingManager for loading the key bindings.
230-
self.key_bindings_manager = KeyBindingManager(
231-
enable_abort_and_exit_bindings=True,
232-
enable_search=True,
233-
enable_vi_mode=Condition(lambda cli: self.vi_mode),
234-
enable_open_in_editor=Condition(lambda cli: self.enable_open_in_editor),
235-
enable_system_bindings=Condition(lambda cli: self.enable_system_bindings),
236-
enable_auto_suggest_bindings=Condition(lambda cli: self.enable_auto_suggest),
237-
238-
# Disable all default key bindings when the sidebar or the exit confirmation
239-
# are shown.
240-
enable_all=Condition(lambda cli: not (self.show_sidebar or self.show_exit_confirmation)))
241-
242-
load_python_bindings(self.key_bindings_manager, self)
243-
load_sidebar_bindings(self.key_bindings_manager, self)
244-
load_confirm_exit_bindings(self.key_bindings_manager, self)
230+
# Create a Registry for the key bindings.
231+
self.key_bindings_registry = MergedRegistry([
232+
ConditionalRegistry(
233+
registry=load_key_bindings_for_prompt(
234+
enable_abort_and_exit_bindings=True,
235+
enable_search=True,
236+
enable_open_in_editor=Condition(lambda cli: self.enable_open_in_editor),
237+
enable_system_bindings=Condition(lambda cli: self.enable_system_bindings),
238+
enable_auto_suggest_bindings=Condition(lambda cli: self.enable_auto_suggest)),
239+
240+
# Disable all default key bindings when the sidebar or the exit confirmation
241+
# are shown.
242+
filter=Condition(lambda cli: not (self.show_sidebar or self.show_exit_confirmation))
243+
),
244+
load_mouse_bindings(),
245+
load_python_bindings(self),
246+
load_sidebar_bindings(self),
247+
load_confirm_exit_bindings(self),
248+
])
245249

246250
# Boolean indicating whether we have a signatures thread running.
247251
# (Never run more than one at the same time.)
@@ -276,10 +280,6 @@ def get_compiler_flags(self):
276280

277281
return flags
278282

279-
@property
280-
def key_bindings_registry(self):
281-
return self.key_bindings_manager.registry
282-
283283
@property
284284
def add_key_binding(self):
285285
"""
@@ -505,7 +505,6 @@ def create_application(self):
505505
return Application(
506506
layout=create_layout(
507507
self,
508-
self.key_bindings_manager,
509508
lexer=self._lexer,
510509
input_buffer_height=self._input_buffer_height,
511510
extra_buffer_processors=self._extra_buffer_processors,
@@ -635,7 +634,6 @@ def run():
635634
cli.eventloop.run_in_executor(run)
636635

637636
def on_reset(self, cli):
638-
self.key_bindings_manager.reset(cli)
639637
self.signatures = []
640638

641639
def enter_history(self, cli):

ptpython/repl.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def _process_document(self, cli, buffer):
8585
# Append to history and reset.
8686
cli.search_state.text = ''
8787
cli.buffers[DEFAULT_BUFFER].reset(append_to_history=True)
88-
self.key_bindings_manager.reset(cli)
8988

9089
def _execute(self, cli, line):
9190
"""

0 commit comments

Comments
 (0)