From 33a45993391bc55cf1f69ddea9b649e17f3c63bc 2021-12-26 11:05:33 From: Martin Skarzynski Date: 2021-12-26 11:05:33 Subject: [PATCH] Add setting to toggle auto_match feature --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 5c79a17..98e5eb8 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -188,6 +188,15 @@ class TerminalInteractiveShell(InteractiveShell): allow_none=True ).tag(config=True) + auto_match = Bool( + False, + help=""" + Automatically add/delete closing bracket or quote when opening bracket or quote is entered/deleted. + Brackets: (), [], {} + Quotes: '', \"\" + """, + ).tag(config=True) + mouse_support = Bool(False, help="Enable mouse support in the prompt\n(Note: prevents selecting text with the mouse)" ).tag(config=True) diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py index 360a393..6aab3d2 100644 --- a/IPython/terminal/shortcuts.py +++ b/IPython/terminal/shortcuts.py @@ -85,6 +85,10 @@ def create_ipython_shortcuts(shell): kb.add('f2', filter=has_focus(DEFAULT_BUFFER))(open_input_in_editor) + @Condition + def auto_match(): + return shell.auto_match + focused_insert = (vi_insert_mode | emacs_insert_mode) & has_focus(DEFAULT_BUFFER) _preceding_text_cache = {} _following_text_cache = {} @@ -120,27 +124,27 @@ def create_ipython_shortcuts(shell): return condition # auto match - @kb.add("(", filter=focused_insert & following_text(r"[,)}\]]|$")) + @kb.add("(", filter=focused_insert & auto_match & 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 & auto_match & 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 & auto_match & 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 & auto_match & 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 & auto_match & following_text(r"[,)}\]]|$")) def _(event): event.current_buffer.insert_text("''") event.current_buffer.cursor_left() @@ -187,33 +191,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("^'")) + @kb.add(")", filter=focused_insert & auto_match & following_text(r"^\)")) + @kb.add("]", filter=focused_insert & auto_match & following_text(r"^\]")) + @kb.add("}", filter=focused_insert & auto_match & following_text(r"^\}")) + @kb.add('"', filter=focused_insert & auto_match & following_text('^"')) + @kb.add("'", filter=focused_insert & auto_match & following_text("^'")) def _(event): event.current_buffer.cursor_right() @kb.add( "backspace", - filter=focused_insert & preceding_text(r".*\($") & following_text(r"^\)"), + filter=focused_insert + & preceding_text(r".*\($") + & auto_match + & following_text(r"^\)"), ) @kb.add( "backspace", - filter=focused_insert & preceding_text(r".*\[$") & following_text(r"^\]"), + filter=focused_insert + & preceding_text(r".*\[$") + & auto_match + & following_text(r"^\]"), ) @kb.add( "backspace", - filter=focused_insert & preceding_text(r".*\{$") & following_text(r"^\}"), + filter=focused_insert + & preceding_text(r".*\{$") + & auto_match + & following_text(r"^\}"), ) @kb.add( "backspace", - filter=focused_insert & preceding_text('.*"$') & following_text('^"'), + filter=focused_insert + & preceding_text('.*"$') + & auto_match + & following_text('^"'), ) @kb.add( "backspace", - filter=focused_insert & preceding_text(r".*'$") & following_text(r"^'"), + filter=focused_insert + & preceding_text(r".*'$") + & auto_match + & following_text(r"^'"), ) def _(event): event.current_buffer.delete()