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

Commit 6d765cb

Browse files
avahe-kellenbergerBios-Marcel
authored andcommitted
Made a work-around for selection char.
Previously when using MoveCursorToIndex, the cursor would not use the correct character - now it does.
1 parent 87b3f2c commit 6d765cb

File tree

2 files changed

+12
-20
lines changed

2 files changed

+12
-20
lines changed

ui/editor.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,21 @@ func (e *Editor) MoveCursorRight(left, right, selection []rune) {
112112
e.setAndFixText(newText)
113113
}
114114

115-
func (e *Editor) MoveCursorToIndex(left, right, selection []rune, index int) {
116-
var newText string = string(left) + string(selection) + string(right)
115+
func (e *Editor) MoveCursorToIndex(text string, index int) {
116+
// Bound the index to the string length
117117
if index < 0 {
118118
index = 0
119-
} else if index >= len(newText) {
120-
index = len(newText) - 1
119+
} else if index >= len(text) {
120+
index = len(text) - 1
121121
}
122122

123-
if index < len(left) {
124-
newText = leftRegion + string(left[:index]) + selRegion + string(left[index]) + rightRegion + string(left[index+1:]) + string(right) + endRegion
125-
} else {
126-
indexSelection := index - len(left)
127-
if indexSelection < len(selection) {
128-
newText = leftRegion + string(left) + string(left[:indexSelection]) + selRegion + string(selection[indexSelection]) + rightRegion + string(selection[indexSelection+1:]) + string(right) + endRegion
129-
} else {
130-
indexRight := index - len(left) - len(selection)
131-
if indexRight < len(right) {
132-
newText = leftRegion + string(left) + string(selection) + string(right[:indexRight]) + selRegion + string(right[indexRight]) + rightRegion + string(right[indexRight+1:]) + endRegion
133-
}
134-
}
123+
left := string(text[:index])
124+
right := string(text[index:])
125+
if len(right) == 1 {
126+
right = " " + selectionChar
127+
left += " "
135128
}
136-
137-
e.setAndFixText(newText)
129+
e.setAndFixText(leftRegion + left + selRegion + string(right[0]) + rightRegion + right[1:] + endRegion)
138130
}
139131

140132
func (e *Editor) SelectWordLeft(left, right, selection []rune) {

ui/window.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,14 +845,14 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
845845
left := oldText[:beginIndex] + strings.TrimSpace(data) + " "
846846
right := oldText[endIndex+1:]
847847
window.messageInput.SetText(left + right)
848-
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
848+
window.messageInput.MoveCursorToIndex(left+right, len(left))
849849
} else {
850850
role, ok := node.GetReference().(*discordgo.Role)
851851
if ok {
852852
left := "<@&" + strings.TrimSpace(role.ID) + "> "
853853
right := oldText[endIndex+1:]
854854
window.messageInput.SetText(left + right)
855-
window.messageInput.MoveCursorToIndex([]rune(left), []rune(right), []rune{}, len(left))
855+
window.messageInput.MoveCursorToIndex(left+right, len(left))
856856
}
857857
}
858858
window.messageInput.mentionHideHandler()

0 commit comments

Comments
 (0)