|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "sort" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/alecthomas/kong" |
| 8 | +) |
| 9 | + |
| 10 | +type completionFlag struct { |
| 11 | + takesValue bool |
| 12 | +} |
| 13 | + |
| 14 | +type completionNode struct { |
| 15 | + children map[string]*completionNode |
| 16 | + flags map[string]completionFlag |
| 17 | +} |
| 18 | + |
| 19 | +func completeWords(cword int, words []string) ([]string, error) { |
| 20 | + if len(words) == 0 { |
| 21 | + return nil, nil |
| 22 | + } |
| 23 | + parser, _, err := newParser(baseDescription()) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + root := buildCompletionNode(parser.Model.Node) |
| 28 | + |
| 29 | + if cword < 0 { |
| 30 | + cword = len(words) - 1 |
| 31 | + } |
| 32 | + if cword < 0 { |
| 33 | + return nil, nil |
| 34 | + } |
| 35 | + if cword > len(words) { |
| 36 | + cword = len(words) |
| 37 | + } |
| 38 | + |
| 39 | + start := 0 |
| 40 | + if isProgramName(words[0]) { |
| 41 | + start = 1 |
| 42 | + } |
| 43 | + |
| 44 | + node := root |
| 45 | + terminatorIndex := -1 |
| 46 | + for i := start; i < cword && i < len(words); { |
| 47 | + word := words[i] |
| 48 | + if word == "--" { |
| 49 | + terminatorIndex = i |
| 50 | + break |
| 51 | + } |
| 52 | + if strings.HasPrefix(word, "-") { |
| 53 | + flagToken, hasValue := splitFlagToken(word) |
| 54 | + if hasValue { |
| 55 | + i++ |
| 56 | + continue |
| 57 | + } |
| 58 | + if spec, ok := node.flags[flagToken]; ok && spec.takesValue { |
| 59 | + if i+1 == cword { |
| 60 | + return nil, nil |
| 61 | + } |
| 62 | + i += 2 |
| 63 | + continue |
| 64 | + } |
| 65 | + i++ |
| 66 | + continue |
| 67 | + } |
| 68 | + if child, ok := node.children[word]; ok { |
| 69 | + node = child |
| 70 | + i++ |
| 71 | + continue |
| 72 | + } |
| 73 | + i++ |
| 74 | + } |
| 75 | + |
| 76 | + if terminatorIndex != -1 && cword >= terminatorIndex { |
| 77 | + return nil, nil |
| 78 | + } |
| 79 | + |
| 80 | + if cword < len(words) && words[cword] == "--" { |
| 81 | + return nil, nil |
| 82 | + } |
| 83 | + |
| 84 | + if cword > start && cword <= len(words) { |
| 85 | + prev := words[cword-1] |
| 86 | + if strings.HasPrefix(prev, "-") { |
| 87 | + flagToken, hasValue := splitFlagToken(prev) |
| 88 | + if hasValue { |
| 89 | + return nil, nil |
| 90 | + } |
| 91 | + if spec, ok := node.flags[flagToken]; ok && spec.takesValue { |
| 92 | + return nil, nil |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + current := "" |
| 98 | + if cword < len(words) { |
| 99 | + current = words[cword] |
| 100 | + } |
| 101 | + |
| 102 | + suggestions := make([]string, 0) |
| 103 | + if strings.HasPrefix(current, "-") { |
| 104 | + suggestions = append(suggestions, matchingFlags(node, current)...) |
| 105 | + } else { |
| 106 | + suggestions = append(suggestions, matchingCommands(node, current)...) |
| 107 | + suggestions = append(suggestions, matchingFlags(node, current)...) |
| 108 | + } |
| 109 | + sort.Strings(suggestions) |
| 110 | + return suggestions, nil |
| 111 | +} |
| 112 | + |
| 113 | +func isProgramName(word string) bool { |
| 114 | + if word == "gog" { |
| 115 | + return true |
| 116 | + } |
| 117 | + if strings.HasSuffix(word, "/gog") || strings.HasSuffix(word, `\gog`) { |
| 118 | + return true |
| 119 | + } |
| 120 | + if strings.HasSuffix(word, "/gog.exe") || strings.HasSuffix(word, `\gog.exe`) { |
| 121 | + return true |
| 122 | + } |
| 123 | + return false |
| 124 | +} |
| 125 | + |
| 126 | +func buildCompletionNode(node *kong.Node) *completionNode { |
| 127 | + current := &completionNode{ |
| 128 | + children: make(map[string]*completionNode), |
| 129 | + flags: make(map[string]completionFlag), |
| 130 | + } |
| 131 | + |
| 132 | + for _, group := range node.AllFlags(true) { |
| 133 | + for _, flag := range group { |
| 134 | + addFlagTokens(current.flags, flag) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + for _, child := range node.Children { |
| 139 | + if child.Hidden { |
| 140 | + continue |
| 141 | + } |
| 142 | + childNode := buildCompletionNode(child) |
| 143 | + for _, name := range append([]string{child.Name}, child.Aliases...) { |
| 144 | + if name == "" { |
| 145 | + continue |
| 146 | + } |
| 147 | + if _, exists := current.children[name]; !exists { |
| 148 | + current.children[name] = childNode |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return current |
| 154 | +} |
| 155 | + |
| 156 | +func addFlagTokens(flags map[string]completionFlag, flag *kong.Flag) { |
| 157 | + takesValue := !(flag.IsBool() || flag.IsCounter()) |
| 158 | + addFlag(flags, "--"+flag.Name, takesValue) |
| 159 | + for _, alias := range flag.Aliases { |
| 160 | + addFlag(flags, "--"+alias, takesValue) |
| 161 | + } |
| 162 | + if flag.Short != 0 { |
| 163 | + addFlag(flags, "-"+string(flag.Short), takesValue) |
| 164 | + } |
| 165 | + if negated := negatedFlagName(flag); negated != "" { |
| 166 | + addFlag(flags, negated, false) |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func negatedFlagName(flag *kong.Flag) string { |
| 171 | + switch flag.Tag.Negatable { |
| 172 | + case "": |
| 173 | + return "" |
| 174 | + case "_": |
| 175 | + return "--no-" + flag.Name |
| 176 | + default: |
| 177 | + return "--" + flag.Tag.Negatable |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +func addFlag(flags map[string]completionFlag, token string, takesValue bool) { |
| 182 | + if token == "" { |
| 183 | + return |
| 184 | + } |
| 185 | + if _, exists := flags[token]; exists { |
| 186 | + return |
| 187 | + } |
| 188 | + flags[token] = completionFlag{takesValue: takesValue} |
| 189 | +} |
| 190 | + |
| 191 | +func splitFlagToken(word string) (string, bool) { |
| 192 | + if idx := strings.Index(word, "="); idx != -1 { |
| 193 | + return word[:idx], true |
| 194 | + } |
| 195 | + return word, false |
| 196 | +} |
| 197 | + |
| 198 | +func matchingCommands(node *completionNode, prefix string) []string { |
| 199 | + results := make([]string, 0, len(node.children)) |
| 200 | + for name := range node.children { |
| 201 | + if strings.HasPrefix(name, prefix) { |
| 202 | + results = append(results, name) |
| 203 | + } |
| 204 | + } |
| 205 | + return results |
| 206 | +} |
| 207 | + |
| 208 | +func matchingFlags(node *completionNode, prefix string) []string { |
| 209 | + results := make([]string, 0, len(node.flags)) |
| 210 | + for name := range node.flags { |
| 211 | + if strings.HasPrefix(name, prefix) { |
| 212 | + results = append(results, name) |
| 213 | + } |
| 214 | + } |
| 215 | + return results |
| 216 | +} |
0 commit comments