-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompletions.go
307 lines (288 loc) · 7.17 KB
/
completions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package senpai
import (
"fmt"
"os"
"path/filepath"
"strings"
"git.sr.ht/~delthas/senpai/irc"
"git.sr.ht/~delthas/senpai/ui"
)
type completionAsync func(e irc.Event) []ui.Completion
func (app *App) completionsChannelMembers(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
var start int
for start = cursorIdx - 1; 0 <= start; start-- {
if text[start] == ' ' {
break
}
}
start++
word := text[start:cursorIdx]
if len(word) == 0 {
return cs
}
netID, buffer := app.win.CurrentBuffer()
s := app.sessions[netID] // is not nil
wordCf := s.Casemap(string(word))
for _, name := range s.Names(buffer) {
if strings.HasPrefix(s.Casemap(name.Name.Name), wordCf) {
nickComp := []rune(name.Name.Name)
if start == 0 {
nickComp = append(nickComp, ':')
}
nickComp = append(nickComp, ' ')
c := make([]rune, len(text)+len(nickComp)-len(word))
copy(c[:start], text[:start])
if cursorIdx < len(text) {
copy(c[start+len(nickComp):], text[cursorIdx:])
}
copy(c[start:], nickComp)
cs = append(cs, ui.Completion{
StartIdx: start,
EndIdx: cursorIdx,
Text: c,
Display: []rune(name.Name.Name),
CursorIdx: start + len(nickComp),
})
}
}
return cs
}
func (app *App) completionsJoin(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
if !hasPrefix(text[:cursorIdx], []rune("/join #")) {
return cs
}
netID, _ := app.win.CurrentBuffer()
s := app.sessions[netID] // is not nil
if s == nil {
return cs
}
if !s.HasListMask() {
return cs
}
post := append([]rune{}, text[cursorIdx:]...)
channel := append([]rune{}, text[6:]...)
if len(channel) < 3 {
// Require at least 2 characters for a search, to avoid triggering large LISTs in completions
return cs
}
s.List(string(channel) + "*")
cs = append(cs, ui.Completion{
Async: completionAsync(func(e irc.Event) []ui.Completion {
l, ok := e.(irc.ListEvent)
if !ok {
return nil
}
cs := make([]ui.Completion, len(l))
for i, e := range l {
text := []rune("/join ")
text = append(text, []rune(e.Channel)...)
text = append(text, post...)
cs[i] = ui.Completion{
StartIdx: 6,
EndIdx: 6 + len([]rune(e.Channel)),
Text: text,
CursorIdx: cursorIdx + len([]rune(e.Channel)) - len(channel),
}
}
return cs
}),
})
return cs
}
func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
if !hasPrefix(text, []rune("/topic ")) {
return cs
}
netID, buffer := app.win.CurrentBuffer()
s := app.sessions[netID] // is not nil
topic, _, _ := s.Topic(buffer)
if cursorIdx == len(text) {
compText := append(text, []rune(topic)...)
cs = append(cs, ui.Completion{
StartIdx: cursorIdx,
EndIdx: cursorIdx,
Text: compText,
CursorIdx: len(compText),
})
}
return cs
}
func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
if !hasPrefix(text, []rune("/msg ")) {
return cs
}
s := app.CurrentSession() // is not nil
// Check if the first word (target) is already written and complete (in
// which case we don't have completions to provide).
var word string
hasMetALetter := false
for i := 5; i < cursorIdx; i += 1 {
if hasMetALetter && text[i] == ' ' {
return cs
}
if !hasMetALetter && text[i] != ' ' {
word = s.Casemap(string(text[i:cursorIdx]))
hasMetALetter = true
}
}
if word == "" {
return cs
}
for _, user := range s.Users() {
if strings.HasPrefix(s.Casemap(user), word) {
nickComp := append([]rune(user), ' ')
c := make([]rune, len(text)+5+len(nickComp)-cursorIdx)
copy(c[:5], []rune("/msg "))
copy(c[5:], nickComp)
if cursorIdx < len(text) {
copy(c[5+len(nickComp):], text[cursorIdx:])
}
cs = append(cs, ui.Completion{
StartIdx: 5,
EndIdx: cursorIdx,
Text: c,
CursorIdx: 5 + len(nickComp),
})
}
}
return cs
}
func (app *App) completionsUpload(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
if !hasPrefix(text, []rune("/upload ")) {
return cs
}
if app.cfg.Transient || !app.cfg.LocalIntegrations {
return cs
}
_, path, ok := strings.Cut(string(text[:cursorIdx]), " ")
if !ok {
return cs
}
dirPath := ""
dirPrefix := ""
if path == "" {
if filepath.Separator != '/' {
return cs
}
dirPath = "/"
} else if strings.HasSuffix(path, string(filepath.Separator)) {
dirPath = path
} else {
dirPath = filepath.Dir(path)
dirPrefix = filepath.Base(path)
}
dir, err := os.ReadDir(dirPath)
if err != nil {
return cs
}
for _, e := range dir {
if !strings.HasPrefix(e.Name(), dirPrefix) {
continue
}
name := e.Name()
var isDir bool
if e.IsDir() {
isDir = true
} else if e.Type() == os.ModeSymlink {
if fi, err := os.Stat(filepath.Join(dirPath, name)); err == nil && fi.IsDir() {
isDir = true
}
}
if isDir {
name += string(filepath.Separator)
}
if path == "" {
name = "/" + name
}
if name == dirPrefix {
continue
}
cs = append(cs, ui.Completion{
StartIdx: cursorIdx - len([]rune(dirPrefix)),
EndIdx: cursorIdx,
Text: []rune(string(text[:cursorIdx]) + name[len(dirPrefix):] + string(text[cursorIdx:])),
CursorIdx: cursorIdx + len([]rune(name)) - len([]rune(dirPrefix)),
})
}
return cs
}
func (app *App) completionsCommands(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
if !hasPrefix(text, []rune("/")) {
return cs
}
for i := 0; i < cursorIdx; i++ {
if text[i] == ' ' {
return cs
}
}
if cursorIdx < len(text) && text[cursorIdx] != ' ' {
return cs
}
uText := strings.ToUpper(string(text[1:cursorIdx]))
for name := range commands {
if strings.HasPrefix(name, uText) {
c := make([]rune, len(text)+len(name)-len(uText))
copy(c[:1], []rune("/"))
copy(c[1:], []rune(strings.ToLower(name)))
copy(c[1+len(name):], text[cursorIdx:])
cs = append(cs, ui.Completion{
StartIdx: 0,
EndIdx: cursorIdx,
Text: c,
CursorIdx: 1 + len(name),
})
}
}
return cs
}
func (app *App) completionsEmoji(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
var start int
for start = cursorIdx - 1; start >= 0; start-- {
r := text[start]
if r == ':' {
break
}
if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '_' || (r >= '0' && r <= '9')) {
return cs
}
}
if start < 0 {
return cs
}
start++
word := text[start:cursorIdx]
if len(word) == 0 {
return cs
}
w := strings.ToLower(string(word))
for _, emoji := range findEmoji(w) {
c := make([]rune, 0, len(text)+len([]rune(emoji.Emoji))-len(word)-1)
c = append(c, text[:start-1]...)
c = append(c, []rune(emoji.Emoji)...)
if cursorIdx < len(text) {
c = append(c, text[cursorIdx:]...)
}
cs = append(cs, ui.Completion{
StartIdx: start - 1,
EndIdx: cursorIdx,
Text: c,
Display: []rune(fmt.Sprintf("%v (%v)", emoji.Emoji, emoji.Alias)),
CursorIdx: start - 1 + len([]rune(emoji.Emoji)),
})
}
return cs
}
func hasPrefix(s, prefix []rune) bool {
return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)])
}
func equal(a, b []rune) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}