From 946e545b181fea95e876b15c51fc60a5a89bf934 2022-02-21 20:38:26 From: Lucy McPhail Date: 2022-02-21 20:38:26 Subject: [PATCH] Improve auto_match for quotes Only insert a pair of quotes if there are an even number of quotes preceding the cursor. This way, if the cursor is inside an unclosed string, typing the closing quote will not insert a pair. --- diff --git a/IPython/terminal/shortcuts.py b/IPython/terminal/shortcuts.py index ea204f8..274f9b2 100644 --- a/IPython/terminal/shortcuts.py +++ b/IPython/terminal/shortcuts.py @@ -139,12 +139,24 @@ def create_ipython_shortcuts(shell): event.current_buffer.insert_text("{}") event.current_buffer.cursor_left() - @kb.add('"', filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) + @kb.add( + '"', + filter=focused_insert + & auto_match + & preceding_text(r'^([^"]+|"[^"]*")*$') + & following_text(r"[,)}\]]|$"), + ) def _(event): event.current_buffer.insert_text('""') event.current_buffer.cursor_left() - @kb.add("'", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) + @kb.add( + "'", + filter=focused_insert + & auto_match + & preceding_text(r"^([^']+|'[^']*')*$") + & following_text(r"[,)}\]]|$"), + ) def _(event): event.current_buffer.insert_text("''") event.current_buffer.cursor_left() @@ -186,16 +198,6 @@ def create_ipython_shortcuts(shell): event.current_buffer.insert_text("{}" + dashes) event.current_buffer.cursor_left(len(dashes) + 1) - @kb.add('"', filter=focused_insert & auto_match & preceding_text(r".*(r|R)$")) - def _(event): - event.current_buffer.insert_text('""') - event.current_buffer.cursor_left() - - @kb.add("'", filter=focused_insert & auto_match & preceding_text(r".*(r|R)$")) - def _(event): - event.current_buffer.insert_text("''") - event.current_buffer.cursor_left() - # just move cursor @kb.add(")", filter=focused_insert & auto_match & following_text(r"^\)")) @kb.add("]", filter=focused_insert & auto_match & following_text(r"^\]"))