-
Notifications
You must be signed in to change notification settings - Fork 1
/
autocomplete.go
121 lines (100 loc) · 2.56 KB
/
autocomplete.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
package main
import (
"strings"
"github.com/c-bata/go-prompt"
)
func WrapAutoCompleter(state *State) prompt.Completer {
return func(doc prompt.Document) []prompt.Suggest {
return autocomplete(state, doc)
}
}
// isFirstWord returns true if the current word is the first word.
func isFirstWord(doc prompt.Document) bool {
return doc.FindStartOfPreviousWord() == 0
}
func autocomplete(state *State, doc prompt.Document) (suggestions []prompt.Suggest) {
// Try to autocomplete command arguments.
before := doc.TextBeforeCursor()
fields := strings.Fields(before)
if len(fields) == 0 {
return nil
}
switch strings.TrimPrefix(fields[0], "/") {
case "join":
var search string
if len(fields) > 1 {
search = fields[1]
}
return searchAllChannels(state, search)
}
word := doc.GetWordBeforeCursor()
if len(word) == 0 {
return nil
}
// Prioritize command completion starting with a slash (/).
if isFirstWord(doc) && word[0] == '/' {
return prompt.FilterHasPrefix(commandSuggestions, word, true)
}
switch p, search := word[0], string(word[1:]); p {
case '@':
members, err := state.Store.Members(state.GuildID())
if err != nil {
return nil
}
lower := strings.ToLower(search)
for _, m := range members {
if match := hasPrefixes(lower, m.User.Username, m.Nick); match != "" {
suggestions = append(suggestions, prompt.Suggest{
Text: m.User.Mention(),
Description: "@" + m.User.Username,
})
}
}
if len(suggestions) == 0 {
state.MemberState.SearchMember(state.GuildID(), search)
}
case '#':
channels, err := state.Store.Channels(state.GuildID())
if err != nil {
return nil
}
for _, ch := range channels {
if match := hasPrefixes(search, ch.Name); match != "" {
suggestions = append(suggestions, prompt.Suggest{
Text: ch.Mention(),
Description: "#" + ch.Name,
})
}
}
}
return nil
}
func searchAllChannels(state *State, prefix string) (suggestions []prompt.Suggest) {
guilds, err := state.Store.Guilds()
if err != nil {
return nil
}
for _, guild := range guilds {
channels, err := state.Store.Channels(guild.ID)
if err != nil {
continue
}
for _, ch := range channels {
if id := ch.ID.String(); strings.HasPrefix(id, prefix) {
suggestions = append(suggestions, prompt.Suggest{
Text: id,
Description: "#" + ch.Name,
})
}
}
}
return
}
func hasPrefixes(word string, matches ...string) string {
for _, match := range matches {
if match != "" && strings.HasPrefix(strings.ToLower(match), word) {
return match
}
}
return ""
}