Skip to content

Fish completion does not handle "nospace" or file completion properly #1248

@marckhouzam

Description

@marckhouzam

This is the same bug that is present for zsh and described in #1211 and #1212.

Bug 1: File completion is not respected

The fish completion script performs file completion only if it receives no completions:

if test $numComps -eq 0; and test $nofiles -eq 0

However, it is valid for ValidArgsFunction to return completions that don't apply to the current toComplete prefix. This means file completion should be performed, but the script does not realize it as it still believes it received valid completions.

Say I have the following ValidArgsFunction for command withfile:

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    // We do not consider the toComplete prefix and always return a completion
    return []string{"why"}, cobra.ShellCompDirectiveDefault
},

If I have a file called myfile and run in fish:

$ ./testprog withfile my<TAB>

I would expect to get file completion and get myfile as a completion, but I don't. This is because

$ ./testprog __complete withfile my<ENTER>
why
:0
Completion ended with directive: ShellCompDirectiveDefault

Notice that above, the script would receive a completion even though it is not technically valid. This is allowed. But then, the script does not realize it must do file completion.

Bug 2: NoSpace is not respected all the time

For the exact same reason, ShellCompDirectiveNoSpace also fails when the __complete command returns completions that don't match the toComplete prefix.

Say I have the following ValidArgsFunction for command nospace:

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
		if len(args) == 0 {
			return []string{"when\tThe time", "who\tThe person"}, cobra.ShellCompDirectiveNoSpace
		}
		return []string{"why\tThe reason"}, cobra.ShellCompDirectiveNoSpace
	},

If I run in fish:

$ ./testprog nospace whe<TAB>

a space is added after ./testprog nospace when even though the ShellCompDirectiveNoSpace was given.

This is because the script only handles ShellCompDirectiveNoSpace if a single completion is returned:

if test $numComps -eq 1; and test $nospace -ne 0

But my ValidArgsFunction does not filter the completions but instead returns all of them, leaving fish to filter them. In this case:

$ ./testprog __complete nospace whe<ENTER>
when	The time
who	The person
:2
Completion ended with directive: ShellCompDirectiveNoSpace

Notice that with the result above the completion script would receive two completions even though the who completion is not technically valid. This is allowed.

Bug 3: NoSpace not respected with descriptions

Finally, the nospace logic for fish does not handle the case when there is a description. To achieve "nospace" the script adds a second completion with an extra "." to prevent the shell from adding a space. However if there is a description, the extra "." is added to the description instead of the completion, and the nospace directive does not work.

I will post a PR to handle this better.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions