diff options
| author | Lucas Bullen | 2017-12-08 17:50:00 +0000 |
|---|---|---|
| committer | Lucas Bullen | 2018-01-11 20:28:08 +0000 |
| commit | b105a174363cffdd389ee1ef930f8b28720d2bd0 (patch) | |
| tree | 06944798a09c8726910b760ed39f2bbfa0e9ed9c | |
| parent | d41ac5516d706126806c7df4e86c94700cc3c8ed (diff) | |
| download | eclipse.platform.ui-b105a174363cffdd389ee1ef930f8b28720d2bd0.tar.gz eclipse.platform.ui-b105a174363cffdd389ee1ef930f8b28720d2bd0.tar.xz eclipse.platform.ui-b105a174363cffdd389ee1ef930f8b28720d2bd0.zip | |
Bug 528301 - Open resource dialog Camel case matches incorrectlyI20180113-1500I20180112-2000
- Fixed Bug
- Restructured for reading clarity
- Proper parsing for extensions
- Digits can be used in camel case
Change-Id: I601cc0d5995e72a8856bea6f93953e4c11bb1cb1
Signed-off-by: Lucas Bullen <lbullen@redhat.com>
2 files changed, 102 insertions, 36 deletions
diff --git a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/FilteredResourcesSelectionDialog.java b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/FilteredResourcesSelectionDialog.java index c8a1732dbf4..1e8912d9393 100644 --- a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/FilteredResourcesSelectionDialog.java +++ b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/FilteredResourcesSelectionDialog.java @@ -44,6 +44,7 @@ import org.eclipse.jface.action.Separator; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.text.ITextSelection; +import org.eclipse.jface.text.Position; import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider; import org.eclipse.jface.viewers.ILabelProviderListener; import org.eclipse.jface.viewers.ISelection; @@ -635,67 +636,98 @@ public class FilteredResourcesSelectionDialog extends return new StyledString(super.getText(element)); } - IResource res = (IResource) element; - StyledString str = new StyledString(res.getName().trim()); + IResource resource = (IResource) element; String searchFieldString = ((Text) getPatternControl()).getText(); + String resourceName = resource.getName(); Styler boldStyler = new Styler() { @Override public void applyStyles(TextStyle textStyle) { textStyle.font = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT); } }; - - int potentialIndex = str.getString().toLowerCase().indexOf(searchFieldString.toLowerCase()); - final String wildcard = "*"; //$NON-NLS-1$ - if (potentialIndex != -1) { - str.setStyle(potentialIndex, searchFieldString.length(), boldStyler); - } else if (searchFieldString.indexOf('?') != -1 || searchFieldString.indexOf('*') != -1) { - str = markRegions(str, String.join(wildcard, searchFieldString.split("(?=[\\.])")), boldStyler); //$NON-NLS-1$ - } else { - String matchingString = String.join(wildcard, searchFieldString.split("(?=[A-Z\\.])")) + wildcard; //$NON-NLS-1$ - str = markRegions(str, matchingString, boldStyler); + StyledString str = styleResourceExtensionMatch(resource, searchFieldString, boldStyler); + if (str.getStyleRanges().length != 0 && searchFieldString.lastIndexOf('.') != -1) { + resourceName = resourceName.substring(0, resourceName.lastIndexOf('.' + resource.getFileExtension())); + searchFieldString = searchFieldString.substring(0, searchFieldString.lastIndexOf('.')); } + getMatchPositions(resourceName, searchFieldString).stream() + .forEach(position -> str.setStyle(position.offset, position.length, boldStyler)); + return str; + } - // extra info for duplicates - if (isDuplicateElement(element)) { - str.append(" - ", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$ - str.append(res.getParent().getFullPath().makeRelative().toString(), StyledString.QUALIFIER_STYLER); + private StyledString styleResourceExtensionMatch(IResource resource, String matchingString, Styler styler) { + StyledString str = new StyledString(resource.getName().trim()); + String resourceExtension = resource.getFileExtension(); + int lastDotIndex = matchingString.lastIndexOf('.'); + if (lastDotIndex == -1 || resourceExtension == null) { + return str; + } + resourceExtension = '.' + resourceExtension; + int resourceExtensionIndex = resource.getName().lastIndexOf(resourceExtension); + String matchingExtension = matchingString.substring(lastDotIndex, matchingString.length()); + for (Position markPosition : getMatchPositions(resourceExtension, matchingExtension)) { + str.setStyle(resourceExtensionIndex + markPosition.offset, markPosition.length, styler); } - return str; } - private StyledString markRegions(StyledString styledString, String matchingString, Styler styler) { - String text = styledString.getString().toLowerCase(); - matchingString = matchingString.toLowerCase(); - StyledString updatedText = styledString; + private List<Position> getMatchPositions(String string, String matching) { + final String originalMatching = matching; + List<Position> positions = new ArrayList<>(); + 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 { + matching = matching.toLowerCase(); + string = string.toLowerCase(); + } + int startingIndex = 0; int currentIndex = 0; - String[] regions = matchingString.replaceAll("\\.", "\\\\.").split("(\\?)|\\*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + String[] regions = matching.replaceAll("\\.", "\\\\.").split("(\\?)|\\*"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + int usedRegions = 0; boolean restart = false; do { for (String region : regions) { if (region == null || region.isEmpty()) { + usedRegions++; continue; - } else if (region.equals("?")) { //$NON-NLS-1$ + } + if (region.equals("?")) { //$NON-NLS-1$ currentIndex++; - } else { - int startlocation = indexOf(Pattern.compile(region), text.substring(currentIndex)); - int length = region.replace("\\", "").length(); //$NON-NLS-1$ //$NON-NLS-2$ - if (startlocation == -1) { - currentIndex = ++startingIndex; - updatedText = styledString; - restart = true; - break; + usedRegions++; + continue; + } + int startlocation = indexOf(Pattern.compile(region), string.substring(currentIndex)); + if (startlocation == -1) { + currentIndex = ++startingIndex; + positions = new ArrayList<>(); + restart = true; + break; + } + int regionIndex = 0; + currentIndex += startlocation; + for (char regionChar : region.toCharArray()) { + if (regionChar == '\\') { + continue; + } + regionIndex++; + if (regionChar == '.' && regionIndex != 1) { + positions.add(new Position(currentIndex, regionIndex - 1)); + currentIndex += regionIndex; + regionIndex = 0; } - updatedText.setStyle(startlocation + currentIndex, length, styler); - currentIndex += startlocation + length; } + positions.add(new Position(currentIndex, regionIndex)); + currentIndex += regionIndex; + usedRegions++; } - } while (restart && currentIndex < text.length()); - - return updatedText; + } while (restart && currentIndex < string.length()); + if (usedRegions != regions.length && string.toLowerCase().startsWith(originalMatching.toLowerCase())) { + positions = new ArrayList<>(); + positions.add(new Position(0, originalMatching.length())); + } + return positions; } private int indexOf(Pattern pattern, String s) { diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/ResourceItemLabelTest.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/ResourceItemLabelTest.java index d6df2438f5e..db04a142060 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/ResourceItemLabelTest.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/ResourceItemLabelTest.java @@ -70,6 +70,9 @@ public class ResourceItemLabelTest extends UITestCase { Position[] full = { new Position(0, 8) }; compareStyleRanges(full, getStyleRanges("test.txt", "test.txt")); + + Position[] withDigits = { new Position(0, 3) }; + compareStyleRanges(withDigits, getStyleRanges("t3s", "t3st.txt")); } /** @@ -86,6 +89,12 @@ public class ResourceItemLabelTest extends UITestCase { Position[] withSubstrings = { new Position(0, 2), new Position(4, 2) }; compareStyleRanges(withSubstrings, getStyleRanges("ThTe", "ThisTest.txt")); + + Position[] withDigits = { new Position(0, 2), new Position(4, 2) }; + compareStyleRanges(withDigits, getStyleRanges("Th3T", "This3Test.txt")); + + Position[] skippingDigit = { new Position(0, 2), new Position(5, 1) }; + compareStyleRanges(skippingDigit, getStyleRanges("ThT", "This3Test.txt")); } /** @@ -102,6 +111,9 @@ public class ResourceItemLabelTest extends UITestCase { Position[] both = { new Position(0, 1), new Position(2, 1), new Position(6, 2) }; compareStyleRanges(both, getStyleRanges("t?s*xt", "test.txt")); + + Position[] withDigits = { new Position(0, 1), new Position(2, 2), new Position(7, 3) }; + compareStyleRanges(withDigits, getStyleRanges("t?s3*x3t", "tes3t.tx3t")); } /** @@ -115,6 +127,28 @@ public class ResourceItemLabelTest extends UITestCase { Position[] withSubstring = { new Position(0, 1), new Position(8, 3) }; compareStyleRanges(withSubstring, getStyleRanges("M.MF", "MANIFEST.MF")); + + Position[] withCamelCase = { new Position(4, 3), new Position(8, 1) }; + compareStyleRanges(withCamelCase, getStyleRanges(".TxT", "test.TxxT")); + + Position[] withPattern = { new Position(4, 2), new Position(8, 1) }; + compareStyleRanges(withPattern, getStyleRanges(".t*t", "test.txxt")); + + Position[] withDigits = { new Position(4, 2), new Position(8, 1) }; + compareStyleRanges(withDigits, getStyleRanges(".3*3", "test.3xx3")); + } + + /** + * Tests for Bug 528301: Camel Case match with precursing letter matches + * + * @throws Exception + */ + public void testBug528301() throws Exception { + Position[] withSameLettersBeforeCamel = { new Position(0, 1), new Position(3, 1), new Position(5, 1) }; + compareStyleRanges(withSameLettersBeforeCamel, getStyleRanges("ABC", "AbcBzCz.txt")); + + Position[] withDigits = { new Position(0, 1), new Position(4, 1), new Position(6, 1), new Position(8, 1) }; + compareStyleRanges(withDigits, getStyleRanges("AB5C", "Ab5cBz5zCz.txt")); } private void compareStyleRanges(Position[] expected, StyleRange[] actual) { |
