Path completion in python code (#4457)

* completions: path: Fix completing a single-arg python code segment

* tests: completions: path: Test completing python code

* news: Add path-completion-in-python-code
This commit is contained in:
Daniel Shimon 2021-09-07 01:23:49 +03:00 committed by GitHub
parent 89f54ee10a
commit 6df8a24fc9
Failed to generate hash of commit
3 changed files with 39 additions and 1 deletions

View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* <news item>
**Fixed:**
* Completing a single-arg python code segment (e.g. ``@(/etc/hos<TAB>)``).
**Security:**
* <news item>

View file

@ -71,3 +71,18 @@ def test_path_from_partial_string(prefix):
else:
expected = (f"{quote}{string}{quote}", string, f"{prefix}{quote}", quote)
assert out == expected
@pytest.mark.parametrize("num_args", (0, 1, 2, 3))
def test_path_in_python_code(num_args, completion_context_parse):
with tempfile.NamedTemporaryFile(prefix="long_name") as tmp:
args = []
if num_args:
args = ["blah"] * 3 + [tmp.name[:-2]]
args = args[-num_args:]
inner_line = " ".join(map(repr, args))
exp = xcp.complete_path(completion_context_parse(inner_line, len(inner_line)))
line = "@(" + inner_line
out = xcp.complete_path(completion_context_parse(line, len(line)))
assert out == exp

View file

@ -362,7 +362,7 @@ def complete_path(context):
elif context.python:
line = context.python.prefix
# simple prefix _complete_path_raw will handle gracefully:
prefix = line.rsplit(" ", 1)[1]
prefix = line.rsplit(" ", 1)[-1]
return _complete_path_raw(prefix, line, len(line) - len(prefix), len(line), {})
return set(), 0