From d838eaecd2e2bfc0764f0372dd81c685640c4d42 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 9 Mar 2020 23:55:17 +0100 Subject: [PATCH] Implement Script.complete_search instead of the complete param and return Completion objects --- jedi/api/__init__.py | 13 +++++++------ jedi/api/completion.py | 10 +++++++++- test/test_api/test_search.py | 20 +++++++++++++++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 3f598851..3aecf0b5 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -344,16 +344,14 @@ class Script(object): :param all_scopes: If True lists the symbols of all scopes instead of only the module. - :param definitions: If True lists the names that have been defined by a - class, function or a statement (``a = b`` returns ``a``). - :param references: If True lists all the names that are not listed by - ``definitions=True``. E.g. ``a = b`` returns ``b``. """ return self._search(string, **kwargs) # Python 2 ... + def _search(self, string, all_scopes=False): + return self._search_func(string, all_scopes=all_scopes) + @to_list - def _search(self, string, complete=False, all_scopes=False, - fuzzy=False): + def _search_func(self, string, all_scopes=False, complete=False, fuzzy=False): names = self._names(all_scopes=all_scopes) wanted_type, wanted_names = helpers.split_search_string(string) return search_in_module( @@ -366,6 +364,9 @@ class Script(object): fuzzy=fuzzy, ) + def complete_search(self, string, **kwargs): + return self._search_func(string, complete=True, **kwargs) + @validate_line_column def help(self, line=None, column=None): """ diff --git a/jedi/api/completion.py b/jedi/api/completion.py index c6776b90..f9cbad24 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -604,6 +604,14 @@ def search_in_module(inference_state, module_context, names, wanted_names, if convert: names = convert_names(names) for n2 in names: - def_ = classes.Definition(inference_state, n2) + if complete: + def_ = classes.Completion( + inference_state, n2, + stack=None, + like_name_length=len(last_name), + is_fuzzy=fuzzy, + ) + else: + def_ = classes.Definition(inference_state, n2) if not wanted_type or wanted_type == def_.type: yield def_ diff --git a/test/test_api/test_search.py b/test/test_api/test_search.py index 7230891a..0f38b560 100644 --- a/test/test_api/test_search.py +++ b/test/test_api/test_search.py @@ -69,6 +69,24 @@ def test_simple_search(Script, string, descriptions, kwargs, skip_pre_python36): if sys.version_info < (3, 6): pytest.skip() - defs = Script(path=__file__).search(string, **kwargs) + if kwargs.pop('complete', False) is True: + defs = Script(path=__file__).complete_search(string, **kwargs) + else: + defs = Script(path=__file__).search(string, **kwargs) this_mod = 'test.test_api.test_search.' assert [d.type + ' ' + d.full_name.replace(this_mod, '') for d in defs] == descriptions + + +@pytest.mark.parametrize( + 'string, completions, fuzzy, all_scopes', [ + ('SomeCl', ['ass'], False, False), + ('SomeCl', [None], True, False), + ('twic', [], False, False), + ('some_f', [], False, False), + ('twic', ['e', 'e'], False, True), + ('some_f', ['unction'], False, True), + ] +) +def test_complete_search(Script, string, completions, fuzzy, all_scopes): + defs = Script(path=__file__).complete_search(string, fuzzy=fuzzy, all_scopes=all_scopes) + assert [d.complete for d in defs] == completions