Path: blob/main/vendor/github.com/chzyer/readline/complete_segment.go
2875 views
package readline12type SegmentCompleter interface {3// a4// |- a15// |--- a116// |- a27// b8// input:9// DoTree([], 0) [a, b]10// DoTree([a], 1) [a]11// DoTree([a, ], 0) [a1, a2]12// DoTree([a, a], 1) [a1, a2]13// DoTree([a, a1], 2) [a1]14// DoTree([a, a1, ], 0) [a11]15// DoTree([a, a1, a], 1) [a11]16DoSegment([][]rune, int) [][]rune17}1819type dumpSegmentCompleter struct {20f func([][]rune, int) [][]rune21}2223func (d *dumpSegmentCompleter) DoSegment(segment [][]rune, n int) [][]rune {24return d.f(segment, n)25}2627func SegmentFunc(f func([][]rune, int) [][]rune) AutoCompleter {28return &SegmentComplete{&dumpSegmentCompleter{f}}29}3031func SegmentAutoComplete(completer SegmentCompleter) *SegmentComplete {32return &SegmentComplete{33SegmentCompleter: completer,34}35}3637type SegmentComplete struct {38SegmentCompleter39}4041func RetSegment(segments [][]rune, cands [][]rune, idx int) ([][]rune, int) {42ret := make([][]rune, 0, len(cands))43lastSegment := segments[len(segments)-1]44for _, cand := range cands {45if !runes.HasPrefix(cand, lastSegment) {46continue47}48ret = append(ret, cand[len(lastSegment):])49}50return ret, idx51}5253func SplitSegment(line []rune, pos int) ([][]rune, int) {54segs := [][]rune{}55lastIdx := -156line = line[:pos]57pos = 058for idx, l := range line {59if l == ' ' {60pos = 061segs = append(segs, line[lastIdx+1:idx])62lastIdx = idx63} else {64pos++65}66}67segs = append(segs, line[lastIdx+1:])68return segs, pos69}7071func (c *SegmentComplete) Do(line []rune, pos int) (newLine [][]rune, offset int) {7273segment, idx := SplitSegment(line, pos)7475cands := c.DoSegment(segment, idx)76newLine, offset = RetSegment(segment, cands, idx)77for idx := range newLine {78newLine[idx] = append(newLine[idx], ' ')79}80return newLine, offset81}828384