Skip to content

Commit b7711f6

Browse files
committed
[searchcursor addon] Add multiline flag to regexp when searching multiline
Closes codemirror#5238
1 parent 4ddf544 commit b7711f6

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

addon/search/searchcursor.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
+ (regexp.multiline ? "m" : "")
2020
}
2121

22-
function ensureGlobal(regexp) {
23-
return regexp.global ? regexp : new RegExp(regexp.source, regexpFlags(regexp) + "g")
22+
function ensureFlags(regexp, flags) {
23+
var current = regexpFlags(regexp), target = current
24+
for (var i = 0; i < flags.length; i++) if (target.indexOf(flags.charAt(i)) == -1)
25+
target += flags.charAt(i)
26+
return current == target ? regexp : new RegExp(regexp.source, target)
2427
}
2528

2629
function maybeMultiline(regexp) {
2730
return /\\s|\\n|\n|\\W|\\D|\[\^/.test(regexp.source)
2831
}
2932

3033
function searchRegexpForward(doc, regexp, start) {
31-
regexp = ensureGlobal(regexp)
34+
regexp = ensureFlags(regexp, "g")
3235
for (var line = start.line, ch = start.ch, last = doc.lastLine(); line <= last; line++, ch = 0) {
3336
regexp.lastIndex = ch
3437
var string = doc.getLine(line), match = regexp.exec(string)
@@ -42,7 +45,7 @@
4245
function searchRegexpForwardMultiline(doc, regexp, start) {
4346
if (!maybeMultiline(regexp)) return searchRegexpForward(doc, regexp, start)
4447

45-
regexp = ensureGlobal(regexp)
48+
regexp = ensureFlags(regexp, "gm")
4649
var string, chunk = 1
4750
for (var line = start.line, last = doc.lastLine(); line <= last;) {
4851
// This grows the search buffer in exponentially-sized chunks
@@ -51,6 +54,7 @@
5154
// searching for something that has tons of matches), but at the
5255
// same time, the amount of retries is limited.
5356
for (var i = 0; i < chunk; i++) {
57+
if (line > last) break
5458
var curLine = doc.getLine(line++)
5559
string = string == null ? curLine : string + "\n" + curLine
5660
}
@@ -81,7 +85,7 @@
8185
}
8286

8387
function searchRegexpBackward(doc, regexp, start) {
84-
regexp = ensureGlobal(regexp)
88+
regexp = ensureFlags(regexp, "g")
8589
for (var line = start.line, ch = start.ch, first = doc.firstLine(); line >= first; line--, ch = -1) {
8690
var string = doc.getLine(line)
8791
if (ch > -1) string = string.slice(0, ch)
@@ -94,7 +98,7 @@
9498
}
9599

96100
function searchRegexpBackwardMultiline(doc, regexp, start) {
97-
regexp = ensureGlobal(regexp)
101+
regexp = ensureFlags(regexp, "gm")
98102
var string, chunk = 1
99103
for (var line = start.line, first = doc.firstLine(); line >= first;) {
100104
for (var i = 0; i < chunk; i++) {
@@ -213,7 +217,7 @@
213217
return (reverse ? searchStringBackward : searchStringForward)(doc, query, pos, caseFold)
214218
}
215219
} else {
216-
query = ensureGlobal(query)
220+
query = ensureFlags(query, "gm")
217221
if (!options || options.multiline !== false)
218222
this.matches = function(reverse, pos) {
219223
return (reverse ? searchRegexpBackwardMultiline : searchRegexpForwardMultiline)(doc, query, pos)

0 commit comments

Comments
 (0)