Skip to content

Commit

Permalink
Make the SmartEmoteStrategy account for emotes starting with :, they
Browse files Browse the repository at this point in the history
shouldn't have additional cost
  • Loading branch information
Mm2PL committed Nov 28, 2023
1 parent 958e6ea commit a810474
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/controllers/completion/strategies/SmartEmoteStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ 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,
const QString &query, bool ignoreColonForCost,
const std::function<bool(EmoteItem, QString, Qt::CaseSensitivity)>
&matchingFunction)
{
Expand Down Expand Up @@ -130,21 +130,31 @@ namespace {
}
}

std::sort(
output.begin(), output.end(),
[query, prioritizeUpper](const EmoteItem &a,
const EmoteItem &b) -> bool {
auto costA = costOfEmote(query, a.searchName, prioritizeUpper);
auto costB = costOfEmote(query, b.searchName, prioritizeUpper);
if (costA == costB)
{
// Case difference and length came up tied for (a, b), break the tie
return QString::compare(a.searchName, b.searchName,
Qt::CaseInsensitive) < 0;
}

return costA < costB;
});
std::sort(output.begin(), output.end(),
[query, prioritizeUpper, ignoreColonForCost](
const EmoteItem &a, const EmoteItem &b) -> bool {
auto tempA = a.searchName;
auto tempB = b.searchName;
if (ignoreColonForCost && tempA.startsWith(":"))
{
tempA = tempA.mid(1);
}
if (ignoreColonForCost && tempB.startsWith(":"))
{
tempB = tempB.mid(1);
}

auto costA = costOfEmote(query, tempA, prioritizeUpper);
auto costB = costOfEmote(query, tempB, prioritizeUpper);
if (costA == costB)
{
// Case difference and length came up tied for (a, b), break the tie
return QString::compare(tempA, tempB,
Qt::CaseInsensitive) < 0;
}

return costA < costB;
});
}
} // namespace

Expand All @@ -153,11 +163,13 @@ void SmartEmoteStrategy::apply(const std::vector<EmoteItem> &items,
const QString &query) const
{
QString normalizedQuery = query;
bool ignoreColonForCost = false;
if (normalizedQuery.startsWith(':'))
{
normalizedQuery = normalizedQuery.mid(1);
ignoreColonForCost = true;
}
completeEmotes(items, output, normalizedQuery,
completeEmotes(items, output, normalizedQuery, ignoreColonForCost,
[](const EmoteItem &left, const QString &right,
Qt::CaseSensitivity caseHandling) {
return left.searchName.contains(right, caseHandling);
Expand All @@ -176,7 +188,7 @@ void SmartTabEmoteStrategy::apply(const std::vector<EmoteItem> &items,
// tab completion with : prefix should do emojis only
emojiOnly = true;
}
completeEmotes(items, output, normalizedQuery,
completeEmotes(items, output, normalizedQuery, false,
[emojiOnly](const EmoteItem &left, const QString &right,
Qt::CaseSensitivity caseHandling) -> bool {
if (emojiOnly ^ left.isEmoji)
Expand Down

0 comments on commit a810474

Please sign in to comment.