diff --git a/AUTHORS.txt b/AUTHORS.txt index 349a867be..3ccb0a7cf 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -65,6 +65,7 @@ Code Contributors - Márcio Mazza (@marciomazza) - Martin Vielsmaier (@moser) - TingJia Wu (@WutingjiaX) +- Nguyễn Hồng Quân And a few more "anonymous" contributors. diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 342f75065..31abcaf73 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -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, @@ -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())) ) diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index 55feaf8bb..8ff559f69 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -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):