forked from ajaxorg/ace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
414 lines (368 loc) · 14.9 KB
/
search.js
File metadata and controls
414 lines (368 loc) · 14.9 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
"use strict";
/**
* @typedef {import("./edit_session").EditSession} EditSession
*/
var lang = require("./lib/lang");
var oop = require("./lib/oop");
var Range = require("./range").Range;
/**
* A class designed to handle all sorts of text searches within a [[Document `Document`]].
**/
class Search {
/**
* Creates a new `Search` object. The following search options are available:
* @typedef SearchOptions
*
* @property {string|RegExp} [needle] - The string or regular expression you're looking for
* @property {boolean} [backwards] - Whether to search backwards from where cursor currently is
* @property {boolean} [wrap] - Whether to wrap the search back to the beginning when it hits the end
* @property {boolean} [caseSensitive] - Whether the search ought to be case-sensitive
* @property {boolean} [wholeWord] - Whether the search matches only on whole words
* @property {Range|null} [range] - The [[Range]] to search within. Set this to `null` for the whole document
* @property {boolean} [regExp] - Whether the search is a regular expression or not
* @property {Range|import("../ace-internal").Ace.Position} [start] - The starting [[Range]] or cursor position to begin the search
* @property {boolean} [skipCurrent] - Whether or not to include the current line in the search
* @property {boolean} [$isMultiLine] - true, if needle has \n or \r\n
* @property {boolean} [preserveCase]
* @property {boolean} [preventScroll]
* @property {boolean} [$supportsUnicodeFlag] - internal property, determine if browser supports unicode flag
* @property {any} [re]
**/
constructor() {
/**@type {SearchOptions}*/
this.$options = {};
}
/**
* Sets the search options via the `options` parameter.
* @param {Partial<import("../ace-internal").Ace.SearchOptions>} options An object containing all the new search properties
* @returns {Search}
* @chainable
**/
set(options) {
oop.mixin(this.$options, options);
return this;
}
/**
* [Returns an object containing all the search options.]{: #Search.getOptions}
* @returns {Partial<import("../ace-internal").Ace.SearchOptions>}
**/
getOptions() {
return lang.copyObject(this.$options);
}
/**
* Sets the search options via the `options` parameter.
* @param {SearchOptions} options object containing all the search propertie
* @related Search.set
**/
setOptions(options) {
this.$options = options;
}
/**
* Searches for `options.needle`. If found, this method returns the [[Range `Range`]] where the text first occurs. If `options.backwards` is `true`, the search goes backwards in the session.
* @param {EditSession} session The session to search with
* @returns {Range|false}
**/
find(session) {
var options = this.$options;
var iterator = this.$matchIterator(session, options);
if (!iterator)
return false;
var firstRange = null;
iterator.forEach(function(sr, sc, er, ec) {
firstRange = new Range(sr, sc, er, ec);
if (sc == ec && options.start && /**@type{Range}*/(options.start).start
&& options.skipCurrent != false && firstRange.isEqual(/**@type{Range}*/(options.start))
) {
firstRange = null;
return false;
}
return true;
});
return firstRange;
}
/**
* Searches for all occurrances `options.needle`. If found, this method returns an array of [[Range `Range`s]] where the text first occurs. If `options.backwards` is `true`, the search goes backwards in the session.
* @param {EditSession} session The session to search with
* @returns {Range[]}
**/
findAll(session) {
var options = this.$options;
if (!options.needle)
return [];
this.$assembleRegExp(options);
var range = options.range;
var lines = range
? session.getLines(range.start.row, range.end.row)
: session.doc.getAllLines();
var ranges = [];
var re = options.re;
if (options.$isMultiLine) {
var len = re.length;
var maxRow = lines.length - len;
var prevRange;
outer: for (var row = re.offset || 0; row <= maxRow; row++) {
for (var j = 0; j < len; j++)
if (lines[row + j].search(re[j]) == -1)
continue outer;
var startLine = lines[row];
var line = lines[row + len - 1];
var startIndex = startLine.length - startLine.match(re[0])[0].length;
var endIndex = line.match(re[len - 1])[0].length;
if (prevRange && prevRange.end.row === row &&
prevRange.end.column > startIndex
) {
continue;
}
ranges.push(prevRange = new Range(
row, startIndex, row + len - 1, endIndex
));
if (len > 2)
row = row + len - 2;
}
} else {
for (var i = 0; i < lines.length; i++) {
var matches = lang.getMatchOffsets(lines[i], re);
for (var j = 0; j < matches.length; j++) {
var match = matches[j];
ranges.push(new Range(i, match.offset, i, match.offset + match.length));
}
}
}
if (range) {
var startColumn = range.start.column;
var endColumn = range.end.column;
var i = 0, j = ranges.length - 1;
while (i < j && ranges[i].start.column < startColumn && ranges[i].start.row == 0)
i++;
var endRow = range.end.row - range.start.row;
while (i < j && ranges[j].end.column > endColumn && ranges[j].end.row == endRow)
j--;
ranges = ranges.slice(i, j + 1);
for (i = 0, j = ranges.length; i < j; i++) {
ranges[i].start.row += range.start.row;
ranges[i].end.row += range.start.row;
}
}
return ranges;
}
/**
* Searches for `options.needle` in `input`, and, if found, replaces it with `replacement`.
* @param {String} input The text to search in
* @param {any} replacement The replacing text
* + (String): If `options.regExp` is `true`, this function returns `input` with the replacement already made. Otherwise, this function just returns `replacement`.<br/>
* If `options.needle` was not found, this function returns `null`.
*
*
* @returns {String}
**/
replace(input, replacement) {
var options = this.$options;
var re = this.$assembleRegExp(options);
if (options.$isMultiLine)
return replacement;
if (!re)
return;
var match = re.exec(input);
if (!match || match[0].length != input.length)
return null;
replacement = input.replace(re, replacement);
if (options.preserveCase) {
replacement = replacement.split("");
for (var i = Math.min(input.length, input.length); i--; ) {
var ch = input[i];
if (ch && ch.toLowerCase() != ch)
replacement[i] = replacement[i].toUpperCase();
else
replacement[i] = replacement[i].toLowerCase();
}
replacement = replacement.join("");
}
return replacement;
}
/**
*
* @param {SearchOptions} options
* @param {boolean} [$disableFakeMultiline]
* @return {RegExp|boolean|*[]|*}
*/
$assembleRegExp(options, $disableFakeMultiline) {
if (options.needle instanceof RegExp)
return options.re = options.needle;
var needle = options.needle;
if (!options.needle)
return options.re = false;
if (!options.regExp)
needle = lang.escapeRegExp(needle);
var modifier = options.caseSensitive ? "gm" : "gmi";
try {
new RegExp(needle, "u");
options.$supportsUnicodeFlag = true;
modifier += "u";
} catch (e) {
options.$supportsUnicodeFlag = false; //left for backward compatibility with previous versions for cases like /ab\{2}/gu
}
if (options.wholeWord)
needle = addWordBoundary(needle, options);
options.$isMultiLine = !$disableFakeMultiline && /[\n\r]/.test(needle);
if (options.$isMultiLine)
return options.re = this.$assembleMultilineRegExp(needle, modifier);
try {
/**@type {RegExp|false}*/
var re = new RegExp(needle, modifier);
} catch(e) {
re = false;
}
return options.re = re;
}
/**
* @param {string} needle
* @param {string} modifier
*/
$assembleMultilineRegExp(needle, modifier) {
var parts = needle.replace(/\r\n|\r|\n/g, "$\n^").split("\n");
var re = [];
for (var i = 0; i < parts.length; i++) try {
re.push(new RegExp(parts[i], modifier));
} catch(e) {
return false;
}
return re;
}
/**
* @param {EditSession} session
*/
$matchIterator(session, options) {
var re = this.$assembleRegExp(options);
if (!re)
return false;
var backwards = options.backwards == true;
var skipCurrent = options.skipCurrent != false;
var supportsUnicodeFlag = re.unicode;
var range = options.range;
var start = options.start;
if (!start)
start = range ? range[backwards ? "end" : "start"] : session.selection.getRange();
if (start.start)
start = start[skipCurrent != backwards ? "end" : "start"];
var firstRow = range ? range.start.row : 0;
var lastRow = range ? range.end.row : session.getLength() - 1;
if (backwards) {
var forEach = function(callback) {
var row = start.row;
if (forEachInLine(row, start.column, callback))
return;
for (row--; row >= firstRow; row--)
if (forEachInLine(row, Number.MAX_VALUE, callback))
return;
if (options.wrap == false)
return;
for (row = lastRow, firstRow = start.row; row >= firstRow; row--)
if (forEachInLine(row, Number.MAX_VALUE, callback))
return;
};
}
else {
var forEach = function(callback) {
var row = start.row;
if (forEachInLine(row, start.column, callback))
return;
for (row = row + 1; row <= lastRow; row++)
if (forEachInLine(row, 0, callback))
return;
if (options.wrap == false)
return;
for (row = firstRow, lastRow = start.row; row <= lastRow; row++)
if (forEachInLine(row, 0, callback))
return;
};
}
if (options.$isMultiLine) {
var len = re.length;
var forEachInLine = function(row, offset, callback) {
var startRow = backwards ? row - len + 1 : row;
if (startRow < 0 || startRow + len > session.getLength()) return;
var line = session.getLine(startRow);
var startIndex = line.search(re[0]);
if (!backwards && startIndex < offset || startIndex === -1) return;
for (var i = 1; i < len; i++) {
line = session.getLine(startRow + i);
if (line.search(re[i]) == -1)
return;
}
var endIndex = line.match(re[len - 1])[0].length;
if (backwards && endIndex > offset) return;
if (callback(startRow, startIndex, startRow + len - 1, endIndex))
return true;
};
}
else if (backwards) {
var forEachInLine = function(row, endIndex, callback) {
var line = session.getLine(row);
var matches = [];
var m, last = 0;
re.lastIndex = 0;
while((m = re.exec(line))) {
var length = m[0].length;
last = m.index;
if (!length) {
if (last >= line.length) break;
re.lastIndex = last += lang.skipEmptyMatch(line, last, supportsUnicodeFlag);
}
if (m.index + length > endIndex)
break;
matches.push(m.index, length);
}
for (var i = matches.length - 1; i >= 0; i -= 2) {
var column = matches[i - 1];
var length = matches[i];
if (callback(row, column, row, column + length))
return true;
}
};
}
else {
var forEachInLine = function(row, startIndex, callback) {
var line = session.getLine(row);
var last;
var m;
re.lastIndex = startIndex;
while((m = re.exec(line))) {
var length = m[0].length;
last = m.index;
if (callback(row, last, row,last + length))
return true;
if (!length) {
re.lastIndex = last += lang.skipEmptyMatch(line, last, supportsUnicodeFlag);
if (last >= line.length) return false;
}
}
};
}
return {forEach: forEach};
}
}
/**
*
* @param {string} needle
* @param {SearchOptions} options
* @return {string}
*/
function addWordBoundary(needle, options) {
let supportsLookbehind = lang.supportsLookbehind();
function wordBoundary(c, firstChar = true) {
let wordRegExp = supportsLookbehind && options.$supportsUnicodeFlag ? new RegExp("[\\p{L}\\p{N}_]","u") : new RegExp("\\w");
if (wordRegExp.test(c) || options.regExp) {
if (supportsLookbehind && options.$supportsUnicodeFlag) {
if (firstChar) return "(?<=^|[^\\p{L}\\p{N}_])";
return "(?=[^\\p{L}\\p{N}_]|$)";
}
return "\\b";
}
return "";
}
let needleArray = Array.from(needle);
let firstChar = needleArray[0];
let lastChar = needleArray[needleArray.length - 1];
return wordBoundary(firstChar) + needle + wordBoundary(lastChar, false);
}
exports.Search = Search;