newCompleter creates an autocompletion function for a set of commands.
(fns []string)
| 343 | |
| 344 | // newCompleter creates an autocompletion function for a set of commands. |
| 345 | func newCompleter(fns []string) func(string) string { |
| 346 | return func(line string) string { |
| 347 | switch tokens := strings.Fields(line); len(tokens) { |
| 348 | case 0: |
| 349 | // Nothing to complete |
| 350 | case 1: |
| 351 | // Single token -- complete command name |
| 352 | if match := matchVariableOrCommand(tokens[0]); match != "" { |
| 353 | return match |
| 354 | } |
| 355 | case 2: |
| 356 | if tokens[0] == "help" { |
| 357 | if match := matchVariableOrCommand(tokens[1]); match != "" { |
| 358 | return tokens[0] + " " + match |
| 359 | } |
| 360 | return line |
| 361 | } |
| 362 | fallthrough |
| 363 | default: |
| 364 | // Multiple tokens -- complete using functions, except for tags |
| 365 | if cmd := pprofCommands[tokens[0]]; cmd != nil && tokens[0] != "tags" { |
| 366 | lastTokenIdx := len(tokens) - 1 |
| 367 | lastToken := tokens[lastTokenIdx] |
| 368 | if strings.HasPrefix(lastToken, "-") { |
| 369 | lastToken = "-" + functionCompleter(lastToken[1:], fns) |
| 370 | } else { |
| 371 | lastToken = functionCompleter(lastToken, fns) |
| 372 | } |
| 373 | return strings.Join(append(tokens[:lastTokenIdx], lastToken), " ") |
| 374 | } |
| 375 | } |
| 376 | return line |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // matchVariableOrCommand attempts to match a string token to the prefix of a Command. |
| 381 | func matchVariableOrCommand(token string) string { |
searching dependent graphs…