Skip to content

Commit

Permalink
Change the behavior of 'pdm completion zsh'
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloxaf authored and frostming committed Apr 1, 2021
1 parent 3c9121c commit 0fd2a74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
12 changes: 8 additions & 4 deletions pdm/cli/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import pathlib

from pdm.cli.commands.base import BaseCommand
from pdm.project import Project
Expand All @@ -21,7 +22,10 @@ def handle(self, project: Project, options: argparse.Namespace) -> None:
import shellingham
from pycomplete import Completer

completer = Completer(project.core.parser)
project.core.ui.echo(
completer.render(options.shell or shellingham.detect_shell()[0])
)
shell = options.shell or shellingham.detect_shell()[0]
if shell == "zsh":
zsh_completion = pathlib.Path(__file__).parent / "../completions/_pdm"
stream.echo(zsh_completion.read_text())
else:
completer = Completer(project.core.parser)
stream.echo(completer.render(shell))
57 changes: 44 additions & 13 deletions completion/_pdm → pdm/cli/completions/_pdm
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ _pdm() {
local -a arguments=(
{-h,--help}'[Show help message and exit]'
{-v,--verbose}'[Show detailed output]'
{-p,--project}'=[Specify another path as the project root]:directory:_files -/'
'-vv[Show more detailed output]'
)
local sub_commands=(
Expand All @@ -36,10 +37,11 @@ _pdm() {
'use:Use the given python version or path as base interpreter'
)

_arguments -C -A '-*' \
_arguments -s -C -A '-*' \
$arguments \
{-V,--version}'[Show the version and exit]' \
'--pep582=[Print the command line to be eval by the shell]:shell:(zsh bash fish powershell cmd tcsh csh)' \
{-I,--ignore-python}'[Ignore the Python path saved in the pdm.toml config]' \
'--pep582=[Print the command line to be eval by the shell]:shell:(zsh bash fish tcsh csh)' \
'*:: :->_subcmds' \
&& return 0

Expand Down Expand Up @@ -75,11 +77,35 @@ _pdm() {
)
;;
cache)
local -a actions=("clear:Clean all the files under cache directory")
_describe -t command 'pdm cache actions' actions && return 0
_arguments -C \
$arguments \
': :->command' \
'*:: :->args' && ret=0
case $state in
command)
local -a actions=(
"clear:Clean all the files under cache directory"
"remove:Remove files matching the given pattern"
"list:List the built wheels stored in the cache"
"info:Show the info and current size of caches"
)
_describe -t command 'pdm cache actions' actions && ret=0
;;
args)
case $words[1] in
clear)
compadd -X type 'hashes' 'http' 'wheels' 'metadata' && ret=0
;;
*)
_message "pattern" && ret=0
;;
esac
;;
esac
return $ret
;;
config)
_arguments \
_arguments -s \
{-g+,--global+}'[Use the global project, accepts an optional path to the project directory]:directory:_files -/' \
{-l,--local}"[Set config in the project's local configuration filie]" \
{-d,--delete}'[Unset a configuration key]' \
Expand Down Expand Up @@ -152,13 +178,18 @@ _pdm() {
)
;;
run)
arguments+=(
{-g+,--global+}"[Use the global project, accepts an optional path to the project directory]:directory:_files -/"
{-l,--list}'[Show all available scripts defined in pyproject.toml]'
{-s,--site-packages}'[Load site-packages from system interpreter]'
'(-)1:command: _command_names -e'
'*::arguments: _normal '
)
_arguments -s \
{-g+,--global+}"[Use the global project, accepts an optional path to the project directory]:directory:_files -/" \
{-l,--list}'[Show all available scripts defined in pyproject.toml]' \
{-s,--site-packages}'[Load site-packages from system interpreter]' \
'(-)1:command:->command' \
'*:arguments: _normal ' && return 0
if [[ $state == command ]]; then
_command_names -e
local local_commands=(__pypackages__/3.9/bin/*(N:t))
_describe "local command" local_commands
return 0
fi
;;
search)
arguments+=(
Expand Down Expand Up @@ -205,7 +236,7 @@ _pdm() {
;;
esac

_arguments $arguments && ret=0
_arguments -s $arguments && ret=0

return ret
}
Expand Down

0 comments on commit 0fd2a74

Please sign in to comment.