psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.87k stars 2.45k forks source link

please add bash-completion (working version included) #1576

Open eighthave opened 4 years ago

eighthave commented 4 years ago

black does not have bash-completion, hitting tab brings up nothing. I wrote up a quick bash-completion script that can be included under the same license as black uses. There are dynamic completion methods, where the bash-completion is generated from the Python code, but many Python devs find the required changes weird.

To test this, since this into either _/etc/bashcompletion.d/black or __:

# Copyright 2020, Hans-Christoph Steiner
_have black &&
_black()
{
    local cur prev

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    case $prev in
    -l|--line-length|--include|--exclude)
        return 0;;
    esac
    if [[ "$cur" == -* ]]; then
    opts='-l -S -N -q -v -h'
    lopts='
        --check
        --config
        --diff
        --exclude
        --fast
        --include
        --line-length
        --quiet
        --safe
        --skip-numeric-underscore-normalization
        --skip-string-normalization
        --verbose
        --version
    '    
    COMPREPLY=( $(compgen -W "${opts[*]} ${lopts[*]}" -- $cur) )
    else
    _filedir
    fi
}
complete -F _black $filenames black
Freed-Wu commented 2 years ago

Black use click as its command line parser library. click has a related library named click-completion to generate bash/zsh/fish completions. It is no need to write shell completion manually, just need someone who know click-completion to add this feature.

Freed-Wu commented 2 years ago

This is method: https://click.palletsprojects.com/en/8.1.x/shell-completion/

Should contact the packager of linux distribution to do this job for black and blackd.

And this issue can be closed. :smile:

BTW, click's completion seems to have space to improve.

Expected behaviour:

❯ black --help <TAB>
no more arguments

Actual:

❯ black --help --<TAB>
unsorted
--code                        Format the code passed in as a string.
--line-length                 How many characters per line to allow.
--target-version              Python versions that should be supported by Black's output. [default: per-file auto-detection]
--pyi                         Format all input files like typing stubs regardless of file extension (useful when piping source on standard input).