Skip to content

Commit

Permalink
Merge pull request #4956 from anijanyan/find-all
Browse files Browse the repository at this point in the history
Find all in a range bug fixed
  • Loading branch information
andrewnester authored Oct 18, 2022
2 parents be8eb12 + 8642199 commit bd3573c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ var Search = function() {

if (range) {
var startColumn = range.start.column;
var endColumn = 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 == range.start.row)
while (i < j && ranges[i].start.column < startColumn && ranges[i].start.row == 0)
i++;

while (i < j && ranges[j].end.column > endColumn && ranges[j].end.row == range.end.row)
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);
Expand Down
27 changes: 27 additions & 0 deletions src/search_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,33 @@ module.exports = {
check(2, 2, 3, 4);
check(0, 2, 1, 4);
check(4, 2, 5, 4);
},

"test: find all matches in a range" : function() {
var session = new EditSession([
"",
" var myVar1 = 1; var myVar2 = 2; var myVar3 = 3;",
" var myVar4 = 4; var myVar5 = 5; var myVar6 = 6;"
]);

var search = new Search().set({
needle: "var",
backwards: true,
caseSensitive: true,
range: {start: {row: 1, column: 20}, end: {row: 2, column: 22}},
wholeWord: false,
regExp: false
});

var ranges = search.findAll(session);

assert.equal(ranges.length, 3);
assert.position(ranges[0].start, 1, 20);
assert.position(ranges[0].end, 1, 23);
assert.position(ranges[1].start, 1, 36);
assert.position(ranges[1].end, 1, 39);
assert.position(ranges[2].start, 2, 4);
assert.position(ranges[2].end, 2, 7);
}
};

Expand Down

0 comments on commit bd3573c

Please sign in to comment.