commit 2313198
Hubert Hirtz
·
2020-11-13 11:00:22 +0000 UTC
parent dfb2100
/topic completion
2 files changed,
+77,
-30
M
app.go
M
app.go
+4,
-30
1@@ -455,36 +455,10 @@ func (app *App) completions(cursorIdx int, text []rune) []ui.Completion {
2 return cs
3 }
4
5- var start int
6- for start = cursorIdx - 1; 0 <= start; start-- {
7- if text[start] == ' ' {
8- break
9- }
10- }
11- start++
12- word := text[start:cursorIdx]
13- if len(word) == 0 {
14- return cs
15- }
16- wordCf := app.s.Casemap(string(word))
17- for _, name := range app.s.Names(app.win.CurrentBuffer()) {
18- if strings.HasPrefix(app.s.Casemap(name.Name.Name), wordCf) {
19- nickComp := []rune(name.Name.Name)
20- if start == 0 {
21- nickComp = append(nickComp, ':')
22- }
23- nickComp = append(nickComp, ' ')
24- c := make([]rune, len(text)+len(nickComp)-len(word))
25- copy(c[:start], text[:start])
26- if cursorIdx < len(text) {
27- copy(c[start+len(nickComp):], text[cursorIdx:])
28- }
29- copy(c[start:], nickComp)
30- cs = append(cs, ui.Completion{
31- Text: c,
32- CursorIdx: start + len(nickComp),
33- })
34- }
35+ buffer := app.win.CurrentBuffer()
36+ if app.s.IsChannel(buffer) {
37+ cs = app.completionsChannelTopic(cs, cursorIdx, text)
38+ cs = app.completionsChannelMembers(cs, cursorIdx, text)
39 }
40
41 if cs != nil {
+73,
-0
1@@ -0,0 +1,73 @@
2+package senpai
3+
4+import (
5+ "strings"
6+
7+ "git.sr.ht/~taiite/senpai/ui"
8+)
9+
10+func (app *App) completionsChannelMembers(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
11+ var start int
12+ for start = cursorIdx - 1; 0 <= start; start-- {
13+ if text[start] == ' ' {
14+ break
15+ }
16+ }
17+ start++
18+ word := text[start:cursorIdx]
19+ if len(word) == 0 {
20+ return cs
21+ }
22+ wordCf := app.s.Casemap(string(word))
23+ for _, name := range app.s.Names(app.win.CurrentBuffer()) {
24+ if strings.HasPrefix(app.s.Casemap(name.Name.Name), wordCf) {
25+ nickComp := []rune(name.Name.Name)
26+ if start == 0 {
27+ nickComp = append(nickComp, ':')
28+ }
29+ nickComp = append(nickComp, ' ')
30+ c := make([]rune, len(text)+len(nickComp)-len(word))
31+ copy(c[:start], text[:start])
32+ if cursorIdx < len(text) {
33+ copy(c[start+len(nickComp):], text[cursorIdx:])
34+ }
35+ copy(c[start:], nickComp)
36+ cs = append(cs, ui.Completion{
37+ Text: c,
38+ CursorIdx: start + len(nickComp),
39+ })
40+ }
41+ }
42+ return cs
43+}
44+
45+func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
46+ if !hasPrefix(text, []rune("/topic ")) {
47+ return cs
48+ }
49+ topic, _, _ := app.s.Topic(app.win.CurrentBuffer())
50+ if cursorIdx == len(text) {
51+ compText := append(text, []rune(topic)...)
52+ cs = append(cs, ui.Completion{
53+ Text: compText,
54+ CursorIdx: len(compText),
55+ })
56+ }
57+ return cs
58+}
59+
60+func hasPrefix(s, prefix []rune) bool {
61+ return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)])
62+}
63+
64+func equal(a, b []rune) bool {
65+ if len(a) != len(b) {
66+ return false
67+ }
68+ for i := 0; i < len(a); i++ {
69+ if a[i] != b[i] {
70+ return false
71+ }
72+ }
73+ return true
74+}