]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118878: Pyrepl: show completions menu below the current line (#118939)
authorDaniel Hollas <daniel.hollas@bristol.ac.uk>
Tue, 21 Jan 2025 21:06:13 +0000 (21:06 +0000)
committerGitHub <noreply@github.com>
Tue, 21 Jan 2025 21:06:13 +0000 (21:06 +0000)
Co-authored-by: Pablo Galindo Salgado <Pablogsal@gmail.com>
Lib/_pyrepl/commands.py
Lib/_pyrepl/completing_reader.py
Lib/test/test_pyrepl/test_pyrepl.py
Lib/test/test_pyrepl/test_reader.py
Misc/NEWS.d/next/Library/2024-07-14-23-19-20.gh-issue-119257.9OEzcN.rst [new file with mode: 0644]

index 285841ca5e5b1c76ac52ce995d0494869569525d..503ca1da329eaaef56f831fd5a56f7814b67e874 100644 (file)
@@ -282,7 +282,7 @@ class down(MotionCommand):
             x, y = r.pos2xy()
             new_y = y + 1
 
-            if new_y > r.max_row():
+            if r.eol() == len(b):
                 if r.historyi < len(r.history):
                     r.select_item(r.historyi + 1)
                     r.pos = r.eol(0)
@@ -309,7 +309,7 @@ class down(MotionCommand):
 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
@@ -321,7 +321,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
index e856bb9807c7f6bd25b9973d2a75b4d95d692445..1cd4b6367ca8b153e439d0424770aa506c7d5ebe 100644 (file)
@@ -260,10 +260,15 @@ class CompletingReader(Reader):
     def calc_screen(self) -> list[str]:
         screen = super().calc_screen()
         if self.cmpltn_menu_visible:
-            ly = self.lxy[1]
+            # We display the completions menu below the current prompt
+            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)
+            # 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.
+            # This is a hack to prevent the cursor jumping
+            # into the completions menu when pressing left or down arrow.
+            if self.pos != len(self.buffer):
+                self.screeninfo[ly:ly] = [(0, [])]*len(self.cmpltn_menu)
         return screen
 
     def finish(self) -> None:
index 77e1faeabff3b29dcc56aafc4a848d59f9dec7ec..00dc990d946a9bdf91adbcdff624652f932b5ce6 100644 (file)
@@ -850,7 +850,7 @@ class TestPyReplCompleter(TestCase):
         output = multiline_input(reader, namespace)
         self.assertEqual(output, "python")
 
-    def test_updown_arrow_with_completion_menu(self):
+    def test_up_down_arrow_with_completion_menu(self):
         """Up arrow in the middle of unfinished tab completion when the menu is displayed
         should work and trigger going back in history. Down arrow should subsequently
         get us back to the incomplete command."""
@@ -860,6 +860,7 @@ class TestPyReplCompleter(TestCase):
         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")),
             ],
index 6c72a1d39c55df6f5f38803980ed620bf1b991c4..863ecc61ddd432df0e11cc9ad8b17c05c8f19704 100644 (file)
@@ -295,8 +295,8 @@ class TestReader(TestCase):
 
         actual = reader.screen
         self.assertEqual(len(actual), 2)
-        self.assertEqual(actual[0].rstrip(), "itertools.accumulate(")
-        self.assertEqual(actual[1], f"{code}a")
+        self.assertEqual(actual[0], f"{code}a")
+        self.assertEqual(actual[1].rstrip(), "itertools.accumulate(")
 
     def test_key_press_on_tab_press_once(self):
         namespace = {"itertools": itertools}
diff --git a/Misc/NEWS.d/next/Library/2024-07-14-23-19-20.gh-issue-119257.9OEzcN.rst b/Misc/NEWS.d/next/Library/2024-07-14-23-19-20.gh-issue-119257.9OEzcN.rst
new file mode 100644 (file)
index 0000000..8f3f863
--- /dev/null
@@ -0,0 +1,2 @@
+Show tab completions menu below the current line, which results in less
+janky behaviour, and fixes a cursor movement bug. Patch by Daniel Hollas