Skip to content

fix(python): complete filenames for script arguments #1018

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions completions/python
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,38 @@ _comp_cmd_python()
esac

local noargopts='!(-*|*[cmQWX]*)'

# if command, module, or script is already given by [-c command | -m module
# | script], complete all kind of files.
local i has_command=""
for ((i = 1; i < cword; i++)); do
# shellcheck disable=SC2254
case ${words[i]} in
-${noargopts}[QWX])
((i++))
;;
-${noargopts}[cm]?*)
has_command=set
break
;;
-- | -${noargopts}[cm])
if ((i + 1 < cword)); then
has_command=set
break
fi
;;
-*) ;;
*)
has_command=set
break
;;
esac
done
if [[ $has_command ]]; then
_comp_compgen_filedir
return
fi

# shellcheck disable=SC2254
case $prev in
--help | --version | -${noargopts}[?hVc])
Expand Down Expand Up @@ -74,19 +106,10 @@ _comp_cmd_python()
_comp_compgen -- -W "help off"
return
;;
!(?(*/)?(micro)python*([0-9.])|?(*/)py@(py|ston)*([0-9.])|-?))
if [[ $cword -lt 2 || ${words[cword - 2]} != -[QWX] ]]; then
_comp_compgen_filedir
return
fi
;;
esac

# if -c or -m is already given, complete all kind of files.
if [[ ${words[*]::cword} == *\ -[cm]\ * ]]; then
_comp_compgen -a filedir
elif [[ $cur != -* ]]; then
_comp_compgen -a filedir '@(py?([cowz])|zip)'
if [[ $prev == -- || $cur != -* ]]; then
_comp_compgen_filedir '@(py?([cowz])|zip)'
else
_comp_compgen_help - <<<"$("$1" -h |
awk '{ sub("\\(-bb:","\n-bb "); print }')"
Expand Down
Empty file added test/fixtures/python/bar.txt
Empty file.
Empty file added test/fixtures/python/foo.py
Empty file.
39 changes: 39 additions & 0 deletions test/t/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,42 @@ def test_9(self, completion):
)
def test_bb(self, completion):
assert "-bb" in completion

@pytest.mark.complete("python foo ", cwd="python")
def test_script_arg(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -- foo ", cwd="python")
def test_script_arg_with_double_hyphen(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -m foo bar -p ", cwd="python")
def test_module_arg(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python foo bar -p ", cwd="python")
def test_script_arg_after_option(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -- foo bar -p ", cwd="python")
def test_script_arg_after_option_with_double_hyphen(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -m foo bar -p ", cwd="python")
def test_module_arg_after_option(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -mfoo bar -p ", cwd="python")
def test_module_arg_after_option_with_connected_m_arg(self, completion):
assert "bar.txt" in completion

@pytest.mark.complete("python -- ", cwd="python")
def test_script_name(self, completion):
assert "bar.txt" not in completion

@pytest.mark.complete("python -W -mfoo ", cwd="python")
def test_script_name_with_fake_m_arg(self, completion):
"""In this case, -mfoo looks like an option to specify the module, but
it should not be treated as the module name because it is an option
argument to -W."""
assert "bar.txt" not in completion