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

feat: Multiline Search Support: line breaks \n #5675

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
9 changes: 4 additions & 5 deletions src/edit_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class EditSession {

/**
* Get "widgetManager" from EditSession
*
*
* @returns {LineWidgets} object
*/
get widgetManager() {
Expand All @@ -202,18 +202,18 @@ class EditSession {

if (this.$editor)
widgetManager.attach(this.$editor);

return widgetManager;
}

/**
* Set "widgetManager" in EditSession
*
*
* @returns void
*/
set widgetManager(value) {
Object.defineProperty(this, "widgetManager", {
writable: true,
writable: true,
enumerable: true,
configurable: true,
value: value,
Expand Down Expand Up @@ -2747,4 +2747,3 @@ config.defineOptions(EditSession.prototype, "session", {
});

exports.EditSession = EditSession;

4 changes: 4 additions & 0 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ class Editor {

// Update cursor because tab characters can influence the cursor position.
this.$cursorChange();

// Updates "updateCounter" and "ace_nomatch" in the search box
if (this.searchBox && this.searchBox.active === true)
this.searchBox.find(false, false, true);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please move this to an input event handler in searchbox, something like editor.on('input', this.$onEditorInput) and remove the listener when deactivating. onDocumentChange can be called multiple times during one edit, and input event debounces that with a timeout.
Also in one of tests add a scenario of changing editor value, e.g.

editor.insert("searchedText") 
setTimeout(function() {
 // verify that search counter have been updated
}, 50)

to prevent coverlay complaining about untested lines

}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/ext/searchbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class SearchBox {
? editor.session.getTextRange(this.searchRange)
: editor.getValue();

if (editor.$search.$isMultilineSearch(editor.getLastSearchOptions()))
value = value.replace(/\r\n|\r|\n/g, "\n");
nlujjawal marked this conversation as resolved.
Show resolved Hide resolved

var offset = editor.session.doc.positionToIndex(editor.selection.anchor);
if (this.searchRange)
offset -= editor.session.doc.positionToIndex(this.searchRange.start);
Expand Down Expand Up @@ -290,6 +293,9 @@ class SearchBox {
this.element.style.display = "";
this.replaceOption.checked = isReplace;

if (this.editor.$search.$options.regExp)
value = lang.escapeRegExp(value);

if (value)
this.searchInput.value = value;

Expand Down
271 changes: 234 additions & 37 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ class Search {
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(session, re, i, lines.length);
ranges.push(new Range(matches.startRow, matches.startCol, matches.endRow, matches.endCol));
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 as the previous case

Copy link
Author

Choose a reason for hiding this comment

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

ACK.

}
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 All @@ -146,6 +152,74 @@ class Search {
return ranges;
}

parseReplaceString(input, replaceString) {
var CharCode = {
DollarSign: 36,
Ampersand: 38,
Digit0: 48,
Digit1: 49,
Digit9: 57,
Backslash: 92,
n: 110,
t: 116
};

var replacement = '';
for (var i = 0, len = replaceString.length; i < len; i++) {
var chCode = replaceString.charCodeAt(i);
if (chCode === CharCode.Backslash) {
// move to next char
i++;
if (i >= len) {
// string ends with a \
break;
}
var nextChCode = replaceString.charCodeAt(i);
switch (nextChCode) {
case CharCode.Backslash:
// \\ => inserts a "\"
replacement = '\\';
break;
case CharCode.n:
// \n => inserts a LF
replacement= '\n';
break;
case CharCode.t:
// \t => inserts a TAB
replacement = '\t';
break;
}
continue;
}

if (chCode === CharCode.DollarSign) {
// move to next char
i++;
if (i >= len) {
// string ends with a $
break;
}
const nextChCode = replaceString.charCodeAt(i);
if (nextChCode === CharCode.DollarSign) {
// $$ => inserts a "$"
replacement = '$';
continue;
}
if (nextChCode === CharCode.Digit0 || nextChCode === CharCode.Ampersand) {
// $& and $0 => inserts the matched substring.
replacement = input;
continue;
}
if (CharCode.Digit1 <= nextChCode && nextChCode <= CharCode.Digit9) {
// $n
replacement = replaceString;
continue;
}
}
}
return replacement;
}

/**
* Searches for `options.needle` in `input`, and, if found, replaces it with `replacement`.
* @param {String} input The text to search in
Expand All @@ -166,11 +240,15 @@ class Search {
if (!re)
return;

if (this.$isMultilineSearch(options))
Copy link
Contributor

Choose a reason for hiding this comment

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

these are used at 2 places should we create another method as this looks like the reusable code.

Copy link
Author

Choose a reason for hiding this comment

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

ACK.

input = input.replace(/\r\n|\r|\n/g, "\n");

var match = re.exec(input);
if (!match || match[0].length != input.length)
if (!match || (!this.$isMultilineSearch(options) && match[0].length != input.length))
return null;
if (!options.regExp) {
replacement = replacement.replace(/\$/g, "$$$$");

if (options.regExp) {
replacement = this.parseReplaceString(input, replacement);
}

replacement = input.replace(re, replacement);
Expand Down Expand Up @@ -248,13 +326,86 @@ class Search {
return re;
}

$isMultilineSearch(options) {
return options.re && /\\r\\n|\\r|\\n/.test(options.re.source) && options.regExp && !options.$isMultiLine;
nlujjawal marked this conversation as resolved.
Show resolved Hide resolved
}

$multiLineForward(session, re, start, last) {
var line,
chunk = chunkEnd(session, start);

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;
}

var match = re.exec(line);
re.lastIndex = 0;
if (match) {
var before = line.slice(0, match.index).split("\n");
nlujjawal marked this conversation as resolved.
Show resolved Hide resolved
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;
}

$multiLineBackward(session, re, endIndex, start, first) {
var line,
chunk = chunkEnd(session, start),
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;
}

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;
}

/**
* @param {EditSession} session
*/
$matchIterator(session, options) {
var re = this.$assembleRegExp(options);
if (!re)
return false;

var multiline = this.$isMultilineSearch(options);
var mtForward = this.$multiLineForward;
var mtBackward = this.$multiLineBackward;

var backwards = options.backwards == true;
var skipCurrent = options.skipCurrent != false;
var supportsUnicodeFlag = re.unicode;
Expand Down Expand Up @@ -322,43 +473,61 @@ class Search {
}
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))
if (multiline) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we reset regex lastIndex above this line just to reset it?
re.lastIndex = 0;

Copy link
Author

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, I wasn’t focused earlier.

No need, because it’s already set in multiLineBackwardMatch.

var pos = mtBackward(session, re, endIndex, row, firstRow);
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 += 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))
if (multiline) {
var pos = mtForward(session, re, row, lastRow);
nlujjawal marked this conversation as resolved.
Show resolved Hide resolved
if (!pos)
return false;
if (callback(pos.startRow, pos.startCol, pos.endRow, pos.endCol))
return true;
if (!length) {
re.lastIndex = last += lang.skipEmptyMatch(line, last, supportsUnicodeFlag);
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 += lang.skipEmptyMatch(line, last, supportsUnicodeFlag);
if (last >= line.length) return false;
}
}
}
};
Expand Down Expand Up @@ -397,4 +566,32 @@ function addWordBoundary(needle, options) {
return wordBoundary(firstChar) + needle + wordBoundary(lastChar, 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;
}

function chunkEnd(session, start) {
var base = 5000,
startPosition = { row: start, column: 0 },
startIndex = session.doc.positionToIndex(startPosition),
targetIndex = startIndex + base,
targetPosition = session.doc.indexToPosition(targetIndex),
targetLine = targetPosition.row;
return targetLine + 1;
}

exports.Search = Search;
Loading
Loading