adaszko / complgen

Declarative bash/fish/zsh completions without writing shell scripts
Apache License 2.0
221 stars 7 forks source link

Bash integration not working for `--color=(always|never)` #34

Closed Ordoviz closed 11 months ago

Ordoviz commented 1 year ago

Consider this grammar from the e2e tests:

mygrep --color "use markers to highlight the matching strings"=<WHEN>;
<WHEN> ::= always | never | auto;

If I save this to /tmp/mygrep.usage and source the Bash integration from the README

for path in /tmp/*.usage; do
    stem=$(basename "$path" .usage)
    eval "
_complgen_jit_$stem () {
    local words cword
    _get_comp_words_by_ref -n = words cword
    local prefix="\${COMP_WORDS[\$COMP_CWORD]}"
    local -a completions=(\$(complgen complete \"/tmp/${stem}.usage\" bash --prefix="\$prefix" -- \${COMP_WORDS[@]:1:\$COMP_CWORD-1}))
    for item in "\${completions[@]}"; do
        if [[ \$item = "\${prefix}"* ]]; then
            COMPREPLY+=("\$item")
        fi
    done
    __ltrim_colon_completions "\$prefix"
    return 0
}
"
    complete -o nospace -F _complgen_jit_$stem "$stem"
    unset stem
done

it will autocomplete up to mygrep --color= but doesn't suggest always or never.

I believe the output from (or the invocation of) complgen complete needs to be adjusted to fix this, but I don't actually know. The PR #33 does NOT fix this.

Compiling the grammar into a Bash script and sourcing that works fine.

adaszko commented 1 year ago

Here's what works:

for path in ~/.config/complgen/*.usage; do
    stem=$(basename "$path" .usage)
    eval "
_complgen_jit_$stem () {
    local words cword
    _get_comp_words_by_ref -n = words cword
    local prefix="\${words[\$cword]}"
    local -a completions=(\$(complgen complete \"$HOME/.config/complgen/${stem}.usage\" bash --prefix="\$prefix" -- \${words[@]:1:\$cword-1}))
    for item in "\${completions[@]}"; do
        if [[ \$item = "\${prefix}"* ]]; then
            COMPREPLY+=("\$item")
        fi
    done
    __ltrim_colon_completions "\$prefix"
    return 0
}
"
    complete -o nospace -F _complgen_jit_$stem "$stem"
    unset stem
done
adaszko commented 1 year ago

Thanks for reporting!

Ordoviz commented 1 year ago

Still broken: mygrep --color=n<TAB> completes to mygrep --color=--color=never.

I have built using cargo build from latest commit and tested with bash --noprofile --norc -i; source /usr/share/bash-completion/bash_completion.

adaszko commented 1 year ago

Take another look please, it should work better now: https://github.com/adaszko/complgen/blob/4927e628c54244114a7d9d691a125333f9eeaafc/README.md#bash-integration

Ordoviz commented 1 year ago

We are getting there. These autocomplete correctly now:

mygrep <TAB>
mygrep --color=<TAB>
mygrep --color=a<TAB>
mygrep --color=al<TAB>

but mygrep --color completes to mygrep =

adaszko commented 12 months ago

mygrep --color<TAB> works on my end:

image

To be clear, I'm talking about the JIT mode, with Bash and complgen at commit 4927e628c54244114a7d9d691a125333f9eeaafc. Perhaps you're at some older commit?

Ordoviz commented 12 months ago

I was using this grammar: https://github.com/adaszko/complgen/blob/4927e628c54244114a7d9d691a125333f9eeaafc/e2e/bash/test_bash_shell_integration.py#L61-L62

Changing it to

mygrep --color=<WHEN>;
<WHEN> ::= always | never | auto;

fixes the bug.

adaszko commented 11 months ago

Check again please. The issue was Bash weirdness to do with handling $COMP_WORDBREAKS characters in completions (= and : are among which). Special filtering needs to be performed then to get correct results. I did that for shell script output in the past but seem to have forgotten about it for the JIT mode.

Ordoviz commented 11 months ago

Tested again. Now it's fixed!