Skip to content

Commit f1b8fe1

Browse files
committed
Simpler top-level context detection for fish completions.
Previously, completions checked against all levels of subcommands. This change has it look only for top-level commands, which must necessarily be present before their children. It also adds hidden top-level commands to the list.
1 parent 53ad542 commit f1b8fe1

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

fish.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,45 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
3030
if err != nil {
3131
return err
3232
}
33-
allCommands := []string{}
3433

3534
// Add global flags
36-
completions := cmd.prepareFishFlags(cmd.VisibleFlags(), allCommands)
35+
completions := cmd.prepareFishFlags(cmd.VisibleFlags(), []string{})
3736

3837
// Add help flag
3938
if !cmd.HideHelp {
4039
completions = append(
4140
completions,
42-
cmd.prepareFishFlags([]Flag{HelpFlag}, allCommands)...,
41+
cmd.prepareFishFlags([]Flag{HelpFlag}, []string{})...,
4342
)
4443
}
4544

4645
// Add version flag
4746
if !cmd.HideVersion {
4847
completions = append(
4948
completions,
50-
cmd.prepareFishFlags([]Flag{VersionFlag}, allCommands)...,
49+
cmd.prepareFishFlags([]Flag{VersionFlag}, []string{})...,
5150
)
5251
}
5352

5453
// Add commands and their flags
5554
completions = append(
5655
completions,
57-
cmd.prepareFishCommands(cmd.VisibleCommands(), &allCommands, []string{})...,
56+
cmd.prepareFishCommands(cmd.VisibleCommands(), []string{})...,
5857
)
5958

59+
toplevelCommandNames := []string{}
60+
for _, child := range cmd.Commands {
61+
toplevelCommandNames = append(toplevelCommandNames, child.Names()...)
62+
}
63+
6064
return t.ExecuteTemplate(w, name, &fishCommandCompletionTemplate{
6165
Command: cmd,
6266
Completions: completions,
63-
AllCommands: allCommands,
67+
AllCommands: toplevelCommandNames,
6468
})
6569
}
6670

67-
func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]string, previousCommands []string) []string {
71+
func (cmd *Command) prepareFishCommands(commands []*Command, previousCommands []string) []string {
6872
completions := []string{}
6973
for _, command := range commands {
7074
var completion strings.Builder
@@ -88,7 +92,6 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
8892
)
8993
}
9094

91-
*allCommands = append(*allCommands, command.Names()...)
9295
completions = append(completions, completion.String())
9396
completions = append(
9497
completions,
@@ -100,7 +103,7 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
100103
completions = append(
101104
completions,
102105
cmd.prepareFishCommands(
103-
command.Commands, allCommands, command.Names(),
106+
command.Commands, command.Names(),
104107
)...,
105108
)
106109
}

testdata/expected-fish-full.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function __fish_greet_no_subcommand --description 'Test if there has been any subcommand yet'
44
for i in (commandline -opc)
5-
if contains -- $i config c sub-config s ss info i in some-command usage u sub-usage su
5+
if contains -- $i config c info i in some-command hidden-command usage u
66
return 1
77
end
88
end

0 commit comments

Comments
 (0)