Skip to content

Commit de33311

Browse files
committed
Better completion results order
1 parent db6c9bd commit de33311

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

bpython/autocomplete.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,16 @@ def get_completer(
747747
double underscore methods like __len__ in method signatures
748748
"""
749749

750+
def _cmpl_sort(x: str) -> Tuple[Any, ...]:
751+
"""
752+
Function used to sort the matches.
753+
"""
754+
# put parameters above everything in completion
755+
return (
756+
x[-1] != "=",
757+
x,
758+
)
759+
750760
for completer in completers:
751761
try:
752762
matches = completer.matches(
@@ -765,7 +775,9 @@ def get_completer(
765775
)
766776
continue
767777
if matches is not None:
768-
return sorted(matches), (completer if matches else None)
778+
return sorted(matches, key=_cmpl_sort), (
779+
completer if matches else None
780+
)
769781

770782
return [], None
771783

bpython/test/test_autocomplete.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ def test_two_completers_get_both(self):
106106
cumulative = autocomplete.CumulativeCompleter([a, b])
107107
self.assertEqual(cumulative.matches(3, "abc"), {"a", "b"})
108108

109+
def test_order_completer(self):
110+
a = self.completer(["ax", "ab="])
111+
b = self.completer(["aa"])
112+
cumulative = autocomplete.CumulativeCompleter([a, b])
113+
self.assertEqual(
114+
autocomplete.get_completer([cumulative], 1, "a"),
115+
(["ab=", "aa", "ax"], cumulative),
116+
)
117+
109118

110119
class TestFilenameCompletion(unittest.TestCase):
111120
def setUp(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy