diff options
author | Carsten Reckord | 2017-12-08 18:01:45 +0000 |
---|---|---|
committer | Carsten Reckord | 2018-01-30 12:56:22 +0000 |
commit | 6a35da449d6559dcb0988c0ba842177eef7e27f0 (patch) | |
tree | 312ab38aeea5486c22cc9d5e9369e928c503e5be /org.eclipse.ui.genericeditor | |
parent | c8a75c5a719cbf05bc1384dbff713955b756d665 (diff) | |
download | eclipse.platform.text-6a35da449d6559dcb0988c0ba842177eef7e27f0.tar.gz eclipse.platform.text-6a35da449d6559dcb0988c0ba842177eef7e27f0.tar.xz eclipse.platform.text-6a35da449d6559dcb0988c0ba842177eef7e27f0.zip |
528344 - [Generic Editor] Default highlighter patterns are brokenI20180130-2000
Use a single (positive) pattern definition for valid words to match
both the current word and occurrences within the text
Change-Id: I6d05484b9991a543c2979005deae4533cb8c2735
Signed-off-by: Carsten Reckord <reckord@yatta.de>
Diffstat (limited to 'org.eclipse.ui.genericeditor')
-rw-r--r-- | org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultWordHighlightStrategy.java | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultWordHighlightStrategy.java b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultWordHighlightStrategy.java index 44673c8d00b..4f4ce28b03b 100644 --- a/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultWordHighlightStrategy.java +++ b/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/DefaultWordHighlightStrategy.java @@ -57,8 +57,9 @@ public class DefaultWordHighlightStrategy implements IReconcilingStrategy, IReco private ISourceViewer sourceViewer; private IDocument document; - private static final String WORD_REGEXP = "[a-zA-Z]+"; //$NON-NLS-1$ - private static final Pattern WORD_PATTERN = Pattern.compile(WORD_REGEXP); + private static final String WORD_REGEXP = "\\w+"; //$NON-NLS-1$ + private static final Pattern WORD_PATTERN = Pattern.compile(WORD_REGEXP, Pattern.UNICODE_CHARACTER_CLASS); + private static final Pattern CURRENT_WORD_START_PATTERN = Pattern.compile(WORD_REGEXP + "$", Pattern.UNICODE_CHARACTER_CLASS); //$NON-NLS-1$ private Annotation[] fOccurrenceAnnotations = null; @@ -71,13 +72,11 @@ public class DefaultWordHighlightStrategy implements IReconcilingStrategy, IReco String text = document.get(); offset = ((ITextViewerExtension5)sourceViewer).widgetOffset2ModelOffset(offset); - int wordStartOffset = findStartingOffset(text, offset); - int wordEndOffset = findEndingOffset(text, offset); - if(wordEndOffset <= wordStartOffset || wordEndOffset == -1 || wordStartOffset == -1) { + String word = findCurrentWord(text, offset); + if(word == null) { removeOccurrenceAnnotations(); return; } - String word = text.substring(wordStartOffset, wordEndOffset); Matcher m = WORD_PATTERN.matcher(text); Map<Annotation, Position> annotationMap = new HashMap<>(); @@ -109,23 +108,25 @@ public class DefaultWordHighlightStrategy implements IReconcilingStrategy, IReco } } - private static int findStartingOffset(String text, int offset) { - final Pattern NON_ALPHANUMERIC_LAST_PATTERN = Pattern.compile("[^\\w](?!.*[^\\w])"); //$NON-NLS-1$ + private static String findCurrentWord(String text, int offset) { + String wordStart = null; + String wordEnd = null; + String substring = text.substring(0, offset); - Matcher m = NON_ALPHANUMERIC_LAST_PATTERN.matcher(substring); - if(m.find()) { - return m.end(); + Matcher m = CURRENT_WORD_START_PATTERN.matcher(substring); + if (m.find()) { + wordStart=m.group(); } - return -1; - } - - private static int findEndingOffset(String text, int offset) { - String substring = text.substring(offset); - String[] split = substring.split("[^a-zA-Z0-9]+");//$NON-NLS-1$ - if(split.length == 0) { - return -1; + substring = text.substring(offset); + m = WORD_PATTERN.matcher(substring); + if (m.lookingAt()) { + wordEnd = m.group(); } - return offset + split[0].length(); + if (wordStart != null && wordEnd != null) + return wordStart + wordEnd; + if (wordStart != null) + return wordStart; + return wordEnd; } public void install(ITextViewer viewer) { |