diff options
author | Hubert Hirtz <hubert@hirtz.pm> | 2020-11-19 16:08:38 +0100 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtz.pm> | 2020-11-19 16:08:38 +0100 |
commit | 83e0e82ad08adeb910c13e4a53336cbf422356c9 (patch) | |
tree | 662ab140d62759c03dfef7a9be5eb6234d0c987e /completions.go | |
parent | Move editor view when completing text (diff) |
Add completion for /msg
Diffstat (limited to 'completions.go')
-rw-r--r-- | completions.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/completions.go b/completions.go index 6fac5e8..db8a7f7 100644 --- a/completions.go +++ b/completions.go @@ -56,6 +56,44 @@ func (app *App) completionsChannelTopic(cs []ui.Completion, cursorIdx int, text return cs } +func (app *App) completionsMsg(cs []ui.Completion, cursorIdx int, text []rune) []ui.Completion { + if !hasPrefix(text, []rune("/msg ")) { + return cs + } + // 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 = app.s.Casemap(string(text[i:cursorIdx])) + hasMetALetter = true + } + } + if word == "" { + return cs + } + for _, user := range app.s.Users() { + if strings.HasPrefix(app.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{ + Text: c, + CursorIdx: 5 + len(nickComp), + }) + } + } + return cs +} + func hasPrefix(s, prefix []rune) bool { return len(prefix) <= len(s) && equal(prefix, s[:len(prefix)]) } |