From 89323ffa1fb73e00c7c2fefdbc92eab809107288 Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Mon, 31 Jul 2023 23:34:53 +0200 Subject: [PATCH] Fixed tab completion rarely completing the wrong word. (#4735) * Fixed tab completion rarely completing the wrong word. Fixes: #3101 * Use QSignalBlocker instead of janky bool, add comment about hidden logic * copypasteo --- CHANGELOG.md | 1 + src/widgets/helper/ResizingTextEdit.cpp | 29 ++++++++++++++++++++++--- src/widgets/helper/ResizingTextEdit.hpp | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 713f1f63f..0f8c817dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Bugfix: Fixed crash that could occurr when closing the usercard too quickly after blocking or unblocking a user. (#4711) - Bugfix: Fixed highlights sometimes not working after changing sound device, or switching users in your operating system. (#4729) - Bugfix: Fixed key bindings not showing in context menus on Mac. (#4722) +- Bugfix: Fixed tab completion rarely completing the wrong word. (#4735) - Dev: Added command to set Qt's logging filter/rules at runtime (`/c2-set-logging-rules`). (#4637) - Dev: Added the ability to see & load custom themes from the Themes directory. No stable promises are made of this feature, changes might be made that breaks custom themes without notice. (#4570) - Dev: Added test cases for emote and tab completion. (#4644) diff --git a/src/widgets/helper/ResizingTextEdit.cpp b/src/widgets/helper/ResizingTextEdit.cpp index 9494a6cf1..aa93299fb 100644 --- a/src/widgets/helper/ResizingTextEdit.cpp +++ b/src/widgets/helper/ResizingTextEdit.cpp @@ -2,10 +2,12 @@ #include "common/Common.hpp" #include "common/CompletionModel.hpp" +#include "common/QLogging.hpp" #include "singletons/Settings.hpp" #include #include +#include namespace chatterino { @@ -20,6 +22,19 @@ ResizingTextEdit::ResizingTextEdit() QObject::connect(this, &QTextEdit::textChanged, this, &QWidget::updateGeometry); + QObject::connect(this, &QTextEdit::cursorPositionChanged, [this]() { + // If tab was pressed and we're completing/replacing the current word, + // this code will not even be called, see ResizingTextEdit::keyPressEvent + + if (!this->completionInProgress_) + { + return; + } + qCDebug(chatterinoCommon) + << "Finishing completion because cursor moved"; + this->completionInProgress_ = false; + }); + // Whenever the setting for emote completion changes, force a // refresh on the completion model the next time "Tab" is pressed getSettings()->prefixOnlyEmoteCompletion.connect([this] { @@ -156,8 +171,12 @@ void ResizingTextEdit::keyPressEvent(QKeyEvent *event) completionModel->refresh(currentCompletionPrefix, this->isFirstWord()); this->completionInProgress_ = true; - this->completer_->setCompletionPrefix(currentCompletionPrefix); - this->completer_->complete(); + { + // this blocks cursor movement events from resetting tab completion + QSignalBlocker dontTriggerCursorMovement(this); + this->completer_->setCompletionPrefix(currentCompletionPrefix); + this->completer_->complete(); + } return; } @@ -182,7 +201,11 @@ void ResizingTextEdit::keyPressEvent(QKeyEvent *event) } } - this->completer_->complete(); + { + // this blocks cursor movement events from updating tab completion + QSignalBlocker dontTriggerCursorMovement(this); + this->completer_->complete(); + } return; } diff --git a/src/widgets/helper/ResizingTextEdit.hpp b/src/widgets/helper/ResizingTextEdit.hpp index 71d4d152b..01a174cda 100644 --- a/src/widgets/helper/ResizingTextEdit.hpp +++ b/src/widgets/helper/ResizingTextEdit.hpp @@ -61,7 +61,7 @@ private: * `Tab` pressed again: * - complete ["pog"] to "PogChamp" * - * [other key] pressed - updating the input text: + * [other key] pressed or cursor moved - updating the input text: * - set `completionInProgress_ = false` */ bool completionInProgress_ = false;