Skip to content

Commit 0c26c26

Browse files
committed
Better autocomplete to repl
1 parent a2a2ff4 commit 0c26c26

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

repl/repl.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import (
1111
)
1212

1313
func main() {
14-
items := []readline.PrefixCompleterInterface{}
15-
for _, name := range builtin.Names {
16-
items = append(items, readline.PcItem(name))
14+
extra := []string{
15+
"exit",
16+
"opcodes",
17+
"map",
18+
"filter",
19+
"all",
20+
"any",
21+
"none",
22+
"one",
1723
}
18-
items = append(items, readline.PcItem("exit"))
19-
items = append(items, readline.PcItem("opcodes"))
2024
rl, err := readline.NewEx(&readline.Config{
2125
Prompt: "> ",
22-
AutoComplete: readline.NewPrefixCompleter(items...),
26+
AutoComplete: completer{append(builtin.Names, extra...)},
2327
})
2428
if err != nil {
2529
panic(err)
@@ -58,3 +62,26 @@ func main() {
5862
fmt.Println(output)
5963
}
6064
}
65+
66+
type completer struct {
67+
words []string
68+
}
69+
70+
func (c completer) Do(line []rune, pos int) ([][]rune, int) {
71+
var lastWord string
72+
for i := pos - 1; i >= 0; i-- {
73+
if line[i] == ' ' {
74+
break
75+
}
76+
lastWord = string(line[i]) + lastWord
77+
}
78+
79+
var words [][]rune
80+
for _, word := range c.words {
81+
if strings.HasPrefix(word, lastWord) {
82+
words = append(words, []rune(strings.TrimPrefix(word, lastWord)))
83+
}
84+
}
85+
86+
return words, len(lastWord)
87+
}

0 commit comments

Comments
 (0)