-
-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathFinder.cpp
More file actions
270 lines (216 loc) · 9.28 KB
/
Finder.cpp
File metadata and controls
270 lines (216 loc) · 9.28 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* This file is part of Notepad Next.
* Copyright 2019 Justin Dailey
*
* Notepad Next is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Notepad Next is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Notepad Next. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Finder.h"
#include "UndoAction.h"
Finder::Finder(ScintillaNext *edit) :
editor(edit)
{
search_flags = editor->searchFlags();
}
void Finder::setEditor(ScintillaNext *editor)
{
this->editor = editor;
}
void Finder::setSearchFlags(int flags)
{
this->search_flags = flags;
}
void Finder::setWrap(bool wrap)
{
this->wrap = wrap;
}
void Finder::setSearchText(const QString &text)
{
this->text = text;
}
void Finder::setSelectionRange(Sci_CharacterRange range)
{
use_selection_range = true;
selection_range = range;
}
void Finder::clearSelectionRange()
{
use_selection_range = false;
}
Sci_CharacterRange Finder::findNext(int startPos)
{
did_latest_search_wrap = false;
if (text.isEmpty())
return {INVALID_POSITION, INVALID_POSITION};
const QByteArray textData = text.toUtf8();
if (use_selection_range) {
// Clamp start position to within the selection
const int rangeEnd = selection_range.cpMax;
int pos = startPos == INVALID_POSITION ? editor->selectionEnd() : startPos;
if (pos < selection_range.cpMin) pos = selection_range.cpMin;
if (pos > rangeEnd) pos = rangeEnd;
editor->setTargetRange(pos, rangeEnd);
editor->setSearchFlags(search_flags);
if (editor->searchInTarget(textData.length(), textData.constData()) != INVALID_POSITION) {
return {static_cast<Sci_PositionCR>(editor->targetStart()), static_cast<Sci_PositionCR>(editor->targetEnd())};
}
// Wrap within selection
if (wrap && pos > selection_range.cpMin) {
editor->setTargetRange(selection_range.cpMin, pos);
if (editor->searchInTarget(textData.length(), textData.constData()) != INVALID_POSITION) {
did_latest_search_wrap = true;
return {static_cast<Sci_PositionCR>(editor->targetStart()), static_cast<Sci_PositionCR>(editor->targetEnd())};
}
}
return {INVALID_POSITION, INVALID_POSITION};
}
const int pos = startPos == INVALID_POSITION ? editor->selectionEnd() : startPos;
editor->setTargetRange(pos, editor->length());
editor->setSearchFlags(search_flags);
if (editor->searchInTarget(textData.length(), textData.constData()) != INVALID_POSITION) {
return {static_cast<Sci_PositionCR>(editor->targetStart()), static_cast<Sci_PositionCR>(editor->targetEnd())};
}
else if (wrap) {
editor->setTargetRange(0, pos);
if (editor->searchInTarget(textData.length(), textData.constData()) != INVALID_POSITION) {
did_latest_search_wrap = true;
return {static_cast<Sci_PositionCR>(editor->targetStart()), static_cast<Sci_PositionCR>(editor->targetEnd())};
}
}
return {INVALID_POSITION, INVALID_POSITION};
}
Sci_CharacterRange Finder::findPrev()
{
did_latest_search_wrap = false;
if (text.isEmpty())
return {INVALID_POSITION, INVALID_POSITION};
const QByteArray textData = text.toUtf8();
if (use_selection_range) {
int pos = editor->selectionStart();
if (pos > selection_range.cpMax) pos = selection_range.cpMax;
if (pos < selection_range.cpMin) pos = selection_range.cpMin;
editor->setSearchFlags(search_flags);
auto range = editor->findText(search_flags, textData.constData(), pos, selection_range.cpMin);
if (range.first != INVALID_POSITION) {
return {static_cast<Sci_PositionCR>(range.first), static_cast<Sci_PositionCR>(range.second)};
}
// Wrap within selection
if (wrap && pos < selection_range.cpMax) {
range = editor->findText(search_flags, textData.constData(), selection_range.cpMax, pos);
if (range.first != INVALID_POSITION) {
did_latest_search_wrap = true;
return {static_cast<Sci_PositionCR>(range.first), static_cast<Sci_PositionCR>(range.second)};
}
}
return {INVALID_POSITION, INVALID_POSITION};
}
const int pos = editor->selectionStart();
editor->setTargetRange(pos, editor->length());
editor->setSearchFlags(search_flags);
auto range = editor->findText(editor->searchFlags(), textData.constData(), pos, 0);
if (range.first != INVALID_POSITION) {
return {static_cast<Sci_PositionCR>(range.first), static_cast<Sci_PositionCR>(range.second)};
}
else if (wrap) {
range = editor->findText(editor->searchFlags(), textData.constData(), editor->length(), pos);
if (range.first != INVALID_POSITION) {
did_latest_search_wrap = true;
return {static_cast<Sci_PositionCR>(range.first), static_cast<Sci_PositionCR>(range.second)};
}
}
return {INVALID_POSITION, INVALID_POSITION};
}
// Count all occurrences in the document (or selection if set)
int Finder::count()
{
int total = 0;
if (text.length() > 0) {
auto counter = [&](int start, int end) {
Q_UNUSED(start);
total++;
return end;
};
if (use_selection_range)
forEachMatchInRange(counter, selection_range);
else
forEachMatch(counter);
}
return total;
}
Sci_CharacterRange Finder::replaceSelectionIfMatch(const QString &replaceText)
{
const QByteArray textData = text.toUtf8();
const bool isRegex = search_flags & SCFIND_REGEXP;
const Sci_PositionCR selectionStart = editor->selectionStart();
const Sci_PositionCR selectionEnd = editor->selectionEnd();
if (use_selection_range && (selectionStart < selection_range.cpMin || selectionEnd > selection_range.cpMax))
return {INVALID_POSITION, INVALID_POSITION};
// Search just in the selection to see if the current selection is a match
editor->setTargetStart(selectionStart);
editor->setTargetEnd(selectionEnd);
editor->setSearchFlags(search_flags);
if (editor->searchInTarget(textData.length(), textData.constData()) != INVALID_POSITION) {
const QByteArray replaceData = replaceText.toUtf8();
const Sci_PositionCR matchStart = editor->targetStart();
const Sci_PositionCR matchEnd = editor->targetEnd();
int replaceLength;
if (isRegex)
replaceLength = editor->replaceTargetRE(replaceData.length(), replaceData.constData());
else
replaceLength = editor->replaceTarget(replaceData.length(), replaceData.constData());
if (use_selection_range)
selection_range.cpMax += replaceLength - (matchEnd - matchStart);
return {matchStart, static_cast<Sci_PositionCR>(matchStart + replaceLength)};
}
return {INVALID_POSITION, INVALID_POSITION};
}
int Finder::replaceAll(const QString &replaceText)
{
if (text.isEmpty())
return 0;
const QByteArray &replaceData = replaceText.toUtf8();
const QByteArray &b = text.toUtf8();
const char *c = b.constData();
const Sci_PositionCR rangeStart = use_selection_range ? selection_range.cpMin : 0;
Sci_PositionCR rangeEnd = use_selection_range ? selection_range.cpMax : (Sci_PositionCR)editor->length();
Sci_TextToFind ttf {{rangeStart, rangeEnd}, c, {-1, -1}};
const bool isRegex = search_flags & SCFIND_REGEXP;
int total = 0;
// Don't technically need to set the search flags here but do it just in case something looks at the search flags later
editor->setSearchFlags(search_flags);
// NOTE: can't use editor->forEachMatch() here since the search range can grow since the document is changing
const UndoAction ua(editor);
while (editor->send(SCI_FINDTEXT, search_flags, reinterpret_cast<sptr_t>(&ttf)) != -1) {
const int start = ttf.chrgText.cpMin;
const int end = ttf.chrgText.cpMax;
editor->setTargetRange(start, end);
if (isRegex)
ttf.chrg.cpMin = start + editor->replaceTargetRE(replaceData.length(), replaceData.constData());
else
ttf.chrg.cpMin = start + editor->replaceTarget(replaceData.length(), replaceData.constData());
// Update the end of the search range based on the length delta of this replacement.
// When confined to a selection, track the selection boundary precisely; otherwise
// expand to the full (possibly grown) document length.
if (use_selection_range) {
rangeEnd += ttf.chrg.cpMin - end; // delta = newLength - oldMatchLength
ttf.chrg.cpMax = rangeEnd;
} else {
ttf.chrg.cpMax = editor->length();
}
total++;
}
// Keep the stored selection range in sync with the post-replacement boundary
if (use_selection_range)
selection_range.cpMax = rangeEnd;
return total;
}