Fix autocompletion for display names with spaces

This commit is contained in:
miruka 2020-08-21 10:42:57 -04:00
parent 9dc9a5e70f
commit cd81c94ce8

View File

@ -13,6 +13,7 @@ HListView {
property bool open: false property bool open: false
property var originalWord: null property var originalWord: null
property int replacementLength: -1
property bool autoOpenCompleted: false property bool autoOpenCompleted: false
property var usersCompleted: ({}) // {displayName: userId} property var usersCompleted: ({}) // {displayName: userId}
@ -24,6 +25,8 @@ HListView {
readonly property var wordToComplete: readonly property var wordToComplete:
open ? originalWord || textArea.getWordBehindCursor() : null open ? originalWord || textArea.getWordBehindCursor() : null
// property var pr: wordToComplete
// onPrChanged: print(JSON.stringify( pr, null, 4))
readonly property string modelFilter: readonly property string modelFilter:
autoOpen && wordToComplete ? wordToComplete.word.replace(/^@/, "") : autoOpen && wordToComplete ? wordToComplete.word.replace(/^@/, "") :
@ -40,12 +43,17 @@ HListView {
return lastWordMatch.index return lastWordMatch.index
} }
function replaceCurrentWord(withText) { function replaceCompletionOrCurrentWord(withText) {
const current = textArea.getWordBehindCursor() const current = textArea.getWordBehindCursor()
if (current) { if (! current) return
textArea.remove(current.start, current.end + 1)
textArea.insertAtCursor(withText) const start =
} replacementLength === -1 ?
current.start :
current.end + 1 - replacementLength
textArea.remove(start, current.end + 1)
textArea.insertAtCursor(withText)
} }
function previous() { function previous() {
@ -81,7 +89,7 @@ HListView {
} }
function cancel() { function cancel() {
if (originalWord) replaceCurrentWord(originalWord.word) if (originalWord) replaceCompletionOrCurrentWord(originalWord.word)
currentIndex = -1 currentIndex = -1
open = false open = false
@ -104,6 +112,7 @@ HListView {
onAutoOpenChanged: open = autoOpen onAutoOpenChanged: open = autoOpen
onOpenChanged: if (! open) { onOpenChanged: if (! open) {
originalWord = null originalWord = null
replacementLength = -1
currentIndex = -1 currentIndex = -1
autoOpenCompleted = false autoOpenCompleted = false
py.callCoro("set_string_filter", [model.modelId, ""]) py.callCoro("set_string_filter", [model.modelId, ""])
@ -119,7 +128,9 @@ HListView {
if (! originalWord) originalWord = textArea.getWordBehindCursor() if (! originalWord) originalWord = textArea.getWordBehindCursor()
if (autoOpen) autoOpenCompleted = true if (autoOpen) autoOpenCompleted = true
replaceCurrentWord(model.get(currentIndex).display_name) const name = model.get(currentIndex).display_name
replaceCompletionOrCurrentWord(name)
replacementLength = name.length
} }
Behavior on opacity { HNumberAnimation {} } Behavior on opacity { HNumberAnimation {} }