readline: Automatically do word completion based on history

This commit is contained in:
Kovid Goyal
2023-03-07 16:44:02 +05:30
parent 4cef83ffd0
commit 0e73c01093
5 changed files with 85 additions and 5 deletions

View File

@@ -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 {