Skip to content

Commit

Permalink
add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Aug 11, 2020
1 parent 1bea038 commit 846dde8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/vs/editor/common/model/textModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,18 @@ export class TextModel extends Disposable implements model.ITextModel {
searchRanges = [this.getFullModelRange()];
}

searchRanges = searchRanges.sort((d1, d2) => d1.startLineNumber - d2.startLineNumber || d1.startColumn - d2.startColumn);

const uniqueSearchRanges: Range[] = [];
uniqueSearchRanges.push(searchRanges.reduce((prev, curr) => {
if (Range.areIntersecting(prev, curr)) {
return prev.plusRange(curr);
}

uniqueSearchRanges.push(prev);
return curr;
}));

let matchMapper: (value: Range, index: number, array: Range[]) => model.FindMatch[];
if (!isRegex && searchString.indexOf('\n') < 0) {
// not regex, not multi line
Expand All @@ -1152,7 +1164,7 @@ export class TextModel extends Disposable implements model.ITextModel {
matchMapper = (searchRange: Range) => TextModelSearch.findMatches(this, new SearchParams(searchString, isRegex, matchCase, wordSeparators), searchRange, captureMatches, limitResultCount);
}

return searchRanges.map(matchMapper).reduce((arr, matches: model.FindMatch[]) => arr.concat(matches), []);
return uniqueSearchRanges.map(matchMapper).reduce((arr, matches: model.FindMatch[]) => arr.concat(matches), []);
}

public findNextMatch(searchString: string, rawSearchStart: IPosition, isRegex: boolean, matchCase: boolean, wordSeparators: string, captureMatches: boolean): model.FindMatch | null {
Expand Down
52 changes: 52 additions & 0 deletions src/vs/editor/contrib/find/test/findModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,58 @@ suite('FindModel', () => {
findState.dispose();
});

findTest('multi-selection find model next stays in scope (overlap)', (editor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello', wholeWord: true, searchScope: [new Range(7, 1, 8, 2), new Range(8, 1, 9, 1)] }, false);
let findModel = new FindModelBoundToEditorModel(editor, findState);

assertFindState(
editor,
[1, 1, 1, 1],
null,
[
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);

findModel.moveToNextMatch();
assertFindState(
editor,
[7, 14, 7, 19],
[7, 14, 7, 19],
[
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);

findModel.moveToNextMatch();
assertFindState(
editor,
[8, 14, 8, 19],
[8, 14, 8, 19],
[
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);

findModel.moveToNextMatch();
assertFindState(
editor,
[7, 14, 7, 19],
[7, 14, 7, 19],
[
[7, 14, 7, 19],
[8, 14, 8, 19]
]
);

findModel.dispose();
findState.dispose();
});

findTest('multi-selection find model next stays in scope', (editor) => {
let findState = new FindReplaceState();
findState.change({ searchString: 'hello', matchCase: true, wholeWord: false, searchScope: [new Range(6, 1, 7, 38), new Range(9, 3, 9, 38)] }, false);
Expand Down

0 comments on commit 846dde8

Please sign in to comment.