Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/terminal/shortcuts/auto_match.py: 37%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

46 statements  

1""" 

2Utilities function for keybinding with prompt toolkit. 

3 

4This will be bound to specific key press and filter modes, 

5like whether we are in edit mode, and whether the completer is open. 

6""" 

7 

8import re 

9from prompt_toolkit.key_binding import KeyPressEvent 

10 

11 

12def parenthesis(event: KeyPressEvent): 

13 """Auto-close parenthesis""" 

14 event.current_buffer.insert_text("()") 

15 event.current_buffer.cursor_left() 

16 

17 

18def brackets(event: KeyPressEvent): 

19 """Auto-close brackets""" 

20 event.current_buffer.insert_text("[]") 

21 event.current_buffer.cursor_left() 

22 

23 

24def braces(event: KeyPressEvent): 

25 """Auto-close braces""" 

26 event.current_buffer.insert_text("{}") 

27 event.current_buffer.cursor_left() 

28 

29 

30def double_quote(event: KeyPressEvent): 

31 """Auto-close double quotes""" 

32 event.current_buffer.insert_text('""') 

33 event.current_buffer.cursor_left() 

34 

35 

36def single_quote(event: KeyPressEvent): 

37 """Auto-close single quotes""" 

38 event.current_buffer.insert_text("''") 

39 event.current_buffer.cursor_left() 

40 

41 

42def 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 

48def 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 

54def 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 

65def 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 

76def 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 

87def 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 

94def delete_pair(event: KeyPressEvent): 

95 """Delete auto-closed parenthesis""" 

96 event.current_buffer.delete() 

97 event.current_buffer.delete_before_cursor() 

98 

99 

100auto_match_parens = {"(": parenthesis, "[": brackets, "{": braces} 

101auto_match_parens_raw_string = { 

102 "(": raw_string_parenthesis, 

103 "[": raw_string_bracket, 

104 "{": raw_string_braces, 

105}