Skip to content
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

Sort completions by input resemblance. #2018

Merged
merged 2 commits into from
Jul 15, 2024
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
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Code Contributors
- Márcio Mazza (@marciomazza)
- Martin Vielsmaier (@moser) <martin@vielsmaier.net>
- TingJia Wu (@WutingjiaX) <wutingjia@bytedance.com>
- Nguyễn Hồng Quân <ng.hong.quan@gmail.com>

And a few more "anonymous" contributors.

Expand Down
8 changes: 7 additions & 1 deletion jedi/api/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ def __init__(self, inference_state, module_context, code_lines, position,

self._fuzzy = fuzzy

# Return list of completions in this order:
# - Beginning with what user is typing
# - Public (alphabet)
# - Private ("_xxx")
# - Dunder ("__xxx")
def complete(self):
leaf = self._module_node.get_leaf_for_position(
self._original_position,
Expand Down Expand Up @@ -176,7 +181,8 @@ def complete(self):
return (
# Removing duplicates mostly to remove False/True/None duplicates.
_remove_duplicates(prefixed_completions, completions)
+ sorted(completions, key=lambda x: (x.name.startswith('__'),
+ sorted(completions, key=lambda x: (not x.name.startswith(self._like_name),
x.name.startswith('__'),
x.name.startswith('_'),
x.name.lower()))
)
Expand Down
13 changes: 11 additions & 2 deletions test/test_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,19 @@ def test_docstrings_for_completions(Script):
assert isinstance(c.docstring(), str)


def test_completions_order_most_resemblance_on_top(Script):
"""Test that the completion which resembles the in-typing the most will come first."""
code = "from pathlib import Path\npath = Path('hello.txt')\n\npat"
script = Script(code)
# User is typing "pat" and "path" is closer to it than "Path".
assert ['path', 'Path'] == [comp.name for comp in script.complete()]


def test_fuzzy_completion(Script):
script = Script('string = "hello"\nstring.upper')
assert ['isupper',
'upper'] == [comp.name for comp in script.complete(fuzzy=True)]
# 'isupper' is included because it is fuzzily matched.
assert ['upper',
'isupper'] == [comp.name for comp in script.complete(fuzzy=True)]


def test_math_fuzzy_completion(Script, environment):
Expand Down