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 1 commit
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
Prev Previous commit
Next Next commit
Better completion results order
  • Loading branch information
gpotter2 committed Jul 22, 2023
commit de333118a7dc89925c7e3d1f246271b814a4e37c
14 changes: 13 additions & 1 deletion bpython/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,16 @@ def get_completer(
double underscore methods like __len__ in method signatures
"""

def _cmpl_sort(x: str) -> Tuple[Any, ...]:
"""
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 @@ -765,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
9 changes: 9 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