Show More
@@ -1,90 +1,96 b'' | |||||
|
1 | """ | |||
|
2 | Utilities function for keybinding with prompt toolkit. | |||
|
3 | ||||
|
4 | This will be bound to specific key press and filter modes, | |||
|
5 | like whether we are in edit mode, and whether the completer is open. | |||
|
6 | """ | |||
1 | import re |
|
7 | import re | |
2 | from prompt_toolkit.key_binding import KeyPressEvent |
|
8 | from prompt_toolkit.key_binding import KeyPressEvent | |
3 |
|
9 | |||
4 |
|
10 | |||
5 | def parenthesis(event: KeyPressEvent): |
|
11 | def parenthesis(event: KeyPressEvent): | |
6 | """Auto-close parenthesis""" |
|
12 | """Auto-close parenthesis""" | |
7 | event.current_buffer.insert_text("()") |
|
13 | event.current_buffer.insert_text("()") | |
8 | event.current_buffer.cursor_left() |
|
14 | event.current_buffer.cursor_left() | |
9 |
|
15 | |||
10 |
|
16 | |||
11 | def brackets(event: KeyPressEvent): |
|
17 | def brackets(event: KeyPressEvent): | |
12 | """Auto-close brackets""" |
|
18 | """Auto-close brackets""" | |
13 | event.current_buffer.insert_text("[]") |
|
19 | event.current_buffer.insert_text("[]") | |
14 | event.current_buffer.cursor_left() |
|
20 | event.current_buffer.cursor_left() | |
15 |
|
21 | |||
16 |
|
22 | |||
17 | def braces(event: KeyPressEvent): |
|
23 | def braces(event: KeyPressEvent): | |
18 | """Auto-close braces""" |
|
24 | """Auto-close braces""" | |
19 | event.current_buffer.insert_text("{}") |
|
25 | event.current_buffer.insert_text("{}") | |
20 | event.current_buffer.cursor_left() |
|
26 | event.current_buffer.cursor_left() | |
21 |
|
27 | |||
22 |
|
28 | |||
23 | def double_quote(event: KeyPressEvent): |
|
29 | def double_quote(event: KeyPressEvent): | |
24 | """Auto-close double quotes""" |
|
30 | """Auto-close double quotes""" | |
25 | event.current_buffer.insert_text('""') |
|
31 | event.current_buffer.insert_text('""') | |
26 | event.current_buffer.cursor_left() |
|
32 | event.current_buffer.cursor_left() | |
27 |
|
33 | |||
28 |
|
34 | |||
29 | def single_quote(event: KeyPressEvent): |
|
35 | def single_quote(event: KeyPressEvent): | |
30 | """Auto-close single quotes""" |
|
36 | """Auto-close single quotes""" | |
31 | event.current_buffer.insert_text("''") |
|
37 | event.current_buffer.insert_text("''") | |
32 | event.current_buffer.cursor_left() |
|
38 | event.current_buffer.cursor_left() | |
33 |
|
39 | |||
34 |
|
40 | |||
35 | def docstring_double_quotes(event: KeyPressEvent): |
|
41 | def docstring_double_quotes(event: KeyPressEvent): | |
36 | """Auto-close docstring (double quotes)""" |
|
42 | """Auto-close docstring (double quotes)""" | |
37 | event.current_buffer.insert_text('""""') |
|
43 | event.current_buffer.insert_text('""""') | |
38 | event.current_buffer.cursor_left(3) |
|
44 | event.current_buffer.cursor_left(3) | |
39 |
|
45 | |||
40 |
|
46 | |||
41 | def docstring_single_quotes(event: KeyPressEvent): |
|
47 | def docstring_single_quotes(event: KeyPressEvent): | |
42 | """Auto-close docstring (single quotes)""" |
|
48 | """Auto-close docstring (single quotes)""" | |
43 | event.current_buffer.insert_text("''''") |
|
49 | event.current_buffer.insert_text("''''") | |
44 | event.current_buffer.cursor_left(3) |
|
50 | event.current_buffer.cursor_left(3) | |
45 |
|
51 | |||
46 |
|
52 | |||
47 | def raw_string_parenthesis(event: KeyPressEvent): |
|
53 | def raw_string_parenthesis(event: KeyPressEvent): | |
48 | """Auto-close parenthesis in raw strings""" |
|
54 | """Auto-close parenthesis in raw strings""" | |
49 | matches = re.match( |
|
55 | matches = re.match( | |
50 | r".*(r|R)[\"'](-*)", |
|
56 | r".*(r|R)[\"'](-*)", | |
51 | event.current_buffer.document.current_line_before_cursor, |
|
57 | event.current_buffer.document.current_line_before_cursor, | |
52 | ) |
|
58 | ) | |
53 | dashes = matches.group(2) if matches else "" |
|
59 | dashes = matches.group(2) if matches else "" | |
54 | event.current_buffer.insert_text("()" + dashes) |
|
60 | event.current_buffer.insert_text("()" + dashes) | |
55 | event.current_buffer.cursor_left(len(dashes) + 1) |
|
61 | event.current_buffer.cursor_left(len(dashes) + 1) | |
56 |
|
62 | |||
57 |
|
63 | |||
58 | def raw_string_bracket(event: KeyPressEvent): |
|
64 | def raw_string_bracket(event: KeyPressEvent): | |
59 | """Auto-close bracker in raw strings""" |
|
65 | """Auto-close bracker in raw strings""" | |
60 | matches = re.match( |
|
66 | matches = re.match( | |
61 | r".*(r|R)[\"'](-*)", |
|
67 | r".*(r|R)[\"'](-*)", | |
62 | event.current_buffer.document.current_line_before_cursor, |
|
68 | event.current_buffer.document.current_line_before_cursor, | |
63 | ) |
|
69 | ) | |
64 | dashes = matches.group(2) if matches else "" |
|
70 | dashes = matches.group(2) if matches else "" | |
65 | event.current_buffer.insert_text("[]" + dashes) |
|
71 | event.current_buffer.insert_text("[]" + dashes) | |
66 | event.current_buffer.cursor_left(len(dashes) + 1) |
|
72 | event.current_buffer.cursor_left(len(dashes) + 1) | |
67 |
|
73 | |||
68 |
|
74 | |||
69 | def raw_string_braces(event: KeyPressEvent): |
|
75 | def raw_string_braces(event: KeyPressEvent): | |
70 | """Auto-close braces in raw strings""" |
|
76 | """Auto-close braces in raw strings""" | |
71 | matches = re.match( |
|
77 | matches = re.match( | |
72 | r".*(r|R)[\"'](-*)", |
|
78 | r".*(r|R)[\"'](-*)", | |
73 | event.current_buffer.document.current_line_before_cursor, |
|
79 | event.current_buffer.document.current_line_before_cursor, | |
74 | ) |
|
80 | ) | |
75 | dashes = matches.group(2) if matches else "" |
|
81 | dashes = matches.group(2) if matches else "" | |
76 | event.current_buffer.insert_text("{}" + dashes) |
|
82 | event.current_buffer.insert_text("{}" + dashes) | |
77 | event.current_buffer.cursor_left(len(dashes) + 1) |
|
83 | event.current_buffer.cursor_left(len(dashes) + 1) | |
78 |
|
84 | |||
79 |
|
85 | |||
80 | def skip_over(event: KeyPressEvent): |
|
86 | def skip_over(event: KeyPressEvent): | |
81 | """Skip over automatically added parenthesis. |
|
87 | """Skip over automatically added parenthesis. | |
82 |
|
88 | |||
83 | (rather than adding another parenthesis)""" |
|
89 | (rather than adding another parenthesis)""" | |
84 | event.current_buffer.cursor_right() |
|
90 | event.current_buffer.cursor_right() | |
85 |
|
91 | |||
86 |
|
92 | |||
87 | def delete_pair(event: KeyPressEvent): |
|
93 | def delete_pair(event: KeyPressEvent): | |
88 | """Delete auto-closed parenthesis""" |
|
94 | """Delete auto-closed parenthesis""" | |
89 | event.current_buffer.delete() |
|
95 | event.current_buffer.delete() | |
90 | event.current_buffer.delete_before_cursor() |
|
96 | event.current_buffer.delete_before_cursor() |
General Comments 0
You need to be logged in to leave comments.
Login now