Skip to content

Commit

Permalink
Merge pull request #367 from pdm-project:completion
Browse files Browse the repository at this point in the history
Add automation script to generate completions on release
  • Loading branch information
frostming authored Apr 3, 2021
2 parents bd95330 + b8349a0 commit d245736
Show file tree
Hide file tree
Showing 10 changed files with 1,031 additions and 16 deletions.
1 change: 1 addition & 0 deletions news/188.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add zsh completion script
1 change: 1 addition & 0 deletions news/367.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Full-featured completion scripts for Zsh and Powershell - section selection, package name autocompletion and so on. Windows is a first-class citizen!
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.

15 changes: 11 additions & 4 deletions pdm/cli/commands/completion.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import argparse
import importlib.resources
import sys

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 @@ -19,9 +23,12 @@ def add_arguments(self, parser: argparse.ArgumentParser) -> None:

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 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.replace("%{python_executable}", sys.executable))
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 --non-interactive"
;;

(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 --dry-run --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

Loading

0 comments on commit d245736

Please sign in to comment.