Make Emote Tab Completion Search for Substrings (#1204)

* Make emote tab completion search for substrings

Previously, tab completion only searched for emotes that start
with the current prefix under the cursor. This commit makes tab
completion look for the current prefix as a substring in any position,
not just the start.

Examples:
    * In forsen's channel, tabbing on "pls" will cycle through "DonaldPls", "forsenPls",
    "GachiPls", "nyanPls" and "SourPls".

    * As a forsen subscriber, tabbing on "sen1" will complete to "forsen1".

    * As a pajlada subscriber, tabbing on "shrug" will cycle through
    "pajaShrugL" and "pajaShrugR". (Unless you are in a channel with
    more "shrug" emotes, of course.)

* Add a setting for prefix and substring completion

This commit adds a setting under the "Miscellaneous" section to allow
user to choose whether they want prefix-only or substring emote
completion.

The QCompleter filter mode and `addString` function are now chosen
according to the user's setting.

* Improve description of emote completion setting

Also reintroduce a blank line that went missing.
This commit is contained in:
Leon Richardt 2019-08-17 17:17:38 +02:00 committed by fourtf
parent 3dcbe89e28
commit 58d892a8c2
4 changed files with 31 additions and 5 deletions

View file

@ -10,6 +10,7 @@
#include "providers/twitch/TwitchChannel.hpp"
#include "providers/twitch/TwitchServer.hpp"
#include "singletons/Emotes.hpp"
#include "singletons/Settings.hpp"
#include <QtAlgorithms>
#include <utility>
@ -78,17 +79,28 @@ int CompletionModel::rowCount(const QModelIndex &) const
void CompletionModel::refresh(const QString &prefix)
{
std::function<void(const QString &, TaggedString::Type)> addString;
if (getSettings()->prefixOnlyEmoteCompletion)
{
addString = [&](const QString &str, TaggedString::Type type) {
if (str.startsWith(prefix, Qt::CaseInsensitive))
this->items_.emplace(str + " ", type);
};
}
else
{
addString = [&](const QString &str, TaggedString::Type type) {
if (str.contains(prefix, Qt::CaseInsensitive))
this->items_.emplace(str + " ", type);
};
}
std::lock_guard<std::mutex> guard(this->itemsMutex_);
this->items_.clear();
if (prefix.length() < 2)
return;
auto addString = [&](const QString &str, TaggedString::Type type) {
if (str.startsWith(prefix, Qt::CaseInsensitive))
this->items_.emplace(str + " ", type);
};
if (auto channel = dynamic_cast<TwitchChannel *>(&this->channel_))
{
// account emotes

View file

@ -99,6 +99,7 @@ public:
"/behaviour/autocompletion/onlyFetchChattersForSmallerStreamers", true};
IntSetting smallStreamerLimit = {
"/behaviour/autocompletion/smallStreamerLimit", 1000};
BoolSetting prefixOnlyEmoteCompletion = {"/behaviour/autocompletion/prefixOnlyCompletion", true};
BoolSetting pauseChatOnHover = {"/behaviour/pauseChatHover", false};
BoolSetting autorun = {"/behaviour/autorun", false};

View file

@ -1,6 +1,7 @@
#include "widgets/helper/ResizingTextEdit.hpp"
#include "common/Common.hpp"
#include "common/CompletionModel.hpp"
#include "singletons/Settings.hpp"
namespace chatterino {
@ -198,6 +199,16 @@ void ResizingTextEdit::setCompleter(QCompleter *c)
this->completer_->setWidget(this);
this->completer_->setCompletionMode(QCompleter::InlineCompletion);
this->completer_->setCaseSensitivity(Qt::CaseInsensitive);
if (getSettings()->prefixOnlyEmoteCompletion)
{
this->completer_->setFilterMode(Qt::MatchStartsWith);
}
else
{
this->completer_->setFilterMode(Qt::MatchContains);
}
QObject::connect(completer_,
static_cast<void (QCompleter::*)(const QString &)>(
&QCompleter::highlighted),

View file

@ -283,6 +283,8 @@ void GeneralPage::initLayout(SettingsLayout &layout)
{"Don't show", "Always show", "Hold shift"}, s.emotesTooltipPreview,
[](int index) { return index; }, [](auto args) { return args.index; },
false);
layout.addCheckbox("Only search for emote autocompletion at the start of emote names",
s.prefixOnlyEmoteCompletion);
layout.addSpacing(16);
layout.addSeperator();