Skip to content

Commit

Permalink
change the location of completion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Apr 1, 2021
1 parent 0fd2a74 commit 6cc7f8a
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 20 deletions.
12 changes: 1 addition & 11 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions pdm/cli/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import argparse
import pathlib
import importlib.resources

from pdm.cli.commands.base import BaseCommand
from pdm.exceptions import PdmUsageError
from pdm.project import Project


class Command(BaseCommand):
"""Generate completion scripts for the given shell"""

arguments = []
SUPPORTED_SHELLS = ("bash", "zsh", "fish", "powershell")

def add_arguments(self, parser: argparse.ArgumentParser) -> None:
parser.add_argument(
Expand All @@ -20,12 +22,12 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:

def handle(self, project: Project, options: argparse.Namespace) -> None:
import shellingham
from pycomplete import Completer

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))
if shell not in self.SUPPORTED_SHELLS:
raise PdmUsageError(f"Unsupported shell: {shell}")
suffix = "ps1" if shell == "powershell" else shell
completion = importlib.resources.read_text(
"pdm.cli.completions", f"pdm.{suffix}"
)
project.core.ui.echo(completion)
Empty file added pdm/cli/completions/__init__.py
Empty file.
127 changes: 127 additions & 0 deletions pdm/cli/completions/pdm.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# BASH completion script for pdm
# Generated by pycomplete 0.3.1

_pdm_74498d98b6b27a5a_complete()
{
local cur script coms opts com
COMPREPLY=()
_get_comp_words_by_ref -n : cur words

# for an alias, get the real script behind it
if [[ $(type -t ${words[0]}) == "alias" ]]; then
script=$(alias ${words[0]} | sed -E "s/alias ${words[0]}='(.*)'/\\1/")
else
script=${words[0]}
fi

# lookup for command
for word in ${words[@]:1}; do
if [[ $word != -* ]]; then
com=$word
break
fi
done

# completing for an option
if [[ ${cur} == --* ]] ; then
opts="--help --ignore-python --pep582 --verbose --version"

case "$com" in

(add)
opts="--dev --editable --global --help --no-sync --project --save-compatible --save-exact --save-wildcard --section --update-eager --update-reuse --verbose"
;;

(build)
opts="--dest --help --no-clean --no-sdist --no-wheel --project --verbose"
;;

(cache)
opts="--help --verbose"
;;

(completion)
opts="--help"
;;

(config)
opts="--delete --global --help --local --project --verbose"
;;

(export)
opts="--dev --format --global --help --no-default --output --project --pyproject --section --verbose --without-hashes"
;;

(import)
opts="--dev --format --global --help --project --section --verbose"
;;

(info)
opts="--env --global --help --project --python --verbose --where"
;;

(init)
opts="--global --help --project --verbose"
;;

(install)
opts="--dev --global --help --no-default --no-lock --project --section --verbose"
;;

(list)
opts="--global --graph --help --project --reverse --verbose"
;;

(lock)
opts="--global --help --project --verbose"
;;

(remove)
opts="--dev --global --help --no-sync --project --section --verbose"
;;

(run)
opts="--global --help --list --project --verbose"
;;

(search)
opts="--global --help --project --verbose"
;;

(show)
opts="--global --help --project --verbose"
;;

(sync)
opts="--clean --dev --dry-run --global --help --no-clean --no-default --project --section --verbose"
;;

(update)
opts="--dev --global --help --no-default --outdated --project --save-compatible --save-exact --save-wildcard --section --top --unconstrained --update-eager --update-reuse --verbose"
;;

(use)
opts="--first --global --help --project --verbose"
;;

esac

COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
__ltrim_colon_completions "$cur"

return 0;
fi

# completing for a command
if [[ $cur == $com ]]; then
coms="add build cache completion config export import info init install list lock remove run search show sync update use"

COMPREPLY=($(compgen -W "${coms}" -- ${cur}))
__ltrim_colon_completions "$cur"

return 0
fi
}

complete -o default -F _pdm_74498d98b6b27a5a_complete pdm

Binary file added pdm/cli/completions/pdm.fish
Binary file not shown.
Binary file added pdm/cli/completions/pdm.ps1
Binary file not shown.
File renamed without changes.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies = [
"pdm-pep517<0.7.0,>=0.6.0",
"pep517",
"pip>=20.1",
"pycomplete<1.0.0,>=0.2.0",

"python-cfonts",
"python-dotenv<1.0.0,>=0.15.0",
"pythonfinder",
Expand Down

0 comments on commit 6cc7f8a

Please sign in to comment.