Skip to content

Commit 4498c29

Browse files
committed
Add test
1 parent dd90c2e commit 4498c29

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

Lib/_pyrepl/completing_reader.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,13 @@ def after_command(self, cmd: Command) -> None:
258258
def calc_complete_screen(self) -> list[str]:
259259
screen = super().calc_complete_screen()
260260
if self.cmpltn_menu_visible:
261+
# We display the completions menu below the current prompt
261262
ly = self.lxy[1] + 1
262263
screen[ly:ly] = self.cmpltn_menu
263-
# This is a horrible hack. If we're not in the middle
264-
# of multiline edit, don't append to screeninfo
265-
# since that screws up the position calculation
266-
# in pos2xy function.
264+
# If we're not in the middle of multiline edit, don't append to screeninfo
265+
# since that screws up the position calculation in pos2xy function.
266+
# This is a hack to prevent the cursor jumping
267+
# into the completions menu when pressing left or down arrow.
267268
if self.pos != len(self.buffer):
268269
self.screeninfo[ly:ly] = [(0, [])]*len(self.cmpltn_menu)
269270
return screen

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def test_global_namespace_completion(self):
546546
output = multiline_input(reader, namespace)
547547
self.assertEqual(output, "python")
548548

549-
def test_updown_arrow_with_completion_menu(self):
549+
def test_up_down_arrow_with_completion_menu(self):
550550
"""Up arrow in the middle of unfinished tab completion when the menu is displayed
551551
should work and trigger going back in history. Down arrow should subsequently
552552
get us back to the incomplete command."""
@@ -571,6 +571,30 @@ def test_updown_arrow_with_completion_menu(self):
571571
output = multiline_input(reader, namespace)
572572
self.assertEqual(output, "os.")
573573

574+
# TODO: This test doesn't seem to work as intended, it always succeeds
575+
def test_right_down_arrows_with_completion_menu(self):
576+
"""Right / Down arrows while the tab completion menu is displayed
577+
should do nothing"""
578+
code = "os.\t\t"
579+
namespace = {"os": os}
580+
581+
events = itertools.chain(
582+
code_to_events(code),
583+
[
584+
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
585+
Event(evt="key", data="right", raw=bytearray(b"\x1bOC")),
586+
],
587+
code_to_events("\n")
588+
)
589+
reader = self.prepare_reader(events, namespace=namespace)
590+
output = multiline_input(reader, namespace)
591+
self.assertEqual(output, "os.")
592+
# When we press right and/or down arrow while
593+
# the completions menu is displayed,
594+
# the cursor should stay where it was on the line.
595+
self.assertEqual(reader.cmpltn_menu_vis, 1)
596+
self.assertEqual(reader.cxy, (6, 0))
597+
574598
@patch("_pyrepl.readline._ReadlineWrapper.get_reader")
575599
@patch("sys.stderr", new_callable=io.StringIO)
576600
def test_completion_with_warnings(self, mock_stderr, mock_get_reader):

0 commit comments

Comments
 (0)