Skip to content

Commit

Permalink
Bug 531785 - Auto infix search in Open Resource - fix search results …
Browse files Browse the repository at this point in the history
…highlighting

Amendments were needed for the newly presented features (and only for them).
Covering this with new tests, while from them only the following have
already been failing BEFORE this fix:

* testSubstringMatch_withAutoInfix
* testDisableAutoInfixMatching

Signed-off-by: Petr Bodnar <p.bodnar@centrum.cz>
  • Loading branch information
pbodnar committed Jul 2, 2022
1 parent 5c87f05 commit 14b25b8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -726,18 +726,23 @@ private StyledString styleResourceExtensionMatch(IResource resource, String matc
}

private List<Position> getMatchPositions(String string, String matching) {
final String originalMatching = matching;
List<Position> positions = new ArrayList<>();
if (matching.isEmpty() || string.isEmpty()) {
return positions;
}

if (matching.startsWith("\\>")) { //$NON-NLS-1$
matching = matching.substring(2);
}

char lastChar = matching.charAt(matching.length() - 1);
if (lastChar == ' ') {
matching = matching.substring(0, matching.length() - 1);
} else if (lastChar == '<') {
matching = matching.substring(0, matching.length() - 2);
}

String originalTrimmedMatching = matching;
if (matching.indexOf('?') == -1 && matching.indexOf('*') == -1) {
matching = String.join("*", matching.split("(?=[A-Z0-9])")) + "*"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
} else {
Expand Down Expand Up @@ -776,9 +781,15 @@ private List<Position> getMatchPositions(String string, String matching) {
usedRegions++;
}
} while (restart && currentIndex < string.length());
if (usedRegions != regions.length && string.toLowerCase().startsWith(originalMatching.toLowerCase())) {
positions = new ArrayList<>();
positions.add(new Position(0, originalMatching.length()));
if (usedRegions != regions.length) {
// We should get here only when camelCase nor wildcard search is done
int matchingIndex = autoInfixSearch
? string.toLowerCase().indexOf(originalTrimmedMatching.toLowerCase())
: (string.toLowerCase().startsWith(originalTrimmedMatching.toLowerCase()) ? 0 : -1);
if (matchingIndex > -1) {
positions = new ArrayList<>();
positions.add(new Position(matchingIndex, originalTrimmedMatching.length()));
}
}
return positions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ public void testSubstringMatch() throws Exception {
compareStyleRanges(withDigits, getStyleRanges("t3s", "t3st.txt"), "t3st.txt", "");
}

/**
* Tests that the highlighting matches basic substrings when infix search is
* automatically performed.
*/
@Test
public void testSubstringMatch_withAutoInfix() throws Exception {
Position[] atEnd = { new Position(2, 6) };
compareStyleRanges(atEnd, getStyleRanges("st.txt", "test.txt"), "test.txt", "");

Position[] atEndDifferentCase = { new Position(2, 6) };
compareStyleRanges(atEndDifferentCase, getStyleRanges("ST.txt", "test.txt"), "test.txt", "");
}

/**
* Tests that the highlighting matches CamelCase searches
*
Expand All @@ -102,6 +115,17 @@ public void testCamelCaseMatch() throws Exception {

Position[] skippingDigit = { new Position(0, 2), new Position(5, 1) };
compareStyleRanges(skippingDigit, getStyleRanges("ThT", "This3Test.txt"), "This3Test.txt", "");

}

/**
* Tests that the highlighting matches CamelCase searches when infix search is
* automatically performed.
*/
@Test
public void testCamelCaseMatch_withAutoInfix() throws Exception {
Position[] atEnd = { new Position(4, 1), new Position(6, 1) };
compareStyleRanges(atEnd, getStyleRanges("IT", "ThisIsTest.txt"), "ThisIsTest.txt", "");
}

/**
Expand All @@ -124,6 +148,16 @@ public void testPatternMatch() throws Exception {
compareStyleRanges(withDigits, getStyleRanges("t?s3*x3t", "tes3t.tx3t"), "tes3t.tx3t", "");
}

/**
* Tests that the highlighting matches searches using '*' and '?' when infix
* search is automatically performed.
*/
@Test
public void testPatternMatch_withAutoInfix() throws Exception {
Position[] atEnd = { new Position(1, 1), new Position(3, 2) };
compareStyleRanges(atEnd, getStyleRanges("t?st", "atest.txt"), "atest.txt", "");
}

/**
* Tests that regex symbols do not break search
*
Expand Down Expand Up @@ -155,6 +189,21 @@ public void testDisableAutoPrefixMatching() throws Exception {
compareStyleRanges(both, getStyleRanges("CreS<", "CreateStuff.java"), "CreateStuff.java", "");
}

/**
* Tests that the highlighting matches searches using '&lt;'.
*/
@Test
public void testDisableAutoInfixMatching() throws Exception {
Position[] substring = { new Position(0, 4) };
compareStyleRanges(substring, getStyleRanges(">make", "Makefile"), "Makefile", "");

Position[] pattern = { new Position(0, 1), new Position(4, 4) };
compareStyleRanges(pattern, getStyleRanges(">M*file", "Makefile"), "Makefile", "");

Position[] camelCase = { new Position(0, 3), new Position(6, 1) };
compareStyleRanges(camelCase, getStyleRanges(">CreS", "CreateStuff.java"), "CreateStuff.java", "");
}

/**
* Tests that the highlighting matches extension searches
*
Expand Down

0 comments on commit 14b25b8

Please sign in to comment.