click-contrib / click-completion

Add or enhance bash, fish, zsh and powershell completion in Click
MIT License
288 stars 32 forks source link

completion with "inner" substrings #14

Closed kontrafiktion closed 6 years ago

kontrafiktion commented 6 years ago

Fixes #13

Konubinix commented 6 years ago

Thank you for the PR. As far as I am concerned, this is ok. I think bash-complete doesn't mind this change for it already checks the completions start with the prefix. Just waiting for @glehmann to discuss further before merging, just in case.

Konubinix commented 6 years ago

I was wrong. Bash completion wont filter out the completions that don't start with the prefix.

Actually, we already have a way to provide completion customization in the init fonction of click_completion. I am realizing we did not make Choice use those.

IMHO, the fix would be to first make Choice use the click_completion matching algorithm, and then extend this matching algorithm to make it work with substring matching.

Konubinix commented 6 years ago

I am giving it a shot right now :-)

Konubinix commented 6 years ago

Actually, click_completion already provide you with this customization. With the current master, simply provide this in the beginning of your application.

from click_completion import init
def match_incomplete(choice, incomplete):
    return incomplete in choice
init(match_incomplete=match_incomplete)

This won't impact only Choice, but all the completions, giving IMHO a better look and feel.

If you want only Choice to have a custom completion, then I suggest you create a new ParamType with its custom completion:

class MyChoice(click.Choice):
    def complete(self, ctx, incomplete):
        return [c for c in self.choices if incomplete in c]
Konubinix commented 6 years ago

@kontrafiktion How did it go?

kontrafiktion commented 6 years ago

Works for me. You can forget my PR