Skip to content

zsh completion does not handle ShellCompDirectiveNoSpace properly #1211

@marckhouzam

Description

@marckhouzam

While working on adding descriptions to the custom completions for helm, I ran into two bugs with zsh and ShellCompDirectiveNoSpace.

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
	},

Bug 1: NoSpace is not respected
If I run in zsh:

$ ./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:

elif [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ] && [ ${compCount} -eq 1 ]; then

But my ValidArgsFunction does not filter the completion but instead returns all of them, leaving zsh 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. But this is allowed.

Bug 2: Descriptions are not handled with ShellCompDirectiveNoSpace
The zsh script was using compadd directly to handle ShellCompDirectiveNoSpace:

compadd -S '' "${lastComp}"

The problem is that compadd does not handle descriptions and just prints them as part of the completion. So, in my case:

$ ./testprog nospace when w<TAB>

will complete to:

./testprog nospace when why:The\ reason

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