Skip to content

Commit

Permalink
Sort completions by input resemblance. (#2018)
Browse files Browse the repository at this point in the history
* Sort completions by input resemblance.

Fixes #2017

* Clean code
  • Loading branch information
hongquan committed Jul 15, 2024
1 parent 2859e4f commit 91ffdea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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

0 comments on commit 91ffdea

Please sign in to comment.