fix: support completing emotes starting with : (#5603)

This commit is contained in:
nerix
2024-10-06 11:29:52 +02:00
committed by GitHub
parent 0085fb64ac
commit 9ba7ef324d
7 changed files with 69 additions and 43 deletions
@@ -52,25 +52,36 @@ void ClassicTabEmoteStrategy::apply(const std::vector<EmoteItem> &items,
std::vector<EmoteItem> &output,
const QString &query) const
{
bool emojiOnly = false;
QString normalizedQuery = query;
if (normalizedQuery.startsWith(':'))
bool colonStart = query.startsWith(':');
QStringView normalizedQuery = query;
if (colonStart)
{
// TODO(Qt6): use sliced
normalizedQuery = normalizedQuery.mid(1);
// tab completion with : prefix should do emojis only
emojiOnly = true;
}
std::set<EmoteItem, CompletionEmoteOrder> emotes;
for (const auto &item : items)
{
if (emojiOnly ^ item.isEmoji)
QStringView itemQuery;
if (item.isEmoji)
{
continue;
if (colonStart)
{
itemQuery = normalizedQuery;
}
else
{
continue; // ignore emojis when not completing with ':'
}
}
else
{
itemQuery = query;
}
if (startsWithOrContains(item.searchName, normalizedQuery,
if (startsWithOrContains(item.searchName, itemQuery,
Qt::CaseInsensitive,
getSettings()->prefixOnlyEmoteCompletion))
{
@@ -25,8 +25,7 @@ namespace {
* @return How different the emote is from query. Values in the range [-10,
* \infty].
*/
int costOfEmote(const QString &query, const QString &emote,
bool prioritizeUpper)
int costOfEmote(QStringView query, QStringView emote, bool prioritizeUpper)
{
int score = 0;
@@ -68,8 +67,8 @@ namespace {
// matchingFunction is used for testing if the emote should be included in the search.
void completeEmotes(
const std::vector<EmoteItem> &items, std::vector<EmoteItem> &output,
const QString &query, bool ignoreColonForCost,
const std::function<bool(EmoteItem, QString, Qt::CaseSensitivity)>
QStringView query, bool ignoreColonForCost,
const std::function<bool(EmoteItem, Qt::CaseSensitivity)>
&matchingFunction)
{
// Given these emotes: pajaW, PAJAW
@@ -92,8 +91,7 @@ namespace {
for (const auto &item : items)
{
if (matchingFunction(
item, query,
haveUpper ? Qt::CaseSensitive : Qt::CaseInsensitive))
item, haveUpper ? Qt::CaseSensitive : Qt::CaseInsensitive))
{
output.push_back(item);
}
@@ -118,7 +116,7 @@ namespace {
// Run the search again but this time without case sensitivity
for (const auto &item : items)
{
if (matchingFunction(item, query, Qt::CaseInsensitive))
if (matchingFunction(item, Qt::CaseInsensitive))
{
output.push_back(item);
}
@@ -170,9 +168,10 @@ void SmartEmoteStrategy::apply(const std::vector<EmoteItem> &items,
ignoreColonForCost = true;
}
completeEmotes(items, output, normalizedQuery, ignoreColonForCost,
[](const EmoteItem &left, const QString &right,
Qt::CaseSensitivity caseHandling) {
return left.searchName.contains(right, caseHandling);
[normalizedQuery](const EmoteItem &left,
Qt::CaseSensitivity caseHandling) {
return left.searchName.contains(normalizedQuery,
caseHandling);
});
}
@@ -180,25 +179,38 @@ void SmartTabEmoteStrategy::apply(const std::vector<EmoteItem> &items,
std::vector<EmoteItem> &output,
const QString &query) const
{
bool emojiOnly = false;
QString normalizedQuery = query;
if (normalizedQuery.startsWith(':'))
bool colonStart = query.startsWith(':');
QStringView normalizedQuery = query;
if (colonStart)
{
// TODO(Qt6): use sliced
normalizedQuery = normalizedQuery.mid(1);
// tab completion with : prefix should do emojis only
emojiOnly = true;
}
completeEmotes(items, output, normalizedQuery, false,
[emojiOnly](const EmoteItem &left, const QString &right,
Qt::CaseSensitivity caseHandling) -> bool {
if (emojiOnly ^ left.isEmoji)
{
return false;
}
return startsWithOrContains(
left.searchName, right, caseHandling,
getSettings()->prefixOnlyEmoteCompletion);
});
completeEmotes(
items, output, normalizedQuery, false,
[&](const EmoteItem &item, Qt::CaseSensitivity caseHandling) -> bool {
QStringView itemQuery;
if (item.isEmoji)
{
if (colonStart)
{
itemQuery = normalizedQuery;
}
else
{
return false; // ignore emojis when not completing with ':'
}
}
else
{
itemQuery = query;
}
return startsWithOrContains(
item.searchName, itemQuery, caseHandling,
getSettings()->prefixOnlyEmoteCompletion);
});
}
} // namespace chatterino::completion