Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8be374f

Browse files
committed
Fix autosuggestions in multi-line mode, vi command mode delay
1 parent 4db24c3 commit 8be374f

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

IPython/terminal/shortcuts/__init__.py

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,30 +181,45 @@ def create_identifier(handler: Callable):
181181
]
182182

183183
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
184188
Binding(
185189
auto_suggest.accept_in_vi_insert_mode,
186190
["end"],
187-
"default_buffer_focused & (ebivim | ~vi_insert_mode)",
191+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
188192
),
189193
Binding(
190194
auto_suggest.accept_in_vi_insert_mode,
191195
["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",
193207
),
194-
Binding(auto_suggest.accept, ["c-f"], "vi_insert_mode & default_buffer_focused"),
195208
Binding(
196209
auto_suggest.accept_word,
197210
["escape", "f"],
198-
"vi_insert_mode & default_buffer_focused & ebivim",
211+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
199212
),
200213
Binding(
201214
auto_suggest.accept_token,
202215
["c-right"],
203-
"has_suggestion & default_buffer_focused",
216+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
204217
),
205218
Binding(
206219
auto_suggest.discard,
207220
["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).
208223
"has_suggestion & default_buffer_focused & emacs_insert_mode",
209224
),
210225
Binding(
@@ -236,22 +251,22 @@ def create_identifier(handler: Callable):
236251
Binding(
237252
auto_suggest.accept_character,
238253
["escape", "right"],
239-
"has_suggestion & default_buffer_focused",
254+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
240255
),
241256
Binding(
242257
auto_suggest.accept_and_move_cursor_left,
243258
["c-left"],
244-
"has_suggestion & default_buffer_focused",
259+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
245260
),
246261
Binding(
247262
auto_suggest.accept_and_keep_cursor,
248263
["c-down"],
249-
"has_suggestion & default_buffer_focused",
264+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
250265
),
251266
Binding(
252267
auto_suggest.backspace_and_resume_hint,
253268
["backspace"],
254-
"has_suggestion & default_buffer_focused",
269+
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
255270
),
256271
]
257272

IPython/terminal/shortcuts/auto_suggest.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ def down(self, query: str, other_than: str, history: History) -> None:
178178
break
179179

180180

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."""
184183
buffer = event.current_buffer
185184
d = buffer.document
186185
after_cursor = d.text[d.cursor_position :]
@@ -193,6 +192,15 @@ def accept_in_vi_insert_mode(event: KeyPressEvent):
193192
nc.end_of_line(event)
194193

195194

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+
196204
def accept(event: KeyPressEvent):
197205
"""Accept autosuggestion"""
198206
buffer = event.current_buffer

IPython/terminal/shortcuts/filters.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,33 @@ def is_windows_os():
181181
"vi_mode": vi_mode,
182182
"vi_insert_mode": vi_insert_mode,
183183
"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,
184206
"has_completions": has_completions,
185207
"insert_mode": vi_insert_mode | emacs_insert_mode,
186208
"default_buffer_focused": default_buffer_focused,
187209
"search_buffer_focused": has_focus(SEARCH_BUFFER),
210+
# `ebivim` stands for emacs bindings in vi insert mode
188211
"ebivim": ebivim,
189212
"supports_suspend": supports_suspend,
190213
"is_windows_os": is_windows_os,

0 commit comments

Comments
 (0)