-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_bindings.py
84 lines (68 loc) · 2.41 KB
/
key_bindings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from __future__ import unicode_literals
import logging
from prompt_toolkit.enums import EditingMode
from prompt_toolkit.filters import completion_is_selected
from prompt_toolkit.key_binding import KeyBindings
_logger = logging.getLogger(__name__)
def cli_bindings(cli):
"""Custom key bindings for cli."""
kb = KeyBindings()
@kb.add("f3")
def _(event):
"""Enable/Disable Multiline Mode."""
_logger.debug("Detected F3 key.")
cli.multi_line = not cli.multi_line
@kb.add("f4")
def _(event):
"""Toggle between Vi and Emacs mode."""
_logger.debug("Detected F4 key.")
if cli.key_bindings == "vi":
event.app.editing_mode = EditingMode.EMACS
cli.key_bindings = "emacs"
else:
event.app.editing_mode = EditingMode.VI
cli.key_bindings = "vi"
@kb.add("tab")
def _(event):
"""Force autocompletion at cursor."""
_logger.debug("Detected <Tab> key.")
b = event.app.current_buffer
if b.complete_state:
b.complete_next()
else:
b.start_completion(select_first=True)
@kb.add("s-tab")
def _(event):
"""Force autocompletion at cursor."""
_logger.debug("Detected <Tab> key.")
b = event.app.current_buffer
if b.complete_state:
b.complete_previous()
else:
b.start_completion(select_last=True)
@kb.add("c-space")
def _(event):
"""
Initialize autocompletion at cursor.
If the autocompletion menu is not showing, display it with the
appropriate completions for the context.
If the menu is showing, select the next completion.
"""
_logger.debug("Detected <C-Space> key.")
b = event.app.current_buffer
if b.complete_state:
b.complete_next()
else:
b.start_completion(select_first=False)
@kb.add("enter", filter=completion_is_selected)
def _(event):
"""Makes the enter key work as the tab key only when showing the menu.
In other words, don't execute query when enter is pressed in
the completion dropdown menu, instead close the dropdown menu
(accept current selection).
"""
_logger.debug("Detected enter key.")
event.current_buffer.complete_state = None
b = event.app.current_buffer
b.complete_state = None
return kb