Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search/replace for line break #5027

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ext/searchbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ var SearchBox = function(editor, range, showReplaceForm) {
? editor.session.getTextRange(this.searchRange)
: editor.getValue();

if (editor.$search.$isMultilineSearch(editor.$search.$options))
value = value.replace(/\r\n|\r|\n/g, "\n");

var offset = editor.session.doc.positionToIndex(editor.selection.anchor);
if (this.searchRange)
offset -= editor.session.doc.positionToIndex(this.searchRange.start);
Expand Down
184 changes: 150 additions & 34 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,16 @@ var Search = function() {
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));
for (var matches, i = 0; i < lines.length; i++) {
if (this.$isMultilineSearch(options)) {
matches = this.$multiLineForward(re, i, lines.length);
ranges.push(new Range(matches.startRow, matches.startCol, matches.endRow, matches.endCol));
} else {
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));
}
}
}
}
Expand Down Expand Up @@ -187,6 +192,9 @@ var Search = function() {

if (!re)
return;

if (this.$isMultilineSearch(options))
input = input.replace(/\r\n|\r|\n/g, "\n");

var match = re.exec(input);
if (!match || match[0].length != input.length)
Expand Down Expand Up @@ -247,11 +255,100 @@ var Search = function() {
}
return re;
};

this.$isMultilineSearch = function(options) {
var regexp = typeof options.regExp === 'undefined' ? false : options.regExp;
return /\\r\\n|\\r|\\n/.test(options.re.source) && regexp && !options.$isMultiLine;
};

this.$multiLineForward = function(re, start, last) {
var line, chunk = 1;
for (var row = start; row <= last;) {
for (var i = 0; i < chunk; i++) {
if (row > last)
break;
var next = session.getLine(row++);
line = line == null ? next : line + "\n" + next;
}
chunk = chunk * 2;

var match = re.exec(line);
if (match) {
var before = line.slice(0, match.index).split("\n");
var inside = match[0].split("\n");
var startRow = start + before.length - 1;
var startCol = before[before.length - 1].length;
var endRow = startRow + inside.length - 1;
var endCol = inside.length == 1 ? startCol + inside[0].length : inside[inside.length - 1].length;

return {
startRow: startRow,
startCol: startCol,
endRow: endRow,
endCol: endCol
};
}
}

return false;
};

function multiLineBackwardMatch(line, re, endMargin) {
var match, from = 0;
while (from <= line.length) {
re.lastIndex = from;
var newMatch = re.exec(line);
if (!newMatch)
break;
var end = newMatch.index + newMatch[0].length;
if (end > line.length - endMargin)
break;
if (!match || end > match.index + match[0].length)
match = newMatch;
from = newMatch.index + 1;
}
return match;
}

this.$multiLineBackward = function(re, endIndex, start, first) {
var line, chunk = 1, endMargin = session.getLine(start).length - endIndex;
for (var row = start; row >= first;) {
for (var i = 0; i < chunk && row >= first; i++) {
var next = session.getLine(row--);
line = line == null ? next : next + "\n" + line;
}
chunk = chunk * 2;

var match = multiLineBackwardMatch(line, re, endMargin);
if (match) {
var before = line.slice(0, match.index).split("\n");
var inside = match[0].split("\n");
var startRow = row + before.length;
var startCol = before[before.length - 1].length;
var endRow = startRow + inside.length - 1;
var endCol = inside.length == 1 ? startCol + inside[0].length : inside[inside.length - 1].length;

return {
startRow: startRow,
startCol: startCol,
endRow: endRow,
endCol: endCol
};
}
}

return false;
};

this.$matchIterator = function(session, options) {
var re = this.$assembleRegExp(options);
if (!re)
return false;

var multiline = this.$isMultilineSearch(options);
var multilineForward = this.$multiLineForward;
var multilineBackward = this.$multiLineBackward;

var backwards = options.backwards == true;
var skipCurrent = options.skipCurrent != false;

Expand Down Expand Up @@ -318,43 +415,62 @@ var Search = function() {
}
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 += 1;
}
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))
if (multiline) {
var pos = multilineBackward(re, endIndex, row, firstRow);
// console.log(pos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment should be removed.

if (!pos)
return false;
if (callback(pos.startRow, pos.startCol, pos.endRow, pos.endCol))
return true;
}
else {
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 += 1;
}
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))
if (multiline) {
var pos = multilineForward(re, row, lastRow);
if (!pos)
return false;
if (callback(pos.startRow, pos.startCol, pos.endRow, pos.endCol))
return true;
if (!length) {
re.lastIndex = last += 1;
if (last >= line.length) return false;
}
else {
var line = session.getLine(row);
var last;
var m;
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 += 1;
if (last >= line.length) return false;
}
}
}
};
Expand Down
63 changes: 56 additions & 7 deletions src/search_highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,71 @@ var SearchHighlight = function(regExp, clazz, type) {
this.regExp = regExp;
this.cache = [];
};

this.isMultilineSearch = function() {
if (typeof editor !== 'undefined' && 'searchBox' in editor && editor.searchBox.active && editor.searchBox.element.contains(document.activeElement)) {
var options = editor.getLastSearchOptions();
var regexp = typeof options.regExp === 'undefined' ? false : options.regExp;
return /\\r\\n|\\r|\\n/.test(options.re.source) && regexp;
}
else {
return false;
}
};

this.multiLine = function(start, end) {
var line, chunk = 1;
for (var row = start; row <= end;) {
for (var i = 0; i < chunk; i++) {
if (row > end)
break;
var current = session.getLine(row++);
line = line == null ? current : line + "\n" + current;
}
chunk = chunk * 2;

var ranges = [];
var match = this.regExp.exec(line);
if (match) {
var before = line.slice(0, match.index).split("\n");
var inside = match[0].split("\n");
var startRow = start + before.length - 1;
var startCol = before[before.length - 1].length;
var endRow = startRow + inside.length - 1;
var endCol = inside.length == 1 ? startCol + inside[0].length : inside[inside.length - 1].length;

ranges.push(new Range(startRow, startCol, endRow, endCol));
if (ranges.length > this.MAX_RANGES)
ranges = ranges.slice(0, this.MAX_RANGES);
return ranges;
}
}

return ranges;
};

this.update = function(html, markerLayer, session, config) {
if (!this.regExp)
return;
var start = config.firstRow, end = config.lastRow;
var multiline = this.isMultilineSearch();
console.log(multiline);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This console.log should probably not be here.

var renderedMarkerRanges = {};

for (var i = start; i <= end; i++) {
var ranges = this.cache[i];
if (ranges == null) {
ranges = lang.getMatchOffsets(session.getLine(i), this.regExp);
if (ranges.length > this.MAX_RANGES)
ranges = ranges.slice(0, this.MAX_RANGES);
ranges = ranges.map(function(match) {
return new Range(i, match.offset, i, match.offset + match.length);
});
if (multiline) {
ranges = this.multiLine(i, end);
}
else {
ranges = lang.getMatchOffsets(session.getLine(i), this.regExp);
if (ranges.length > this.MAX_RANGES)
ranges = ranges.slice(0, this.MAX_RANGES);
ranges = ranges.map(function(match) {
return new Range(i, match.offset, i, match.offset + match.length);
});
}
this.cache[i] = ranges.length ? ranges : "";
}

Expand All @@ -45,7 +94,7 @@ var SearchHighlight = function(regExp, clazz, type) {
if (renderedMarkerRanges[rangeAsString]) continue;

renderedMarkerRanges[rangeAsString] = true;
markerLayer.drawSingleLineMarker(
markerLayer[multiline ? 'drawMultiLineMarker' : 'drawSingleLineMarker'](
html, rangeToAddMarkerTo, this.clazz, config);
}
}
Expand Down