Show More
@@ -140,6 +140,18 b' def create_ipython_shortcuts(shell):' | |||
|
140 | 140 | _following_text_cache[pattern] = condition |
|
141 | 141 | return condition |
|
142 | 142 | |
|
143 | @Condition | |
|
144 | def not_inside_unclosed_string(): | |
|
145 | app = get_app() | |
|
146 | s = app.current_buffer.document.text_before_cursor | |
|
147 | # remove escaped quotes | |
|
148 | s = s.replace('\\"', "").replace("\\'", "") | |
|
149 | # remove triple-quoted string literals | |
|
150 | s = re.sub(r"(?:\"\"\"[\s\S]*\"\"\"|'''[\s\S]*''')", "", s) | |
|
151 | # remove single-quoted string literals | |
|
152 | s = re.sub(r"""(?:"[^"]*["\n]|'[^']*['\n])""", "", s) | |
|
153 | return not ('"' in s or "'" in s) | |
|
154 | ||
|
143 | 155 | # auto match |
|
144 | 156 | @kb.add("(", filter=focused_insert & auto_match & following_text(r"[,)}\]]|$")) |
|
145 | 157 | def _(event): |
@@ -160,7 +172,7 b' def create_ipython_shortcuts(shell):' | |||
|
160 | 172 | '"', |
|
161 | 173 | filter=focused_insert |
|
162 | 174 | & auto_match |
|
163 | & preceding_text(r'^([^"]+|"[^"]*")*$') | |
|
175 | & not_inside_unclosed_string | |
|
164 | 176 | & following_text(r"[,)}\]]|$"), |
|
165 | 177 | ) |
|
166 | 178 | def _(event): |
@@ -171,13 +183,35 b' def create_ipython_shortcuts(shell):' | |||
|
171 | 183 | "'", |
|
172 | 184 | filter=focused_insert |
|
173 | 185 | & auto_match |
|
174 | & preceding_text(r"^([^']+|'[^']*')*$") | |
|
186 | & not_inside_unclosed_string | |
|
175 | 187 | & following_text(r"[,)}\]]|$"), |
|
176 | 188 | ) |
|
177 | 189 | def _(event): |
|
178 | 190 | event.current_buffer.insert_text("''") |
|
179 | 191 | event.current_buffer.cursor_left() |
|
180 | 192 | |
|
193 | @kb.add( | |
|
194 | '"', | |
|
195 | filter=focused_insert | |
|
196 | & auto_match | |
|
197 | & not_inside_unclosed_string | |
|
198 | & preceding_text(r'^.*""$'), | |
|
199 | ) | |
|
200 | def _(event): | |
|
201 | event.current_buffer.insert_text('""""') | |
|
202 | event.current_buffer.cursor_left(3) | |
|
203 | ||
|
204 | @kb.add( | |
|
205 | "'", | |
|
206 | filter=focused_insert | |
|
207 | & auto_match | |
|
208 | & not_inside_unclosed_string | |
|
209 | & preceding_text(r"^.*''$"), | |
|
210 | ) | |
|
211 | def _(event): | |
|
212 | event.current_buffer.insert_text("''''") | |
|
213 | event.current_buffer.cursor_left(3) | |
|
214 | ||
|
181 | 215 | # raw string |
|
182 | 216 | @kb.add( |
|
183 | 217 | "(", filter=focused_insert & auto_match & preceding_text(r".*(r|R)[\"'](-*)$") |
General Comments 0
You need to be logged in to leave comments.
Login now