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

45 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 06:09 +0000

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""" 

7import re 

8from prompt_toolkit.key_binding import KeyPressEvent 

9 

10 

11def parenthesis(event: KeyPressEvent): 

12 """Auto-close parenthesis""" 

13 event.current_buffer.insert_text("()") 

14 event.current_buffer.cursor_left() 

15 

16 

17def brackets(event: KeyPressEvent): 

18 """Auto-close brackets""" 

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

20 event.current_buffer.cursor_left() 

21 

22 

23def braces(event: KeyPressEvent): 

24 """Auto-close braces""" 

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

26 event.current_buffer.cursor_left() 

27 

28 

29def double_quote(event: KeyPressEvent): 

30 """Auto-close double quotes""" 

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

32 event.current_buffer.cursor_left() 

33 

34 

35def single_quote(event: KeyPressEvent): 

36 """Auto-close single quotes""" 

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

38 event.current_buffer.cursor_left() 

39 

40 

41def docstring_double_quotes(event: KeyPressEvent): 

42 """Auto-close docstring (double quotes)""" 

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

44 event.current_buffer.cursor_left(3) 

45 

46 

47def docstring_single_quotes(event: KeyPressEvent): 

48 """Auto-close docstring (single quotes)""" 

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

50 event.current_buffer.cursor_left(3) 

51 

52 

53def raw_string_parenthesis(event: KeyPressEvent): 

54 """Auto-close parenthesis in raw strings""" 

55 matches = re.match( 

56 r".*(r|R)[\"'](-*)", 

57 event.current_buffer.document.current_line_before_cursor, 

58 ) 

59 dashes = matches.group(2) if matches else "" 

60 event.current_buffer.insert_text("()" + dashes) 

61 event.current_buffer.cursor_left(len(dashes) + 1) 

62 

63 

64def raw_string_bracket(event: KeyPressEvent): 

65 """Auto-close bracker in raw strings""" 

66 matches = re.match( 

67 r".*(r|R)[\"'](-*)", 

68 event.current_buffer.document.current_line_before_cursor, 

69 ) 

70 dashes = matches.group(2) if matches else "" 

71 event.current_buffer.insert_text("[]" + dashes) 

72 event.current_buffer.cursor_left(len(dashes) + 1) 

73 

74 

75def raw_string_braces(event: KeyPressEvent): 

76 """Auto-close braces in raw strings""" 

77 matches = re.match( 

78 r".*(r|R)[\"'](-*)", 

79 event.current_buffer.document.current_line_before_cursor, 

80 ) 

81 dashes = matches.group(2) if matches else "" 

82 event.current_buffer.insert_text("{}" + dashes) 

83 event.current_buffer.cursor_left(len(dashes) + 1) 

84 

85 

86def skip_over(event: KeyPressEvent): 

87 """Skip over automatically added parenthesis/quote. 

88 

89 (rather than adding another parenthesis/quote)""" 

90 event.current_buffer.cursor_right() 

91 

92 

93def delete_pair(event: KeyPressEvent): 

94 """Delete auto-closed parenthesis""" 

95 event.current_buffer.delete() 

96 event.current_buffer.delete_before_cursor() 

97 

98 

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

100auto_match_parens_raw_string = { 

101 "(": raw_string_parenthesis, 

102 "[": raw_string_bracket, 

103 "{": raw_string_braces, 

104}