diff --git a/fish_completions.go b/fish_completions.go index 4dd9b840..2a5ec19a 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -34,6 +34,11 @@ function __fish_%s_no_subcommand --description 'Test if %s has yet to be given t end return 0 end +function __fish_%s_seen_subcommand_path --description 'Test whether the full path of subcommands is the current path' + set -l cmd (commandline -opc) + set -e cmd[1] + return (test (string trim -- "$argv") = (string trim -- "$cmd")) +end function __fish_%s_has_flag for i in (commandline -opc) if contains -- "--$1" $i @@ -42,7 +47,7 @@ function __fish_%s_has_flag end return 1 end -`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name())) +`, cmd.Name(), cmd.Name(), strings.Join(subCommandNames, " "), cmd.Name(), cmd.Name())) } func writeFishCommandCompletion(rootCmd, cmd *Command, buf *bytes.Buffer) { @@ -120,7 +125,7 @@ func commandCompletionCondition(rootCmd, cmd *Command) string { localNonPersistentFlags := cmd.LocalNonPersistentFlags() bareConditions := []string{} if rootCmd != cmd { - bareConditions = append(bareConditions, fmt.Sprintf("__fish_seen_subcommand_from %s", subCommandPath(rootCmd, cmd))) + bareConditions = append(bareConditions, fmt.Sprintf("__fish_%s_seen_subcommand_path %s", rootCmd.Name(), subCommandPath(rootCmd, cmd))) } else { bareConditions = append(bareConditions, fmt.Sprintf("__fish_%s_no_subcommand", rootCmd.Name())) } @@ -133,7 +138,7 @@ func commandCompletionCondition(rootCmd, cmd *Command) string { func completionCondition(rootCmd, cmd *Command) string { condition := fmt.Sprintf("-n '__fish_%s_no_subcommand'", rootCmd.Name()) if rootCmd != cmd { - condition = fmt.Sprintf("-n '__fish_seen_subcommand_from %s'", subCommandPath(rootCmd, cmd)) + condition = fmt.Sprintf("-n '__fish_%s_seen_subcommand_path %s'", rootCmd.Name(), subCommandPath(rootCmd, cmd)) } return condition } diff --git a/fish_completions_test.go b/fish_completions_test.go index e85c86c8..fc65dfd9 100644 --- a/fish_completions_test.go +++ b/fish_completions_test.go @@ -88,6 +88,7 @@ func TestFishCompletions(t *testing.T) { // check for preamble helper functions check(t, output, "__fish_root_no_subcommand") check(t, output, "__fish_root_has_flag") + check(t, output, "__fish_root_seen_subcommand_path") // check for subcommands check(t, output, "-a echo") @@ -96,21 +97,21 @@ func TestFishCompletions(t *testing.T) { check(t, output, "-a cmd:colon") // check for nested subcommands - checkRegex(t, output, `-n '__fish_seen_subcommand_from echo(; and[^']*)?' -a times`) + checkRegex(t, output, `-n '__fish_root_seen_subcommand_path echo(; and[^']*)?' -a times`) // check for flags that will take arguments check(t, output, "-n '__fish_root_no_subcommand' -r -s i -l introot") check(t, output, "-n '__fish_root_no_subcommand' -r -l filename") check(t, output, "-n '__fish_root_no_subcommand' -r -l persistent-filename") check(t, output, "-n '__fish_root_no_subcommand' -r -l theme") - check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l config") - check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l filename") + check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l config") + check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l filename") // check for persistent flags that will take arguments - check(t, output, "-n '__fish_seen_subcommand_from cmd:colon' -r -l persistent-filename") - check(t, output, "-n '__fish_seen_subcommand_from echo' -r -l persistent-filename") - check(t, output, "-n '__fish_seen_subcommand_from echo times' -r -l persistent-filename") - check(t, output, "-n '__fish_seen_subcommand_from print' -r -l persistent-filename") + check(t, output, "-n '__fish_root_seen_subcommand_path cmd:colon' -r -l persistent-filename") + check(t, output, "-n '__fish_root_seen_subcommand_path echo' -r -l persistent-filename") + check(t, output, "-n '__fish_root_seen_subcommand_path echo times' -r -l persistent-filename") + check(t, output, "-n '__fish_root_seen_subcommand_path print' -r -l persistent-filename") // check for positional arguments to a command checkRegex(t, output, `-n '__fish_root_no_subcommand(; and[^']*)?' -a pod`)