提交 4d85f2ed 编写于 作者: A Alex Dima

Fixes #6053: handle reaching end of line while searching better

上级 ed9e56b2
......@@ -999,6 +999,10 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
break;
}
bestResult = result;
if (m.index + m[0].length === text.length) {
// Reached the end of the line
break;
}
}
return bestResult;
}
......@@ -1011,8 +1015,8 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
m = searchRegex.exec(text);
if (m) {
var range = new Range(lineNumber, m.index + 1 + deltaOffset, lineNumber, m.index + 1 + m[0].length + deltaOffset);
// Exit early if the regex matches the same range
if (range.equalsRange(result[result.length - 1])) {
// Exit early if the regex matches the same range
return counter;
}
result.push(range);
......@@ -1020,6 +1024,10 @@ export class TextModel extends OrderGuaranteeEventEmitter implements editorCommo
if (counter >= limitResultCount) {
return counter;
}
if (m.index + m[0].length === text.length) {
// Reached the end of the line
return counter;
}
}
} while(m);
return counter;
......
......@@ -963,6 +963,35 @@ suite('FindModel', () => {
findState.dispose();
});
findTest('find .*', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: '.*', isRegex: true }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);
assertFindState(
editor,
[1, 1, 1, 1],
null,
[
[ 1, 1, 1, 18],
[ 2, 1, 2, 18],
[ 3, 1, 3, 20],
[ 4, 1, 4, 1],
[ 5, 1, 5, 13],
[ 6, 1, 6, 43],
[ 7, 1, 7, 41],
[ 8, 1, 8, 41],
[ 9, 1, 9, 40],
[10, 1, 10, 2],
[11, 1, 11, 17],
[12, 1, 12, 1],
]
);
findModel.dispose();
findState.dispose();
});
findTest('find next ^.*$', (editor, cursor) => {
let findState = new FindReplaceState();
findState.change({ searchString: '^.*$', isRegex: true }, false);
......
......@@ -528,22 +528,26 @@ suite('Editor Model - Find', () => {
let actualRanges = model.findMatches(searchString, false, isRegex, matchCase, wholeWord);
let actual = actualRanges.map(toArrRange);
assert.deepEqual(actual, expected);
assert.deepEqual(actual, expected, 'findMatches OK');
// test `findNextMatch`
let match = model.findNextMatch(searchString, new Position(1, 1), isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[0]);
let startPos = new Position(1, 1);
let match = model.findNextMatch(searchString, startPos, isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[0], `findNextMatch ${startPos}`);
for (let i = 0; i < expected.length; i++) {
match = model.findNextMatch(searchString, new Position(expected[i][0], expected[i][1]), isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[i]);
startPos = new Position(expected[i][0], expected[i][1]);
match = model.findNextMatch(searchString, startPos, isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[i], `findNextMatch ${startPos}`);
}
// test `findPrevMatch`
match = model.findPreviousMatch(searchString, new Position(model.getLineCount(), model.getLineMaxColumn(model.getLineCount())), isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[expected.length-1]);
startPos = new Position(model.getLineCount(), model.getLineMaxColumn(model.getLineCount()));
match = model.findPreviousMatch(searchString, startPos, isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[expected.length-1], `findPrevMatch ${startPos}`);
for (let i = 0; i < expected.length; i++) {
match = model.findPreviousMatch(searchString, new Position(expected[i][2], expected[i][3]), isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[i]);
startPos = new Position(expected[i][2], expected[i][3]);
match = model.findPreviousMatch(searchString, startPos, isRegex, matchCase, wholeWord);
assert.deepEqual(toArrRange(match), expected[i], `findPrevMatch ${startPos}`);
}
model.dispose();
......@@ -623,6 +627,20 @@ suite('Editor Model - Find', () => {
);
});
test('/.*/ find', () => {
assertFindMatches(
regularText.join('\n'),
'.*', true, false, false,
[
[1, 1, 1, 74],
[2, 1, 2, 69],
[3, 1, 3, 54],
[4, 1, 4, 65],
[5, 1, 5, 31]
]
);
});
test('/^$/ find', () => {
assertFindMatches(
[
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册