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