MCPcopy Create free account
hub / github.com/google/pprof / newCompleter

Function newCompleter

internal/driver/interactive.go:345–378  ·  view source on GitHub ↗

newCompleter creates an autocompletion function for a set of commands.

(fns []string)

Source from the content-addressed store, hash-verified

343
344// newCompleter creates an autocompletion function for a set of commands.
345func 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.
381func matchVariableOrCommand(token string) string {

Callers 2

interactiveFunction · 0.85
TestAutoCompleteFunction · 0.85

Calls 2

matchVariableOrCommandFunction · 0.85
functionCompleterFunction · 0.85

Tested by 1

TestAutoCompleteFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…