##// END OF EJS Templates
Fix autosuggestions in multi-line mode, vi command mode delay
krassowski -
Show More
@@ -181,30 +181,45 b' AUTO_MATCH_BINDINGS = ['
181 181 ]
182 182
183 183 AUTO_SUGGEST_BINDINGS = [
184 # there are two reasons for re-defining bindings defined upstream:
185 # 1) prompt-toolkit does not execute autosuggestion bindings in vi mode,
186 # 2) prompt-toolkit checks if we are at the end of text, not end of line
187 # hence it does not work in multi-line mode of navigable provider
184 188 Binding(
185 189 auto_suggest.accept_in_vi_insert_mode,
186 190 ["end"],
187 "default_buffer_focused & (ebivim | ~vi_insert_mode)",
191 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
188 192 ),
189 193 Binding(
190 194 auto_suggest.accept_in_vi_insert_mode,
191 195 ["c-e"],
192 "vi_insert_mode & default_buffer_focused & ebivim",
196 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
197 ),
198 Binding(
199 auto_suggest.accept,
200 ["c-f"],
201 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
202 ),
203 Binding(
204 auto_suggest.accept,
205 ["right"],
206 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
193 207 ),
194 Binding(auto_suggest.accept, ["c-f"], "vi_insert_mode & default_buffer_focused"),
195 208 Binding(
196 209 auto_suggest.accept_word,
197 210 ["escape", "f"],
198 "vi_insert_mode & default_buffer_focused & ebivim",
211 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
199 212 ),
200 213 Binding(
201 214 auto_suggest.accept_token,
202 215 ["c-right"],
203 "has_suggestion & default_buffer_focused",
216 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
204 217 ),
205 218 Binding(
206 219 auto_suggest.discard,
207 220 ["escape"],
221 # note this one is using `emacs_insert_mode`, not `emacs_like_insert_mode`
222 # as in `vi_insert_mode` we do not want `escape` to be shadowed (ever).
208 223 "has_suggestion & default_buffer_focused & emacs_insert_mode",
209 224 ),
210 225 Binding(
@@ -236,22 +251,22 b' AUTO_SUGGEST_BINDINGS = ['
236 251 Binding(
237 252 auto_suggest.accept_character,
238 253 ["escape", "right"],
239 "has_suggestion & default_buffer_focused",
254 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
240 255 ),
241 256 Binding(
242 257 auto_suggest.accept_and_move_cursor_left,
243 258 ["c-left"],
244 "has_suggestion & default_buffer_focused",
259 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
245 260 ),
246 261 Binding(
247 262 auto_suggest.accept_and_keep_cursor,
248 263 ["c-down"],
249 "has_suggestion & default_buffer_focused",
264 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
250 265 ),
251 266 Binding(
252 267 auto_suggest.backspace_and_resume_hint,
253 268 ["backspace"],
254 "has_suggestion & default_buffer_focused",
269 "has_suggestion & default_buffer_focused & emacs_like_insert_mode",
255 270 ),
256 271 ]
257 272
@@ -178,9 +178,8 b' class NavigableAutoSuggestFromHistory(AutoSuggestFromHistory):'
178 178 break
179 179
180 180
181 # Needed for to accept autosuggestions in vi insert mode
182 def accept_in_vi_insert_mode(event: KeyPressEvent):
183 """Apply autosuggestion if at end of line."""
181 def accept_or_jump_to_end(event: KeyPressEvent):
182 """Apply autosuggestion or jump to end of line."""
184 183 buffer = event.current_buffer
185 184 d = buffer.document
186 185 after_cursor = d.text[d.cursor_position :]
@@ -193,6 +192,15 b' def accept_in_vi_insert_mode(event: KeyPressEvent):'
193 192 nc.end_of_line(event)
194 193
195 194
195 def accept_in_vi_insert_mode(event: KeyPressEvent):
196 """Accept autosuggestion or jump to end of line.
197
198 .. deprecated:: 8.12
199 Use `accept_or_jump_to_end` instead.
200 """
201 return accept_or_jump_to_end(event)
202
203
196 204 def accept(event: KeyPressEvent):
197 205 """Accept autosuggestion"""
198 206 buffer = event.current_buffer
@@ -181,10 +181,33 b' KEYBINDING_FILTERS = {'
181 181 "vi_mode": vi_mode,
182 182 "vi_insert_mode": vi_insert_mode,
183 183 "emacs_insert_mode": emacs_insert_mode,
184 # https://github.com/ipython/ipython/pull/12603 argued for inclusion of
185 # emacs key bindings with a configurable `emacs_bindings_in_vi_insert_mode`
186 # toggle; when the toggle is on user can access keybindigns like `ctrl + e`
187 # in vi insert mode. Because some of the emacs bindings involve `escape`
188 # followed by another key, e.g. `escape` followed by `f`, prompt-toolkit
189 # needs to wait to see if there will be another character typed in before
190 # executing pure `escape` keybinding; in vi insert mode `escape` switches to
191 # command mode which is common and performance critical action for vi users.
192 # To avoid the delay users employ a workaround:
193 # https://github.com/ipython/ipython/issues/13443#issuecomment-1032753703
194 # which involves switching `emacs_bindings_in_vi_insert_mode` off.
195 #
196 # For the workaround to work:
197 # 1) end users need to toggle `emacs_bindings_in_vi_insert_mode` off
198 # 2) all keybindings which would involve `escape` need to respect that
199 # toggle by including either:
200 # - `vi_insert_mode & ebivim` for actions which have emacs keybindings
201 # predefined upstream in prompt-toolkit, or
202 # - `emacs_like_insert_mode` for actions which do not have existing
203 # emacs keybindings predefined upstream (or need overriding of the
204 # upstream bindings to modify behaviour), defined below.
205 "emacs_like_insert_mode": (vi_insert_mode & ebivim) | emacs_insert_mode,
184 206 "has_completions": has_completions,
185 207 "insert_mode": vi_insert_mode | emacs_insert_mode,
186 208 "default_buffer_focused": default_buffer_focused,
187 209 "search_buffer_focused": has_focus(SEARCH_BUFFER),
210 # `ebivim` stands for emacs bindings in vi insert mode
188 211 "ebivim": ebivim,
189 212 "supports_suspend": supports_suspend,
190 213 "is_windows_os": is_windows_os,
General Comments 0
You need to be logged in to leave comments. Login now