From 79a3da3358a47c71bc937ab640faa667768aa896 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Wed, 5 Mar 2025 21:00:47 -0500 Subject: [PATCH] Stop emojis from destroying your message This is easy to reproduce in the following scenario with a bunch of half-completed emojis: ":a :a :a :a". Trying to complete anything but the last one starts replacing parts of the message because it only considers the last colon to the current completion identifier. This change fixes that and said scenario can no longer cause a message massacare. This bug doesn't seem to affect the other completions because their searching in the string was correct, but I made sure they all share the same index now. BUG: 479587 FIXED-IN: 24.12.3 --- src/chatdocumenthandler.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/chatdocumenthandler.cpp b/src/chatdocumenthandler.cpp index f1086659f..5ff6ab6bf 100644 --- a/src/chatdocumenthandler.cpp +++ b/src/chatdocumenthandler.cpp @@ -222,11 +222,14 @@ void ChatDocumentHandler::complete(int index) return; } + // Ensure we only search for the beginning of the current completion identifier + const auto fromIndex = qMax(completionStartIndex(), 0); + if (m_completionModel->autoCompletionType() == CompletionModel::User) { auto name = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::DisplayNameRole).toString(); auto id = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::SubtitleRole).toString(); auto text = getText(); - auto at = text.lastIndexOf(QLatin1Char('@'), cursorPosition() - 1); + auto at = text.indexOf(QLatin1Char('@'), fromIndex); QTextCursor cursor(document()->textDocument()); cursor.setPosition(at); cursor.setPosition(cursorPosition(), QTextCursor::KeepAnchor); @@ -239,7 +242,7 @@ void ChatDocumentHandler::complete(int index) } else if (m_completionModel->autoCompletionType() == CompletionModel::Command) { auto command = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::ReplacedTextRole).toString(); auto text = getText(); - auto at = text.lastIndexOf(QLatin1Char('/')); + auto at = text.indexOf(QLatin1Char('/'), fromIndex); QTextCursor cursor(document()->textDocument()); cursor.setPosition(at); cursor.setPosition(cursorPosition(), QTextCursor::KeepAnchor); @@ -247,7 +250,7 @@ void ChatDocumentHandler::complete(int index) } else if (m_completionModel->autoCompletionType() == CompletionModel::Room) { auto alias = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::SubtitleRole).toString(); auto text = getText(); - auto at = text.lastIndexOf(QLatin1Char('#'), cursorPosition() - 1); + auto at = text.indexOf(QLatin1Char('#'), fromIndex); QTextCursor cursor(document()->textDocument()); cursor.setPosition(at); cursor.setPosition(cursorPosition(), QTextCursor::KeepAnchor); @@ -260,7 +263,7 @@ void ChatDocumentHandler::complete(int index) } else if (m_completionModel->autoCompletionType() == CompletionModel::Emoji) { auto shortcode = m_completionModel->data(m_completionModel->index(index, 0), CompletionModel::ReplacedTextRole).toString(); auto text = getText(); - auto at = text.lastIndexOf(QLatin1Char(':')); + auto at = text.indexOf(QLatin1Char(':'), fromIndex); QTextCursor cursor(document()->textDocument()); cursor.setPosition(at); cursor.setPosition(cursorPosition(), QTextCursor::KeepAnchor);