Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix autosuggestions in multi-line mode, vi command mode delay #13991

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions IPython/terminal/shortcuts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ def create_identifier(handler: Callable):
# 2) prompt-toolkit checks if we are at the end of text, not end of line
# hence it does not work in multi-line mode of navigable provider
Binding(
auto_suggest.accept_in_vi_insert_mode,
auto_suggest.accept_or_jump_to_end,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user configured shortcuts, their custom configuration will no longer work after this change. This is a twofold issue:

  • if they overridden (modified/removed) this shortcut by matching by the old command name (accept_in_vi_insert_mode), the override will no longer work as it will not find a match (accept_in_vi_insert_mode is no longer here)
  • if they added a new shortcut using the old command name, it will not work either as accept_in_vi_insert_mode command is not exposed anymore (currently we only allow commands which are in existing bindings; we could have a separate register of additional allowed commands which would alleviate this problem)

This is relatively minor breakage given that shortcuts were just introduced, and the name of this command was in a way misleading (it does not check for vi insert mode), so I don't have a strong opinion here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, though if we don't emit a deprecation warning people will use the old name for ever.

Let's try to break, and if we have complaints I can do a patch release.

["end"],
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
krassowski marked this conversation as resolved.
Show resolved Hide resolved
),
Binding(
auto_suggest.accept_in_vi_insert_mode,
auto_suggest.accept_or_jump_to_end,
["c-e"],
"has_suggestion & default_buffer_focused & emacs_like_insert_mode",
),
Expand Down
16 changes: 15 additions & 1 deletion IPython/terminal/shortcuts/auto_suggest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tokenize
from io import StringIO
from typing import Callable, List, Optional, Union, Generator, Tuple
import warnings

from prompt_toolkit.buffer import Buffer
from prompt_toolkit.key_binding import KeyPressEvent
Expand Down Expand Up @@ -192,7 +193,7 @@ def accept_or_jump_to_end(event: KeyPressEvent):
nc.end_of_line(event)


def accept_in_vi_insert_mode(event: KeyPressEvent):
def _deprected_accept_in_vi_insert_mode(event: KeyPressEvent):
"""Accept autosuggestion or jump to end of line.

.. deprecated:: 8.12
Expand Down Expand Up @@ -381,3 +382,16 @@ def swap_autosuggestion_down(event: KeyPressEvent):
provider=provider,
direction_method=provider.down,
)


def __getattr__(key):
if key == "accept_in_vi_insert_mode":
warnings.warn(
"`accept_in_vi_insert_mode` is deprecated since IPython 8.12 and "
"renamed to `accept_or_jump_to_end`. Please update your configuration "
"accordingly",
DeprecationWarning,
stacklevel=2,
)
return _deprected_accept_in_vi_insert_mode
raise AttributeError
11 changes: 9 additions & 2 deletions IPython/terminal/tests/test_shortcuts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from IPython.terminal.shortcuts.auto_suggest import (
accept,
accept_in_vi_insert_mode,
accept_or_jump_to_end,
accept_token,
accept_character,
accept_word,
Expand All @@ -22,6 +22,13 @@
from unittest.mock import patch, Mock


def test_deprected():
import IPython.terminal.shortcuts.auto_suggest as iptsa

with pytest.warns(DeprecationWarning, match=r"8\.12.+accept_or_jump_to_end"):
iptsa.accept_in_vi_insert_mode


def make_event(text, cursor, suggestion):
event = Mock()
event.current_buffer = Mock()
Expand Down Expand Up @@ -80,7 +87,7 @@ def test_autosuggest_at_EOL(text, cursor, suggestion, called):

event = make_event(text, cursor, suggestion)
event.current_buffer.insert_text = Mock()
accept_in_vi_insert_mode(event)
accept_or_jump_to_end(event)
if called:
event.current_buffer.insert_text.assert_called()
else:
Expand Down