diff --git a/RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarker.java b/RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarker.java index ac5375f89..42634e70f 100755 --- a/RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarker.java +++ b/RSyntaxTextArea/src/main/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarker.java @@ -39,9 +39,13 @@ public Token getTokenToMark(RSyntaxTextArea textArea) { RSyntaxUtilities.isNonWordChar(t)) { // Try to the "left" of the caret. dot--; + t = null; // Clear in case it's a non-word char try { if (dot>=textArea.getLineStartOffset(line)) { t = RSyntaxUtilities.getTokenAtOffset(tokenList, dot); + if (t != null && RSyntaxUtilities.isNonWordChar(t)) { + t = null; + } } } catch (BadLocationException ble) { ble.printStackTrace(); // Never happens diff --git a/RSyntaxTextArea/src/test/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarkerTest.java b/RSyntaxTextArea/src/test/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarkerTest.java index be9f49bab..11fec1403 100644 --- a/RSyntaxTextArea/src/test/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarkerTest.java +++ b/RSyntaxTextArea/src/test/java/org/fife/ui/rsyntaxtextarea/DefaultOccurrenceMarkerTest.java @@ -45,6 +45,42 @@ void testGetTokenToMark_endOfWord() { } + @Test + void testGetTokenToMark_none_atEndOfLineAfterNonWordChar() { + + String origContent = "foo\n."; + RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_JAVA, origContent); + textArea.setCaretPosition(origContent.indexOf('.') + 1); // End of document + + DefaultOccurrenceMarker marker = new DefaultOccurrenceMarker(); + Assertions.assertNull(marker.getTokenToMark(textArea)); + } + + + @Test + void testGetTokenToMark_none_betweenTwoNonWordChars() { + + String origContent = ".."; + RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_JAVA, origContent); + textArea.setCaretPosition(1); // Between the two periods + + DefaultOccurrenceMarker marker = new DefaultOccurrenceMarker(); + Assertions.assertNull(marker.getTokenToMark(textArea)); + } + + + @Test + void testGetTokenToMark_none_atStartOfLinePrecedingNonWordChar() { + + String origContent = "foo\n."; + RSyntaxTextArea textArea = createTextArea(SyntaxConstants.SYNTAX_STYLE_JAVA, origContent); + textArea.setCaretPosition(origContent.indexOf('.')); // Start of line before the period + + DefaultOccurrenceMarker marker = new DefaultOccurrenceMarker(); + Assertions.assertNull(marker.getTokenToMark(textArea)); + } + + @Test void testIsValidType_validType() {