kislyuk / argcomplete

Python and tab completion, better together.
https://kislyuk.github.io/argcomplete/
Apache License 2.0
1.39k stars 129 forks source link

Inconsistent completion when using a custom validator between `fish` and `bash` #487

Open waivek opened 1 month ago

waivek commented 1 month ago

Code Sample for Reproduction:

#!/usr/bin/env /home/vivek/pyscripts/.direnv/python-3.10.12/bin/python
# PYTHON_ARGCOMPLETE_OK

import argparse
import argcomplete

def complete_foo(prefix, parsed_args, **kwargs):
    return ['foo', 'foobar', 'foo1', 'foo2', 'red']

def default_validator(completion_candidate, current_input):
    return completion_candidate.startswith(current_input)

def custom_validator(completion_candidate, current_input):
    if current_input == "x":
        return True
    return default_validator(completion_candidate, current_input)

parser = argparse.ArgumentParser()
parser.add_argument('foo').completer = complete_foo # pyright: ignore[reportAttributeAccessIssue]

argcomplete.autocomplete(parser, validator=custom_validator)
args = parser.parse_args()

To register completions on bash, I do

chmod +x
eval "$(register-python-argcomplete mycommand.py)"

And for fish I run

register-python-argcomplete --shell fish /path/to/mycommand.py | source

On bash, when I type ./mycommand.py x<TAB> I get:

image

This is the desired result, that I would also like to get in my fish completion, however on fish I get:

image

Basically, no matches.

Upon more experimentation it seems that the fish completion script seems to be adding it's own startswith type check, thus overriding the custom validator.

Is there a way to modify the generated fish completion script to remove this behavior?

evanunderscore commented 1 month ago

I'm not sure exactly what you mean when you say "the fish completion script seems to be adding it's own startswith type check, thus overriding the custom validator." This is the code we're generating:

https://github.com/kislyuk/argcomplete/blob/6c9e540fdbdf331129a0100c3ed1a88730871634/argcomplete/shell_integration.py#L95-L112

My guess would be fish's own machinery is filtering returned completions by prefix. I'm not familiar enough with fish to know if there's an option to change this. It's possible the shell simply doesn't allow it.