Commit 6e3e25f4 authored by Jan Paul Batrina's avatar Jan Paul Batrina
Browse files

[ColorPicker] Improve Color regex

QRegularExpression::CaseInsensitiveOption is now used instead of QString.toLower to avoid a copy.
The regex is now just a normal member of ColorNoteProvider (Instead of static).
The hex color matching will now ignore color codes inside other words (e.g. #define, #123-456)
parent 8d3c7108
......@@ -30,15 +30,12 @@
#include <QRegularExpression>
#include <QVariant>
QRegularExpression ColorPickerInlineNoteProvider::s_colorRegEx = QRegularExpression();
bool ColorPickerInlineNoteProvider::s_putPreviewAfterColor = true;
ColorPickerInlineNoteProvider::ColorPickerInlineNoteProvider(KTextEditor::Document *doc)
: m_doc(doc)
{
// initialize the color regex
updateColorMatchingCriteria();
s_colorRegEx.setPatternOptions(QRegularExpression::DontCaptureOption);
m_colorRegEx.setPatternOptions(QRegularExpression::DontCaptureOption | QRegularExpression::CaseInsensitiveOption);
for (auto view : m_doc->views()) {
qobject_cast<KTextEditor::InlineNoteInterface *>(view)->registerInlineNoteProvider(this);
......@@ -98,7 +95,7 @@ void ColorPickerInlineNoteProvider::updateColorMatchingCriteria()
// sort by decreasing number of digits to maximize matched hex
std::sort(matchHexLengths.rbegin(), matchHexLengths.rend());
if (matchHexLengths.size() > 0) {
colorRegex = QLatin1String("#(%1)(?![[:xdigit:]])");
colorRegex = QLatin1String("#(%1)(?![-\\w])");
QStringList hexRegex;
for (const int hexLength : matchHexLengths) {
hexRegex.append(QStringLiteral("[[:xdigit:]]{%1}").arg(hexLength));
......@@ -139,8 +136,8 @@ void ColorPickerInlineNoteProvider::updateColorMatchingCriteria()
colorRegex = QLatin1String("(?!)");
}
s_colorRegEx.setPattern(colorRegex);
s_putPreviewAfterColor = config.readEntry("PreviewAfterColor", true);
m_colorRegEx.setPattern(colorRegex);
m_putPreviewAfterColor = config.readEntry("PreviewAfterColor", true);
}
void ColorPickerInlineNoteProvider::updateNotes(int startLine, int endLine)
......@@ -169,12 +166,12 @@ QVector<int> ColorPickerInlineNoteProvider::inlineNotes(int line) const
if (!m_colorNoteIndices.contains(line)) {
m_colorNoteIndices.insert(line, {});
auto matchIterator = s_colorRegEx.globalMatch(m_doc->line(line).toLower());
auto matchIterator = m_colorRegEx.globalMatch(m_doc->line(line));
while (matchIterator.hasNext()) {
const auto match = matchIterator.next();
int colorOtherIndex = match.capturedStart();
int colorNoteIndex = colorOtherIndex + match.capturedLength();
if (!s_putPreviewAfterColor) {
if (!m_putPreviewAfterColor) {
colorOtherIndex = colorNoteIndex;
colorNoteIndex = match.capturedStart();
}
......@@ -280,10 +277,9 @@ void KateColorPickerPlugin::addDocument(KTextEditor::Document *doc)
});
}
void KateColorPickerPlugin::readConfig()
{
ColorPickerInlineNoteProvider::updateColorMatchingCriteria();
void KateColorPickerPlugin::readConfig() {
for (auto colorNoteProvider : m_inlineColorNoteProviders.values()) {
colorNoteProvider->updateColorMatchingCriteria();
colorNoteProvider->updateNotes();
}
}
......
......@@ -27,7 +27,7 @@ public:
ColorPickerInlineNoteProvider(KTextEditor::Document *doc);
~ColorPickerInlineNoteProvider();
static void updateColorMatchingCriteria();
void updateColorMatchingCriteria();
// if startLine == -1, update all notes. endLine is inclusive and optional
void updateNotes(int startLine = -1, int endLine = -1);
......@@ -45,8 +45,8 @@ private:
mutable QHash<int, QHash<int, int>> m_colorNoteIndices;
// config variables shared between all note providers
static QRegularExpression s_colorRegEx;
static bool s_putPreviewAfterColor;
QRegularExpression m_colorRegEx;
bool m_putPreviewAfterColor;
};
class KateColorPickerPlugin : public KTextEditor::Plugin
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment