Skip to content

Commit 4ec67cb

Browse files
committed
Add aliases in autocompletion
1 parent 8b8aa74 commit 4ec67cb

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Cmd struct {
2626
// By default all commands get autocomplete of
2727
// subcommands.
2828
// A non-nil Completer overrides the default behaviour.
29-
Completer func(args []string) []string
29+
Completer func(cmd *Cmd, args []string, prefix string) []string
3030

3131
// subcommands.
3232
children map[string]*Cmd

completer.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package ishell
33
import (
44
"strings"
55

6-
"github.com/flynn-archive/go-shlex"
6+
shlex "github.com/flynn-archive/go-shlex"
77
)
88

99
type iCompleter struct {
@@ -23,14 +23,12 @@ func (ic iCompleter) Do(line []rune, pos int) (newLine [][]rune, length int) {
2323
words = strings.Fields(string(line))
2424
}
2525

26-
var cWords []string
2726
prefix := ""
2827
if len(words) > 0 && pos > 0 && line[pos-1] != ' ' {
2928
prefix = words[len(words)-1]
30-
cWords = ic.getWords(words[:len(words)-1])
31-
} else {
32-
cWords = ic.getWords(words)
29+
words = words[:len(words)-1]
3330
}
31+
cWords := ic.getWords(words, prefix)
3432

3533
var suggestions [][]rune
3634
for _, w := range cWords {
@@ -44,16 +42,23 @@ func (ic iCompleter) Do(line []rune, pos int) (newLine [][]rune, length int) {
4442
return suggestions, len(prefix)
4543
}
4644

47-
func (ic iCompleter) getWords(w []string) (s []string) {
45+
func (ic iCompleter) getWords(w []string, prefix string) (s []string) {
4846
cmd, args := ic.cmd.FindCmd(w)
4947
if cmd == nil {
5048
cmd, args = ic.cmd, w
5149
}
5250
if cmd.Completer != nil {
53-
return cmd.Completer(args)
51+
return cmd.Completer(cmd, args, prefix)
5452
}
55-
for k := range cmd.children {
53+
for k, child := range cmd.children {
5654
s = append(s, k)
55+
56+
// add aliases when command name won't match
57+
if !strings.HasPrefix(k, prefix) {
58+
for _, a := range child.Aliases {
59+
s = append(s, a)
60+
}
61+
}
5762
}
5863
return
5964
}

0 commit comments

Comments
 (0)