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