summaryrefslogtreecommitdiff
path: root/completions.go
diff options
context:
space:
mode:
authorHubert Hirtz <hubert@hirtz.pm>2020-11-13 12:00:22 +0100
committerHubert Hirtz <hubert@hirtz.pm>2020-11-13 12:01:09 +0100
commit23131981b029bb6e332975c22caaa467affab216 (patch)
tree3117183469cbaee9ba30f526ca29e7f3a541567b /completions.go
parentDon't autocomplete on empty word (diff)
/topic completion
Diffstat (limited to 'completions.go')
-rw-r--r--completions.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/completions.go b/completions.go
new file mode 100644
index 0000000..6fac5e8
--- /dev/null
+++ b/completions.go
@@ -0,0 +1,73 @@
+package senpai
+
+import (
+ "strings"
+
+ "git.sr.ht/~taiite/senpai/ui"
+)
+
+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
+ }
+ wordCf := app.s.Casemap(string(word))
+ for _, name := range app.s.Names(app.win.CurrentBuffer()) {
+ if strings.HasPrefix(app.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{
+ Text: c,
+ CursorIdx: start + len(nickComp),
+ })
+ }
+ }
+ return cs
+}
+
+func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion {
+ if !hasPrefix(text, []rune("/topic ")) {
+ return cs
+ }
+ topic, _, _ := app.s.Topic(app.win.CurrentBuffer())
+ if cursorIdx == len(text) {
+ compText := append(text, []rune(topic)...)
+ cs = append(cs, ui.Completion{
+ Text: compText,
+ CursorIdx: len(compText),
+ })
+ }
+ 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
+}