auto_match.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """
  2. Utilities function for keybinding with prompt toolkit.
  3. This will be bound to specific key press and filter modes,
  4. like whether we are in edit mode, and whether the completer is open.
  5. """
  6. import re
  7. from prompt_toolkit.key_binding import KeyPressEvent
  8. def parenthesis(event: KeyPressEvent):
  9. """Auto-close parenthesis"""
  10. event.current_buffer.insert_text("()")
  11. event.current_buffer.cursor_left()
  12. def brackets(event: KeyPressEvent):
  13. """Auto-close brackets"""
  14. event.current_buffer.insert_text("[]")
  15. event.current_buffer.cursor_left()
  16. def braces(event: KeyPressEvent):
  17. """Auto-close braces"""
  18. event.current_buffer.insert_text("{}")
  19. event.current_buffer.cursor_left()
  20. def double_quote(event: KeyPressEvent):
  21. """Auto-close double quotes"""
  22. event.current_buffer.insert_text('""')
  23. event.current_buffer.cursor_left()
  24. def single_quote(event: KeyPressEvent):
  25. """Auto-close single quotes"""
  26. event.current_buffer.insert_text("''")
  27. event.current_buffer.cursor_left()
  28. def docstring_double_quotes(event: KeyPressEvent):
  29. """Auto-close docstring (double quotes)"""
  30. event.current_buffer.insert_text('""""')
  31. event.current_buffer.cursor_left(3)
  32. def docstring_single_quotes(event: KeyPressEvent):
  33. """Auto-close docstring (single quotes)"""
  34. event.current_buffer.insert_text("''''")
  35. event.current_buffer.cursor_left(3)
  36. def raw_string_parenthesis(event: KeyPressEvent):
  37. """Auto-close parenthesis in raw strings"""
  38. matches = re.match(
  39. r".*(r|R)[\"'](-*)",
  40. event.current_buffer.document.current_line_before_cursor,
  41. )
  42. dashes = matches.group(2) if matches else ""
  43. event.current_buffer.insert_text("()" + dashes)
  44. event.current_buffer.cursor_left(len(dashes) + 1)
  45. def raw_string_bracket(event: KeyPressEvent):
  46. """Auto-close bracker in raw strings"""
  47. matches = re.match(
  48. r".*(r|R)[\"'](-*)",
  49. event.current_buffer.document.current_line_before_cursor,
  50. )
  51. dashes = matches.group(2) if matches else ""
  52. event.current_buffer.insert_text("[]" + dashes)
  53. event.current_buffer.cursor_left(len(dashes) + 1)
  54. def raw_string_braces(event: KeyPressEvent):
  55. """Auto-close braces 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. def skip_over(event: KeyPressEvent):
  64. """Skip over automatically added parenthesis/quote.
  65. (rather than adding another parenthesis/quote)"""
  66. event.current_buffer.cursor_right()
  67. def delete_pair(event: KeyPressEvent):
  68. """Delete auto-closed parenthesis"""
  69. event.current_buffer.delete()
  70. event.current_buffer.delete_before_cursor()
  71. auto_match_parens = {"(": parenthesis, "[": brackets, "{": braces}
  72. auto_match_parens_raw_string = {
  73. "(": raw_string_parenthesis,
  74. "[": raw_string_bracket,
  75. "{": raw_string_braces,
  76. }