Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions 21 bpython/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,12 @@ def matches(
return matches if matches else None

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


class ExpressionAttributeCompletion(AttrCompletion):
Expand Down Expand Up @@ -742,6 +747,16 @@ def get_completer(
double underscore methods like __len__ in method signatures
"""

def _cmpl_sort(x: str) -> Tuple[bool, ...]:
"""
Function used to sort the matches.
"""
# put parameters above everything in completion
return (
x[-1] != "=",
x,
)

for completer in completers:
try:
matches = completer.matches(
Expand All @@ -760,7 +775,9 @@ def get_completer(
)
continue
if matches is not None:
return sorted(matches), (completer if matches else None)
return sorted(matches, key=_cmpl_sort), (
completer if matches else None
)

return [], None

Expand Down
13 changes: 13 additions & 0 deletions 13 bpython/test/test_autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ def test_two_completers_get_both(self):
cumulative = autocomplete.CumulativeCompleter([a, b])
self.assertEqual(cumulative.matches(3, "abc"), {"a", "b"})

def test_order_completer(self):
a = self.completer(["ax", "ab="])
b = self.completer(["aa"])
cumulative = autocomplete.CumulativeCompleter([a, b])
self.assertEqual(
autocomplete.get_completer([cumulative], 1, "a"),
(["ab=", "aa", "ax"], cumulative),
)


class TestFilenameCompletion(unittest.TestCase):
def setUp(self):
Expand Down Expand Up @@ -435,3 +444,7 @@ def func(apple, apricot, banana, carrot):
self.assertSetEqual(
com.matches(3, "car", funcprops=funcspec), {"carrot="}
)
self.assertSetEqual(
com.matches(5, "func(", funcprops=funcspec),
{"apple=", "apricot=", "banana=", "carrot="},
)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.