-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
fish_buff.go
134 lines (109 loc) · 2.81 KB
/
fish_buff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package model
import (
"sort"
)
// SuggestionListener listens for suggestions.
type SuggestionListener interface {
BuffWatcher
// SuggestionChanged notifies suggestion changes.
SuggestionChanged(text, sugg string)
}
// SuggestionFunc produces suggestions.
type SuggestionFunc func(text string) sort.StringSlice
// FishBuff represents a suggestion buffer.
type FishBuff struct {
*CmdBuff
suggestionFn SuggestionFunc
suggestions []string
suggestionIndex int
}
// NewFishBuff returns a new command buffer.
func NewFishBuff(key rune, kind BufferKind) *FishBuff {
return &FishBuff{
CmdBuff: NewCmdBuff(key, kind),
suggestionIndex: -1,
}
}
// PrevSuggestion returns the prev suggestion.
func (f *FishBuff) PrevSuggestion() (string, bool) {
if len(f.suggestions) == 0 {
return "", false
}
if f.suggestionIndex < 0 {
f.suggestionIndex = 0
} else {
f.suggestionIndex--
}
if f.suggestionIndex < 0 {
f.suggestionIndex = len(f.suggestions) - 1
}
return f.suggestions[f.suggestionIndex], true
}
// NextSuggestion returns the next suggestion.
func (f *FishBuff) NextSuggestion() (string, bool) {
if len(f.suggestions) == 0 {
return "", false
}
if f.suggestionIndex < 0 {
f.suggestionIndex = 0
} else {
f.suggestionIndex++
}
if f.suggestionIndex >= len(f.suggestions) {
f.suggestionIndex = 0
}
return f.suggestions[f.suggestionIndex], true
}
// ClearSuggestions clear out all suggestions.
func (f *FishBuff) ClearSuggestions() {
f.suggestionIndex = -1
}
// CurrentSuggestion returns the current suggestion.
func (f *FishBuff) CurrentSuggestion() (string, bool) {
if len(f.suggestions) == 0 || f.suggestionIndex < 0 || f.suggestionIndex >= len(f.suggestions) {
return "", false
}
return f.suggestions[f.suggestionIndex], true
}
// AutoSuggests returns true if model implements auto suggestions.
func (f *FishBuff) AutoSuggests() bool {
return true
}
// Suggestions returns suggestions.
func (f *FishBuff) Suggestions() []string {
if f.suggestionFn != nil {
return f.suggestionFn(string(f.buff))
}
return nil
}
// SetSuggestionFn sets up suggestions.
func (f *FishBuff) SetSuggestionFn(fn SuggestionFunc) {
f.suggestionFn = fn
}
// Notify publish suggestions to all listeners.
func (f *FishBuff) Notify(delete bool) {
if f.suggestionFn == nil {
return
}
f.fireSuggestionChanged(f.suggestionFn(string(f.buff)))
}
// Add adds a new character to the buffer.
func (f *FishBuff) Add(r rune) {
f.CmdBuff.Add(r)
f.Notify(false)
}
// Delete removes the last character from the buffer.
func (f *FishBuff) Delete() {
f.CmdBuff.Delete()
f.Notify(true)
}
func (f *FishBuff) fireSuggestionChanged(ss []string) {
f.suggestions, f.suggestionIndex = ss, 0
var suggest string
if len(ss) == 0 {
f.suggestionIndex = -1
} else {
suggest = ss[f.suggestionIndex]
}
f.SetText(f.GetText(), suggest)
}