commit 83e0e82

Hubert Hirtz  ·  2020-11-19 15:08:38 +0000 UTC
parent 10f2baa
Add completion for /msg
3 files changed,  +47, -0
M app.go
M app.go
+1, -0
1@@ -460,6 +460,7 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
2 		cs = app.completionsChannelTopic(cs, cursorIdx, text)
3 		cs = app.completionsChannelMembers(cs, cursorIdx, text)
4 	}
5+	cs = app.completionsMsg(cs, cursorIdx, text)
6 
7 	if cs != nil {
8 		cs = append(cs, ui.Completion{
+38, -0
 1@@ -56,6 +56,44 @@ func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text
 2 	return cs
 3 }
 4 
 5+func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
 6+	if !hasPrefix(text, []rune("/msg ")) {
 7+		return cs
 8+	}
 9+	// Check if the first word (target) is already written and complete (in
10+	// which case we don't have completions to provide).
11+	var word string
12+	hasMetALetter := false
13+	for i := 5; i < cursorIdx; i += 1 {
14+		if hasMetALetter && text[i] == ' ' {
15+			return cs
16+		}
17+		if !hasMetALetter && text[i] != ' ' {
18+			word = app.s.Casemap(string(text[i:cursorIdx]))
19+			hasMetALetter = true
20+		}
21+	}
22+	if word == "" {
23+		return cs
24+	}
25+	for _, user := range app.s.Users() {
26+		if strings.HasPrefix(app.s.Casemap(user), word) {
27+			nickComp := append([]rune(user), ' ')
28+			c := make([]rune, len(text)+5+len(nickComp)-cursorIdx)
29+			copy(c[:5], []rune("/msg "))
30+			copy(c[5:], nickComp)
31+			if cursorIdx < len(text) {
32+				copy(c[5+len(nickComp):], text[cursorIdx:])
33+			}
34+			cs = append(cs, ui.Completion{
35+				Text:      c,
36+				CursorIdx: 5 + len(nickComp),
37+			})
38+		}
39+	}
40+	return cs
41+}
42+
43 func hasPrefix(s, prefix []rune) bool {
44 	return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)])
45 }
+8, -0
 1@@ -265,6 +265,14 @@ func (s *Session) Casemap(name string) string {
 2 	return CasemapASCII(name)
 3 }
 4 
 5+func (s *Session) Users() []string {
 6+	users := make([]string, 0, len(s.users))
 7+	for _, u := range s.users {
 8+		users = append(users, u.Name.Name)
 9+	}
10+	return users
11+}
12+
13 func (s *Session) Names(channel string) []Member {
14 	var names []Member
15 	if c, ok := s.channels[s.Casemap(channel)]; ok {