readline: Automatically do word completion based on history
This commit is contained in:
@@ -560,18 +560,18 @@ func (self *Command) Exec(args ...string) {
|
||||
}
|
||||
|
||||
func (self *Command) GetCompletions(argv []string, init_completions func(*Completions)) *Completions {
|
||||
ans := Completions{Groups: make([]*MatchGroup, 0, 4)}
|
||||
ans := NewCompletions()
|
||||
if init_completions != nil {
|
||||
init_completions(&ans)
|
||||
init_completions(ans)
|
||||
}
|
||||
if len(argv) > 0 {
|
||||
exe := argv[0]
|
||||
cmd := self.FindSubCommand(exe)
|
||||
if cmd != nil {
|
||||
if cmd.ParseArgsForCompletion != nil {
|
||||
cmd.ParseArgsForCompletion(cmd, argv[1:], &ans)
|
||||
cmd.ParseArgsForCompletion(cmd, argv[1:], ans)
|
||||
} else {
|
||||
completion_parse_args(cmd, argv[1:], &ans)
|
||||
completion_parse_args(cmd, argv[1:], ans)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -582,5 +582,5 @@ func (self *Command) GetCompletions(argv []string, init_completions func(*Comple
|
||||
}
|
||||
}
|
||||
ans.Groups = non_empty_groups
|
||||
return &ans
|
||||
return ans
|
||||
}
|
||||
|
||||
@@ -115,12 +115,46 @@ type Completions struct {
|
||||
split_on_equals bool // true if the cmdline is split on = (BASH does this because readline does this)
|
||||
}
|
||||
|
||||
func NewCompletions() *Completions {
|
||||
return &Completions{Groups: make([]*MatchGroup, 0, 4)}
|
||||
}
|
||||
|
||||
func (self *Completions) AddPrefixToAllMatches(prefix string) {
|
||||
for _, mg := range self.Groups {
|
||||
mg.AddPrefixToAllMatches(prefix)
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Completions) MergeMatchGroup(mg *MatchGroup) {
|
||||
if len(mg.Matches) == 0 {
|
||||
return
|
||||
}
|
||||
var dest *MatchGroup
|
||||
for _, q := range self.Groups {
|
||||
if q.Title == mg.Title {
|
||||
dest = q
|
||||
break
|
||||
}
|
||||
}
|
||||
if dest == nil {
|
||||
dest = self.AddMatchGroup(mg.Title)
|
||||
dest.NoTrailingSpace = mg.NoTrailingSpace
|
||||
dest.IsFiles = mg.IsFiles
|
||||
}
|
||||
seen := utils.NewSet[string](64)
|
||||
for _, q := range self.Groups {
|
||||
for _, m := range q.Matches {
|
||||
seen.Add(m.Word)
|
||||
}
|
||||
}
|
||||
for _, m := range mg.Matches {
|
||||
if !seen.Has(m.Word) {
|
||||
seen.Add(m.Word)
|
||||
dest.Matches = append(dest.Matches, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (self *Completions) AddMatchGroup(title string) *MatchGroup {
|
||||
for _, q := range self.Groups {
|
||||
if q.Title == title {
|
||||
|
||||
Reference in New Issue
Block a user