Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Mention results only search from the @ symbol to the cursor.
Browse files Browse the repository at this point in the history
  • Loading branch information
avahe-kellenberger authored and Bios-Marcel committed Oct 6, 2019
1 parent 3afd623 commit 8bebf29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
22 changes: 21 additions & 1 deletion ui/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ func (e *Editor) MoveCursorRight(left, right, selection []rune) {
e.setAndFixText(newText)
}

func (e *Editor) MoveCursorToIndex(left, right, selection []rune, index int) {
var newText string
if index < len(left) {
newText = leftRegion + string(left[:index]) + selRegion + string(left[index]) + rightRegion + string(left[index+1:]) + string(right) + endRegion
} else {
indexSelection := index - len(left)
if indexSelection < len(selection) {
newText = leftRegion + string(left) + string(left[:indexSelection]) + selRegion + string(selection[indexSelection]) + rightRegion + string(selection[indexSelection+1:]) + string(right) + endRegion
} else {
indexRight := index - len(left) - len(selection)
if indexRight < len(right) {
newText = leftRegion + string(left) + string(selection) + string(right[:indexRight]) + selRegion + string(right[indexRight]) + rightRegion + string(right[indexRight+1:]) + endRegion
}
}
}

e.setAndFixText(newText)
}

func (e *Editor) SelectWordLeft(left, right, selection []rune) {
if len(left) > 0 {
selectionFrom := 0
Expand Down Expand Up @@ -363,7 +382,8 @@ func (editor *Editor) UpdateMentionHandler() {
}

func (editor *Editor) ShowMentionHandler(atSymbolIndex int) {
lookupKeyword := editor.GetText()[atSymbolIndex+1:]
text := editor.internalTextView.GetRegionText("left")
lookupKeyword := text[atSymbolIndex+1:]
editor.currentMentionBeginIndex = atSymbolIndex + 1
editor.currentMentionEndIndex = len(lookupKeyword) + atSymbolIndex
if editor.mentionShowHandler != nil {
Expand Down
12 changes: 8 additions & 4 deletions ui/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,13 +842,17 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
data, ok := node.GetReference().(string)
oldText := window.messageInput.GetText()
if ok {
newText := oldText[:beginIndex] + strings.TrimSpace(data) + oldText[endIndex+1:] + " "
window.messageInput.SetText(newText)
left := oldText[:beginIndex] + strings.TrimSpace(data) + " "
right := oldText[endIndex+1:]
window.messageInput.SetText(left + right)
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
} else {
role, ok := node.GetReference().(*discordgo.Role)
if ok {
newText := oldText[:beginIndex-1] + "<@&" + strings.TrimSpace(role.ID) + ">" + oldText[endIndex+1:] + " "
window.messageInput.SetText(newText)
left := "<@&" + strings.TrimSpace(role.ID) + "> "
right := oldText[endIndex+1:]
window.messageInput.SetText(left + right)
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
}
}
window.messageInput.mentionHideHandler()
Expand Down

0 comments on commit 8bebf29

Please sign in to comment.