diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py index 2c9190e..360a393 100644 --- a/IPython/terminal/shortcuts.py +++ b/IPython/terminal/shortcuts.py @@ -89,7 +89,6 @@ def create_ipython_shortcuts(shell): _preceding_text_cache = {} _following_text_cache = {} - def preceding_text(pattern): try: return _preceding_text_cache[pattern] @@ -105,7 +104,6 @@ def create_ipython_shortcuts(shell): _preceding_text_cache[pattern] = condition return condition - def following_text(pattern): try: return _following_text_cache[pattern] @@ -121,19 +119,18 @@ def create_ipython_shortcuts(shell): _following_text_cache[pattern] = condition return condition - # auto match - @kb.add('(', filter=focused_insert & following_text(r"[,)}\]]|$")) + @kb.add("(", filter=focused_insert & following_text(r"[,)}\]]|$")) def _(event): event.current_buffer.insert_text("()") event.current_buffer.cursor_left() - @kb.add('[', filter=focused_insert & following_text(r"[,)}\]]|$")) + @kb.add("[", filter=focused_insert & following_text(r"[,)}\]]|$")) def _(event): event.current_buffer.insert_text("[]") event.current_buffer.cursor_left() - @kb.add('{', filter=focused_insert & following_text(r"[,)}\]]|$")) + @kb.add("{", filter=focused_insert & following_text(r"[,)}\]]|$")) def _(event): event.current_buffer.insert_text("{}") event.current_buffer.cursor_left() @@ -149,23 +146,32 @@ def create_ipython_shortcuts(shell): event.current_buffer.cursor_left() # raw string - @kb.add('(', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) + @kb.add("(", filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) def _(event): - matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) + matches = re.match( + r".*(r|R)[\"'](-*)", + event.current_buffer.document.current_line_before_cursor, + ) dashes = matches.group(2) or "" event.current_buffer.insert_text("()" + dashes) event.current_buffer.cursor_left(len(dashes) + 1) - @kb.add('[', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) + @kb.add("[", filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) def _(event): - matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) + matches = re.match( + r".*(r|R)[\"'](-*)", + event.current_buffer.document.current_line_before_cursor, + ) dashes = matches.group(2) or "" event.current_buffer.insert_text("[]" + dashes) event.current_buffer.cursor_left(len(dashes) + 1) - @kb.add('{', filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) + @kb.add("{", filter=focused_insert & preceding_text(r".*(r|R)[\"'](-*)$")) def _(event): - matches = re.match(r".*(r|R)[\"'](-*)", event.current_buffer.document.current_line_before_cursor) + matches = re.match( + r".*(r|R)[\"'](-*)", + event.current_buffer.document.current_line_before_cursor, + ) dashes = matches.group(2) or "" event.current_buffer.insert_text("{}" + dashes) event.current_buffer.cursor_left(len(dashes) + 1) @@ -181,30 +187,48 @@ def create_ipython_shortcuts(shell): event.current_buffer.cursor_left() # just move cursor - @kb.add(')', filter=focused_insert & following_text(r"^\)")) - @kb.add(']', filter=focused_insert & following_text(r"^\]")) - @kb.add('}', filter=focused_insert & following_text(r"^\}")) - @kb.add('"', filter=focused_insert & following_text("^\"")) + @kb.add(")", filter=focused_insert & following_text(r"^\)")) + @kb.add("]", filter=focused_insert & following_text(r"^\]")) + @kb.add("}", filter=focused_insert & following_text(r"^\}")) + @kb.add('"', filter=focused_insert & following_text('^"')) @kb.add("'", filter=focused_insert & following_text("^'")) def _(event): event.current_buffer.cursor_right() - @kb.add('backspace', filter=focused_insert & preceding_text(r".*\($") & following_text(r"^\)")) - @kb.add('backspace', filter=focused_insert & preceding_text(r".*\[$") & following_text(r"^\]")) - @kb.add('backspace', filter=focused_insert & preceding_text(r".*\{$") & following_text(r"^\}")) - @kb.add('backspace', filter=focused_insert & preceding_text('.*"$') & following_text('^"')) - @kb.add('backspace', filter=focused_insert & preceding_text(r".*'$") & following_text(r"^'")) + @kb.add( + "backspace", + filter=focused_insert & preceding_text(r".*\($") & following_text(r"^\)"), + ) + @kb.add( + "backspace", + filter=focused_insert & preceding_text(r".*\[$") & following_text(r"^\]"), + ) + @kb.add( + "backspace", + filter=focused_insert & preceding_text(r".*\{$") & following_text(r"^\}"), + ) + @kb.add( + "backspace", + filter=focused_insert & preceding_text('.*"$') & following_text('^"'), + ) + @kb.add( + "backspace", + filter=focused_insert & preceding_text(r".*'$") & following_text(r"^'"), + ) def _(event): event.current_buffer.delete() event.current_buffer.delete_before_cursor() - - if shell.display_completions == 'readlinelike': - kb.add('c-i', filter=(has_focus(DEFAULT_BUFFER) - & ~has_selection - & insert_mode - & ~cursor_in_leading_ws - ))(display_completions_like_readline) + if shell.display_completions == "readlinelike": + kb.add( + "c-i", + filter=( + has_focus(DEFAULT_BUFFER) + & ~has_selection + & insert_mode + & ~cursor_in_leading_ws + ), + )(display_completions_like_readline) if sys.platform == "win32": kb.add("c-v", filter=(has_focus(DEFAULT_BUFFER) & ~vi_mode))(win_paste)