fuzzy_match and start_match are now match with fuzzy param

This commit is contained in:
Dave Halter
2020-03-01 18:03:13 +01:00
parent ccc1262a3e
commit a6ef8efb72
4 changed files with 20 additions and 21 deletions

View File

@@ -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,

View File

@@ -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:

View File

@@ -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))

View File

@@ -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):