diff --git a/jedi/api/completion.py b/jedi/api/completion.py index f8853cdf..86636db1 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -48,11 +48,7 @@ def filter_names(inference_state, completion_names, stack, like_name, fuzzy, cac string = name.string_name if settings.case_insensitive_completion: string = string.lower() - if fuzzy: - match = helpers.fuzzy_match(string, like_name) - else: - match = helpers.start_match(string, like_name) - if match: + if helpers.match(string, like_name, fuzzy=fuzzy): new = classes.Completion( inference_state, name, diff --git a/jedi/api/file_name.py b/jedi/api/file_name.py index 11994074..3bce430d 100644 --- a/jedi/api/file_name.py +++ b/jedi/api/file_name.py @@ -3,7 +3,7 @@ import os from jedi._compatibility import FileNotFoundError, force_unicode, scandir from jedi.api import classes from jedi.api.strings import StringName, get_quote_ending -from jedi.api.helpers import fuzzy_match, start_match +from jedi.api.helpers import match from jedi.inference.helpers import get_str_or_none @@ -42,11 +42,7 @@ def complete_file_name(inference_state, module_context, start_leaf, string, return for entry in listed: name = entry.name - if fuzzy: - match = fuzzy_match(name, must_start_with) - else: - match = start_match(name, must_start_with) - if match: + if match(name, must_start_with, fuzzy=fuzzy): if is_in_os_path_join or not entry.is_dir(): name += get_quote_ending(start_leaf.value, code_lines, position) else: diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index fbfbdb31..cd6c70c4 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -20,19 +20,26 @@ from jedi.cache import signature_time_cache CompletionParts = namedtuple('CompletionParts', ['path', 'has_dot', 'name']) -def start_match(string, like_name): +def _start_match(string, like_name): return string.startswith(like_name) -def fuzzy_match(string, like_name): +def _fuzzy_match(string, like_name): if len(like_name) <= 1: return like_name in string pos = string.find(like_name[0]) if pos >= 0: - return fuzzy_match(string[pos + 1:], like_name[1:]) + return _fuzzy_match(string[pos + 1:], like_name[1:]) return False +def match(string, like_name, fuzzy=False): + if fuzzy: + return _fuzzy_match(string, like_name) + else: + return _start_match(string, like_name) + + def sorted_definitions(defs): # Note: `or ''` below is required because `module_path` could be return sorted(defs, key=lambda x: (x.module_path or '', x.line or 0, x.column or 0, x.name)) diff --git a/test/test_api/test_completion.py b/test/test_api/test_completion.py index 7d5d183f..153928b4 100644 --- a/test/test_api/test_completion.py +++ b/test/test_api/test_completion.py @@ -6,7 +6,7 @@ from textwrap import dedent import pytest from ..helpers import root_dir -from jedi.api.helpers import start_match, fuzzy_match +from jedi.api.helpers import _start_match, _fuzzy_match def test_in_whitespace(Script): @@ -386,15 +386,15 @@ def test_dict_keys_completions(Script, added_code, column, expected, skip_pre_py def test_start_match(): - assert start_match('Condition', 'C') + assert _start_match('Condition', 'C') def test_fuzzy_match(): - assert fuzzy_match('Condition', 'i') - assert not fuzzy_match('Condition', 'p') - assert fuzzy_match('Condition', 'ii') - assert not fuzzy_match('Condition', 'Ciito') - assert fuzzy_match('Condition', 'Cdiio') + assert _fuzzy_match('Condition', 'i') + assert not _fuzzy_match('Condition', 'p') + assert _fuzzy_match('Condition', 'ii') + assert not _fuzzy_match('Condition', 'Ciito') + assert _fuzzy_match('Condition', 'Cdiio') def test_ellipsis_completion(Script):