Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
waivek opened this issue May 21, 2024 · 1 comment

Comments

@waivek
Copy link

waivek commented May 21, 2024

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
Copy link
Collaborator

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:

fishcode = r"""
function __fish_%(function_name)s_complete
set -x _ARGCOMPLETE 1
set -x _ARGCOMPLETE_DFS \t
set -x _ARGCOMPLETE_IFS \n
set -x _ARGCOMPLETE_SUPPRESS_SPACE 1
set -x _ARGCOMPLETE_SHELL fish
set -x COMP_LINE (commandline -p)
set -x COMP_POINT (string length (commandline -cp))
set -x COMP_TYPE
if set -q _ARC_DEBUG
%(argcomplete_script)s 8>&1 9>&2 1>&9 2>&1
else
%(argcomplete_script)s 8>&1 9>&2 1>/dev/null 2>&1
end
end
complete %(completion_arg)s %(executable)s -f -a '(__fish_%(function_name)s_complete)'
"""

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants