Skip to content

Commit 9c83806

Browse files
committed
Merge PR #996
2 parents be21521 + 77bed91 commit 9c83806

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

bpython/autocomplete.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,12 @@ def matches(
604604
return matches if matches else None
605605

606606
def locate(self, cursor_offset: int, line: str) -> Optional[LinePart]:
607-
return lineparts.current_word(cursor_offset, line)
607+
r = lineparts.current_word(cursor_offset, line)
608+
if r and r.word[-1] == "(":
609+
# if the word ends with a (, it's the parent word with an empty
610+
# param. Return an empty word
611+
return lineparts.LinePart(r.stop, r.stop, "")
612+
return r
608613

609614

610615
class ExpressionAttributeCompletion(AttrCompletion):
@@ -742,6 +747,16 @@ def get_completer(
742747
double underscore methods like __len__ in method signatures
743748
"""
744749

750+
def _cmpl_sort(x: str) -> Tuple[bool, ...]:
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+
745760
for completer in completers:
746761
try:
747762
matches = completer.matches(
@@ -760,7 +775,9 @@ def get_completer(
760775
)
761776
continue
762777
if matches is not None:
763-
return sorted(matches), (completer if matches else None)
778+
return sorted(matches, key=_cmpl_sort), (
779+
completer if matches else None
780+
)
764781

765782
return [], None
766783

bpython/test/test_autocomplete.py

Lines changed: 13 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):
@@ -435,3 +444,7 @@ def func(apple, apricot, banana, carrot):
435444
self.assertSetEqual(
436445
com.matches(3, "car", funcprops=funcspec), {"carrot="}
437446
)
447+
self.assertSetEqual(
448+
com.matches(5, "func(", funcprops=funcspec),
449+
{"apple=", "apricot=", "banana=", "carrot="},
450+
)

0 commit comments

Comments
 (0)