Skip to content

Commit

Permalink
gh-118878: pyrepl: Show completion menu below current line
Browse files Browse the repository at this point in the history
The main advantage of this is that the behaviour is less janky,
since the current line no longer jumps up and down.

This also allows fixing the behaviour of arrow keys when the menu is
displayed.
  • Loading branch information
danielhollas committed May 11, 2024
1 parent dc98b55 commit a1d95c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
9 changes: 6 additions & 3 deletions Lib/_pyrepl/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ def do(self) -> None:
x, y = r.pos2xy()
new_y = y + 1

if new_y > r.max_row():
# bol1 = r.bol()
eol1 = r.eol()
if eol1 == len(b):
#if new_y > r.max_row():
if r.historyi < len(r.history):
r.select_item(r.historyi + 1)
r.pos = r.eol(0)
Expand All @@ -303,7 +306,7 @@ def do(self) -> None:
class left(MotionCommand):
def do(self) -> None:
r = self.reader
for i in range(r.get_arg()):
for _ in range(r.get_arg()):
p = r.pos - 1
if p >= 0:
r.pos = p
Expand All @@ -315,7 +318,7 @@ class right(MotionCommand):
def do(self) -> None:
r = self.reader
b = r.buffer
for i in range(r.get_arg()):
for _ in range(r.get_arg()):
p = r.pos + 1
if p <= len(b):
r.pos = p
Expand Down
12 changes: 8 additions & 4 deletions Lib/_pyrepl/completing_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# types
Command = commands.Command
if False:
from .types import Callback, SimpleContextManager, KeySpec, CommandName
from .types import KeySpec, CommandName


def prefix(wordlist: list[str], j: int = 0) -> str:
Expand Down Expand Up @@ -258,10 +258,14 @@ def after_command(self, cmd: Command) -> None:
def calc_screen(self) -> list[str]:
screen = super().calc_screen()
if self.cmpltn_menu_vis:
ly = self.lxy[1]
ly = self.lxy[1] + 1
screen[ly:ly] = self.cmpltn_menu
self.screeninfo[ly:ly] = [(0, [])]*len(self.cmpltn_menu)
self.cxy = self.cxy[0], self.cxy[1] + len(self.cmpltn_menu)
# This is a horrible hack. If we're not in the middle
# of multiline edit, don't append to screeninfo
# since that screws up the position calculation
# in pos2xy function.
if self.pos != len(self.buffer):
self.screeninfo[ly:ly] = [(0, [])]*len(self.cmpltn_menu)
return screen

def finish(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ def test_updown_arrow_with_completion_menu(self):
events = itertools.chain(
code_to_events(code),
[
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
Event(evt='key', data='up', raw=bytearray(b'\x1bOA')),
Event(evt="key", data="down", raw=bytearray(b"\x1bOB")),
],
Expand Down

0 comments on commit a1d95c9

Please sign in to comment.