From b7c2bacbd21007a06bbfb79a2937c41ea68265d6 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 5 Aug 2019 10:11:36 +0200 Subject: [PATCH] Fix string additions when used in certain ways --- jedi/api/file_name.py | 17 ++++++++++------- test/test_api/test_completion.py | 6 ++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/jedi/api/file_name.py b/jedi/api/file_name.py index cf0663c6..6dfaf7ea 100644 --- a/jedi/api/file_name.py +++ b/jedi/api/file_name.py @@ -7,27 +7,32 @@ from jedi.evaluate.helpers import get_str_or_none def file_name_completions(evaluator, module_context, start_leaf, string, like_name): + # First we want to find out what can actually be changed as a name. base_name = os.path.basename(string) - like_name = base_name + like_name - string = os.path.dirname(string) + like_name_length = len(base_name + like_name) string = _get_string_additions(module_context, start_leaf) + string + # Here we use basename again, because if strings are added like + # `'foo' + 'bar`, it should complete to `foobar/`. + must_start_with = os.path.basename(string) + like_name + string = os.path.dirname(string) + base_path = os.path.join(evaluator.project._path, string) try: listed = os.listdir(base_path) except FileNotFoundError: return for name in listed: - if name.startswith(like_name): + if name.startswith(must_start_with): path_for_name = os.path.join(base_path, name) if os.path.isdir(path_for_name): name += os.path.sep yield classes.Completion( evaluator, - FileName(evaluator, name), + FileName(evaluator, name[len(must_start_with) - like_name_length:]), stack=None, - like_name_length=len(like_name), + like_name_length=like_name_length ) @@ -55,8 +60,6 @@ def _get_string_additions(module_context, start_leaf): if child_node != '+': break was_addition = True - child_node - module_context return string diff --git a/test/test_api/test_completion.py b/test/test_api/test_completion.py index 1d333996..286e969e 100644 --- a/test/test_api/test_completion.py +++ b/test/test_api/test_completion.py @@ -190,6 +190,12 @@ def test_keyword_completion(Script, code, has_keywords): ('example.py', '"test" + "%stest_cac' % s, None, ['he.py']), ('example.py', '"test" + "%s" + "test_cac' % s, None, ['he.py']), ('example.py', 'x = 1 + "test', None, [s]), + ('example.py', 'x = f("te" + "st)', 16, [s]), + ('example.py', 'x = f("te" + "st', 16, [s]), + ('example.py', 'x = f("te" + "st"', 16, [s]), + ('example.py', 'x = f("te" + "st")', 16, [s]), + ('example.py', 'x = f("t" + "est")', 16, [s]), + ('example.py', '"test" + "', None, [s]), ] ) def test_file_path_completions(Script, file, code, column, expected):