-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_highlight_plugin_view.cpp
169 lines (154 loc) · 6.16 KB
/
text_highlight_plugin_view.cpp
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
163
164
165
166
167
168
169
#include "text_highlight_plugin_view.h"
#include "logger.h"
#include <algorithm>
QObject *TextHighlightPluginView::createView(TextHighlightPlugin *plugin, KTextEditor::MainWindow *mainWindow)
{
return new TextHighlightPluginView(plugin, mainWindow);
}
void TextHighlightPluginView::onViewChanged(KTextEditor::View *view)
{
auto oldView = m_activeView;
if (oldView == view) {
return;
}
m_activeView = view;
if (view && view->focusProxy()) {
view->focusProxy()->installEventFilter(this);
connect(view, &KTextEditor::View::verticalScrollPositionChanged, this, &TextHighlightPluginView::onVerticalScrollPositionChanged);
//
// https://api.kde.org/frameworks/ktexteditor/html/classKTextEditor_1_1MovingRange.html
// Chapter `MovingRange Example`
//
connect(view->document(),
&KTextEditor::Document::aboutToInvalidateMovingInterfaceContent,
this,
&TextHighlightPluginView::clearMovingRanges,
Qt::UniqueConnection);
connect(view->document(),
&KTextEditor::Document::aboutToDeleteMovingInterfaceContent,
this,
&TextHighlightPluginView::clearMovingRanges,
Qt::UniqueConnection);
onVerticalScrollPositionChanged();
}
if (oldView && oldView->focusProxy()) {
oldView->focusProxy()->removeEventFilter(this);
disconnect(oldView, &KTextEditor::View::verticalScrollPositionChanged, this, &TextHighlightPluginView::onVerticalScrollPositionChanged);
}
}
void TextHighlightPluginView::onVerticalScrollPositionChanged()
{
if (m_activeView && !m_stringHighlightData.empty()) {
for (const auto &[str, data] : m_stringHighlightData) {
if (!data.highlightAllMatches) {
continue;
}
highlightAllMatches();
}
}
}
QIcon TextHighlightPluginView::createColorIcon(const QColor &color, int size)
{
QPixmap pixmap(size, size);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(color);
painter.drawRect(0, 0, size, size);
return {pixmap};
}
void TextHighlightPluginView::highlight(bool /*unused*/)
{
auto *action = qobject_cast<QAction *>(sender());
auto color = QColor(action->iconText());
auto selectionText = m_mainWindow->activeView()->selectionText();
if (selectionText.isEmpty()) {
return;
}
QString lowerSelectionText = selectionText.toLower();
if (!m_caseSensitive->isChecked()) {
std::erase_if(m_stringHighlightData, [&lowerSelectionText](const auto &pair) {
return pair.first.toLower() == lowerSelectionText;
});
std::erase_if(m_movingRanges[m_activeView->document()], [&lowerSelectionText](const auto &pair) {
return pair.first.toLower() == lowerSelectionText;
});
} else {
std::for_each(m_stringHighlightData.begin(), m_stringHighlightData.end(), [&lowerSelectionText](auto &pair) {
if (pair.first.toLower() == lowerSelectionText) {
pair.second.caseSensitive = true;
}
});
m_stringHighlightData.erase(selectionText);
m_movingRanges[m_activeView->document()].erase(selectionText);
}
auto it = m_stringHighlightData.find(selectionText);
if (it != m_stringHighlightData.end()) {
it->second.color = color;
it->second.highlightAllMatches = m_highlightAllMatches->isChecked();
it->second.caseSensitive = m_caseSensitive->isChecked();
} else {
m_stringHighlightData.insert({selectionText, HighlightData(color, m_highlightAllMatches->isChecked(), m_caseSensitive->isChecked())});
}
if (m_highlightAllMatches->isChecked()) {
highlightAllMatches();
} else {
highlightMatch(selectionText, m_mainWindow->activeView()->selectionRange(), color);
}
Logger::Log(Logger::INFO, selectionText, m_mainWindow);
}
void TextHighlightPluginView::highlightAllMatches(KTextEditor::Range range)
{
if (!m_mainWindow || !m_mainWindow->activeView()) {
return;
}
auto lineRange = range.toLineRange();
auto *doc = m_activeView->document();
const int startLine = lineRange.isValid() ? lineRange.start() : m_activeView->firstDisplayedLine();
const int endLine = lineRange.isValid() ? lineRange.end() : m_activeView->lastDisplayedLine();
auto &strs = m_movingRanges[doc];
if (range.isValid()) {
for (auto &[_, v] : strs) {
v.erase(std::remove_if(v.begin(),
v.end(),
[lineRange](const std::unique_ptr<KTextEditor::MovingRange> &r) {
return lineRange.overlapsLine(r->start().line());
}),
v.end());
}
} else {
for (auto &[_, v] : strs) {
v.clear();
}
}
for (int line = startLine; line < endLine; line++) {
for (const auto &[str, highlightData] : m_stringHighlightData) {
QString content = doc->line(line);
for (qsizetype i = 0;;) {
i = content.indexOf(str, i, highlightData.caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
if (i == -1) {
// Not found
break;
}
highlightMatch(str, KTextEditor::Range(line, i, line, i + str.size()), highlightData.color);
i += str.size();
}
}
}
}
void TextHighlightPluginView::highlightMatch(const QString &str, KTextEditor::Range range, QColor color)
{
KTextEditor::MovingRange *movingRange = m_activeView->document()->newMovingRange(range);
const KTextEditor::Attribute::Ptr attr([color] {
auto *attr = new KTextEditor::Attribute;
QBrush brush(color, Qt::BrushStyle::SolidPattern);
attr->setBackground(brush);
return attr;
}());
movingRange->setAttribute(attr);
m_movingRanges[m_activeView->document()][str].emplace_back(movingRange);
}
void TextHighlightPluginView::clearMovingRanges()
{
m_movingRanges.clear();
}