Skip to content

Commit

Permalink
pkg/terminal: Add auto-complete for local variables (#3004)
Browse files Browse the repository at this point in the history
* pkg/terminal: fix typo in loadConfig comment

* pkg/terminal: add auto-complete for local variables

Fixes #2944

* pkg/terminal: use an empty config to load local vars

* pkg/terminal: cache loaded local vars until next command

* pkg/terminal: lazy loading of local vars trie
  • Loading branch information
pippolo84 authored May 18, 2022
1 parent c31040b commit 928e34d
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pkg/terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ func (t *Term) Run() (int, error) {
}
}

var locs *trie.Trie

t.line.SetCompleter(func(line string) (c []string) {
cmd := t.cmds.Find(strings.Split(line, " ")[0], noPrefix)
switch cmd.aliases[0] {
Expand All @@ -243,6 +245,30 @@ func (t *Term) Run() (int, error) {
case "nullcmd", "nocmd":
commands := cmds.FuzzySearch(strings.ToLower(line))
c = append(c, commands...)
case "print", "whatis":
if locs == nil {
localVars, err := t.client.ListLocalVariables(
api.EvalScope{GoroutineID: -1, Frame: t.cmds.frame, DeferredCall: 0},
api.LoadConfig{},
)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to get local variables: %s\n", err)
break
}

locs = trie.New()
for _, loc := range localVars {
locs.Add(loc.Name, nil)
}
}

if spc := strings.LastIndex(line, " "); spc > 0 {
prefix := line[:spc] + " "
locals := locs.FuzzySearch(line[spc+1:])
for _, l := range locals {
c = append(c, prefix+l)
}
}
}
return
})
Expand Down Expand Up @@ -279,6 +305,8 @@ func (t *Term) Run() (int, error) {
_, _ = t.client.GetState()

for {
locs = nil

cmdstr, err := t.promptForInput()
if err != nil {
if err == io.EOF {
Expand Down Expand Up @@ -460,7 +488,7 @@ func (t *Term) handleExit() (int, error) {
return 0, nil
}

// loadConfig returns an api.LoadConfig with the parameterss specified in
// loadConfig returns an api.LoadConfig with the parameters specified in
// the configuration file.
func (t *Term) loadConfig() api.LoadConfig {
r := api.LoadConfig{FollowPointers: true, MaxVariableRecurse: 1, MaxStringLen: 64, MaxArrayValues: 64, MaxStructFields: -1}
Expand Down

0 comments on commit 928e34d

Please sign in to comment.