Skip to content

Improve autocompletion of parameters #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2023
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 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 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="},
)