-
-
Notifications
You must be signed in to change notification settings - Fork 568
Expand file tree
/
Copy pathInputHighlighter.cpp
More file actions
162 lines (140 loc) · 4.93 KB
/
InputHighlighter.cpp
File metadata and controls
162 lines (140 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-FileCopyrightText: 2026 Contributors to Chatterino <https://chatterino.com>
//
// SPDX-License-Identifier: MIT
#include "widgets/splits/InputHighlighter.hpp"
#include "Application.hpp"
#include "common/Aliases.hpp"
#include "common/LinkParser.hpp"
#include "common/QLogging.hpp"
#include "controllers/accounts/AccountController.hpp"
#include "controllers/commands/CommandController.hpp"
#include "controllers/spellcheck/SpellChecker.hpp"
#include "messages/Emote.hpp"
#include "providers/bttv/BttvEmotes.hpp"
#include "providers/seventv/SeventvEmotes.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "providers/twitch/TwitchChannel.hpp"
#include "singletons/Settings.hpp"
#include <QTextCharFormat>
#include <QTextDocument>
namespace {
using namespace chatterino;
bool isIgnoredWord(TwitchChannel *twitch, const QString &word)
{
qCDebug(chatterinoSpellcheck) << "isIgnoredWord" << word;
EmoteName name{word};
if (twitch)
{
if (twitch->bttvEmote(name) || twitch->ffzEmote(name) ||
twitch->seventvEmote(name))
{
return true;
}
auto locals = twitch->localTwitchEmotes();
if (locals->contains(name))
{
return true;
}
QString chatter;
// skip '@' to allow @chatter
if (word.startsWith('@')) {
chatter = word.sliced(1);
} else {
chatter = word;
}
if (twitch->accessChatters()->contains(chatter) ||
(getSettings()->alwaysIncludeBroadcasterInUserCompletions &&
chatter.toLower() == twitch->getName().toLower()))
{
return true;
}
}
if (getApp()->getBttvEmotes()->emote(name) ||
getApp()->getFfzEmotes()->emote(name) ||
getApp()->getSeventvEmotes()->globalEmote(name))
{
return true;
}
if (getApp()
->getAccounts()
->twitch.getCurrent()
->twitchEmote(name)
.has_value())
{
return true;
}
// TODO: Replace this with a link parser variant that doesn't return the parsed data
auto link = linkparser::parse(word);
return link.has_value();
}
} // namespace
namespace chatterino {
InputHighlighter::InputHighlighter(SpellChecker &spellChecker, QObject *parent)
: QSyntaxHighlighter(parent)
, spellChecker(spellChecker)
// FIXME: this also matches URLs - this probably needs to be some function like Firefox' mozEnglishWordUtils::FindNextWord
, wordRegex(R"(\p{L}(?:\P{Z}+\p{L}+)*)",
QRegularExpression::PatternOption::UseUnicodePropertiesOption)
{
this->spellFmt.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
this->spellFmt.setUnderlineColor(Qt::red);
}
InputHighlighter::~InputHighlighter() = default;
void InputHighlighter::setChannel(const std::shared_ptr<Channel> &channel)
{
auto twitch = std::dynamic_pointer_cast<TwitchChannel>(channel);
this->channel = twitch;
this->rehighlight();
}
void InputHighlighter::highlightBlock(const QString &text)
{
if (!this->spellChecker.isLoaded())
{
return;
}
auto *channel = this->channel.lock().get();
QRegularExpression outerRegex(R"(\S+)");
// maybe change to R"(\p{L}+)" to allow for more/any seperators in words (would fix #6762)
QRegularExpression innerRegex = this->wordRegex;
QStringView textView = text;
// skip leading command trigger
auto cmdTriggerLen = getApp()->getCommands()->commandTriggerLen(textView);
textView = textView.sliced(cmdTriggerLen);
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
auto outerIt = outerRegex.globalMatchView(textView);
#else
auto outerIt = outerRegex.globalMatch(textView);
#endif
// iterate over strings of any non-whitespace characters
while (outerIt.hasNext())
{
auto outerMatch = outerIt.next();
auto outerText = outerMatch.captured();
if (!isIgnoredWord(channel, outerText))
{
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
auto innerIt = innerRegex.globalMatchView(outerText);
#else
auto innerIt = innerRegex.globalMatch(outerText);
#endif
// iterate over words that match the word regex
while (innerIt.hasNext())
{
auto innerMatch = innerIt.next();
auto innerText = innerMatch.captured();
qCDebug(chatterinoSpellcheck) << "check" << innerText;
if (!this->spellChecker.check(innerText))
{
this->setFormat(
static_cast<int>(cmdTriggerLen +
outerMatch.capturedStart() +
innerMatch.capturedStart()),
static_cast<int>(innerText.size()), this->spellFmt);
}
}
}
}
}
}
} // namespace chatterino