diff --git a/jedi/api/__init__.py b/jedi/api/__init__.py index 0e505c9f..4ab5c19c 100644 --- a/jedi/api/__init__.py +++ b/jedi/api/__init__.py @@ -201,7 +201,7 @@ class Script(object): self._inference_state.environment, ) - def completions(self, fuzzy=False): + def completions(self, fuzzy=True): """ Return :class:`classes.Completion` objects. Those objects contain information about the completions, more than just names. diff --git a/jedi/api/completion.py b/jedi/api/completion.py index 906a2149..d9ffda06 100644 --- a/jedi/api/completion.py +++ b/jedi/api/completion.py @@ -27,18 +27,6 @@ def get_call_signature_param_names(call_signatures): Parameter.KEYWORD_ONLY): yield p._name -def start_match(string, like_name): - return string.startswith(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 False - def filter_names(inference_state, completion_names, stack, like_name, fuzzy): comp_dct = {} @@ -49,9 +37,9 @@ def filter_names(inference_state, completion_names, stack, like_name, fuzzy): if settings.case_insensitive_completion: string = string.lower() if fuzzy: - match = fuzzy_match(string, like_name) + match = helpers.fuzzy_match(string, like_name) else: - match = start_match(string, like_name) + match = helpers.start_match(string, like_name) if match: new = classes.Completion( inference_state, @@ -111,7 +99,8 @@ class Completion: completions = list(file_name_completions( self._inference_state, self._module_context, start_leaf, string, self._like_name, self._call_signatures_callback, - self._code_lines, self._original_position + self._code_lines, self._original_position, + fuzzy )) if completions: return completions diff --git a/jedi/api/file_name.py b/jedi/api/file_name.py index 5871fd90..04f90ba9 100644 --- a/jedi/api/file_name.py +++ b/jedi/api/file_name.py @@ -3,12 +3,13 @@ import os from jedi._compatibility import FileNotFoundError, force_unicode, scandir from jedi.inference.names import AbstractArbitraryName from jedi.api import classes +from jedi.api.helpers import fuzzy_match, start_match from jedi.inference.helpers import get_str_or_none from jedi.parser_utils import get_string_quote def file_name_completions(inference_state, module_context, start_leaf, string, - like_name, call_signatures_callback, code_lines, position): + like_name, call_signatures_callback, code_lines, position, fuzzy): # First we want to find out what can actually be changed as a name. like_name_length = len(os.path.basename(string) + like_name) @@ -37,7 +38,11 @@ def file_name_completions(inference_state, module_context, start_leaf, string, return for entry in listed: name = entry.name - if name.startswith(must_start_with): + if fuzzy: + match = fuzzy_match(name, must_start_with) + else: + match = start_match(name, must_start_with) + if match: if is_in_os_path_join or not entry.is_dir(): if start_leaf.type == 'string': quote = get_string_quote(start_leaf) diff --git a/jedi/api/helpers.py b/jedi/api/helpers.py index 236deb02..b59fee93 100644 --- a/jedi/api/helpers.py +++ b/jedi/api/helpers.py @@ -19,6 +19,19 @@ from jedi.cache import call_signature_time_cache CompletionParts = namedtuple('CompletionParts', ['path', 'has_dot', 'name']) +def start_match(string, like_name): + return string.startswith(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 False + + 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/jedi/api/replstartup.py b/jedi/api/replstartup.py index 3ac84708..38aa8e6f 100644 --- a/jedi/api/replstartup.py +++ b/jedi/api/replstartup.py @@ -21,7 +21,7 @@ import jedi.utils from jedi import __version__ as __jedi_version__ print('REPL completion using Jedi %s' % __jedi_version__) -jedi.utils.setup_readline() +jedi.utils.setup_readline(fuzzy=False) del jedi diff --git a/jedi/utils.py b/jedi/utils.py index a5cf84b1..56b21d0c 100644 --- a/jedi/utils.py +++ b/jedi/utils.py @@ -17,7 +17,7 @@ from jedi import Interpreter READLINE_DEBUG = False -def setup_readline(namespace_module=__main__): +def setup_readline(namespace_module=__main__, fuzzy=False): """ Install Jedi completer to :mod:`readline`. @@ -83,7 +83,7 @@ def setup_readline(namespace_module=__main__): logging.debug("Start REPL completion: " + repr(text)) interpreter = Interpreter(text, [namespace_module.__dict__]) - completions = interpreter.completions() + completions = interpreter.completions(fuzzy=fuzzy) logging.debug("REPL completions: %s", completions) self.matches = [ diff --git a/test/test_api/test_api.py b/test/test_api/test_api.py index a90c1a89..c370be48 100644 --- a/test/test_api/test_api.py +++ b/test/test_api/test_api.py @@ -306,6 +306,18 @@ def test_goto_follow_builtin_imports(Script): assert d.in_builtin_module() is True +def test_file_fuzzy_completion(Script, tmp_path): + folder0 = tmp_path / "inference" + folder0.mkdir() + file0_path0 = folder0 / "sys_path.py" + file0_path0.write_text('\n') + file0_path1 = folder0 / "syntax_tree.py" + file0_path1.write_text('\n') + script = Script('"{}/yt'.format(folder0)) + assert ['sys_path.py"', + 'syntax_tree.py"' ] == [comp.name for comp in script.completions(fuzzy=True)] + + def test_fuzzy_completion(Script): script = Script('string = "hello"\nstring.upper') assert ['isupper', @@ -325,4 +337,6 @@ def test_math_fuzzy_completion(Script): def test_math_fuzzy_completion(Script): script = Script('import math\nmath.og') assert ['copysign', 'log', 'log10', - 'log1p'] == [comp.name for comp in script.completions(fuzzy=True)] \ No newline at end of file + 'log1p'] == [comp.name for comp in script.completions(fuzzy=True)] + + diff --git a/test/test_api/test_completion.py b/test/test_api/test_completion.py index 6b5735af..ab1af15b 100644 --- a/test/test_api/test_completion.py +++ b/test/test_api/test_completion.py @@ -268,7 +268,7 @@ def test_file_path_completions(Script, file, code, column, expected): else: assert [c.complete for c in comps] == expected -from jedi.api.completion import start_match, fuzzy_match +from jedi.api.helpers import start_match, fuzzy_match def test_start_match(): assert start_match('Condition', 'C')