/** * Returns a list of "mark all" highlights in the text area. If there are * no such highlights, this will be an empty list. * * @return The list of "mark all" highlight ranges. */ public List<DocumentRange> getMarkAllHighlightRanges() { List<DocumentRange> list = new ArrayList<DocumentRange>( markAllHighlights.size()); for (HighlightInfo info : markAllHighlights) { int start = info.getStartOffset(); int end = info.getEndOffset() + 1; // HACK DocumentRange range = new DocumentRange(start, end); list.add(range); } return list; }
/** * Returns a list of "marked occurrences" in the text area. If there are * no marked occurrences, this will be an empty list. * * @return The list of marked occurrences, or an empty list if none. The * contents of this list will be of type {@link DocumentRange}. */ public List<DocumentRange> getMarkedOccurrences() { List<DocumentRange> list = new ArrayList<DocumentRange>(markedOccurrences.size()); for (HighlightInfo info : markedOccurrences) { int start = info.getStartOffset(); int end = info.getEndOffset() + 1; // HACK if (start <= end) { // Occasionally a Marked Occurrence can have a lost end offset // but not start offset (replacing entire text content with // new content, and a marked occurrence is on the last token // in the document). DocumentRange range = new DocumentRange(start, end); list.add(range); } } return list; }
@Test(expected = IllegalArgumentException.class) public void testConstructor_NegativeStart() throws Exception { new DocumentRange(-1, 8); }
@Test(expected = IllegalArgumentException.class) public void testConstructor_NegativeEnd() throws Exception { new DocumentRange(5, -2); }
@Test(expected = IllegalArgumentException.class) public void testConstructor_EndLessThanStart() throws Exception { new DocumentRange(9, 8); }
@Test(expected = IllegalArgumentException.class) public void testSet_NegativeStart() throws Exception { new DocumentRange(1, 1).set(-1, 8); }
@Test(expected = IllegalArgumentException.class) public void testSet_NegativeEnd() throws Exception { new DocumentRange(1, 1).set(5, -2); }
@Test(expected = IllegalArgumentException.class) public void testSet_EndLessThanStart() throws Exception { new DocumentRange(1, 1).set(9, 8); }
@Test public void testIsZeroLength() { Assert.assertFalse(new DocumentRange(1, 3).isZeroLength()); Assert.assertFalse(new DocumentRange(2, 3).isZeroLength()); Assert.assertTrue(new DocumentRange(3, 3).isZeroLength()); }
protected void mouseClicked(MouseEvent e) { ParserNotice pn = notices.get(0); int offs = pn.getOffset(); int len = pn.getLength(); if (offs>-1 && len>-1) { // These values are optional DocumentRange range = new DocumentRange(offs, offs + len); RSyntaxUtilities.selectAndPossiblyCenter(textArea, range, true); } else { int line = pn.getLine(); try { offs = textArea.getLineStartOffset(line); textArea.getFoldManager().ensureOffsetNotInClosedFold(offs); textArea.setCaretPosition(offs); } catch (BadLocationException ble) { // Never happens UIManager.getLookAndFeel().provideErrorFeedback(textArea); } } }
@Test public void testHashCode() { // NOTE: Not a good test, assumes formula of hash code! DocumentRange range = new DocumentRange(5, 8); Assert.assertEquals(5+8, range.hashCode()); }
@Test public void testCompareTo() { DocumentRange range = new DocumentRange(5, 8); Assert.assertTrue(range.compareTo(null) == 1); DocumentRange range2 = new DocumentRange(4, 8); Assert.assertTrue(range.compareTo(range2) > 0); range2 = new DocumentRange(5, 8); Assert.assertTrue(range.compareTo(range2) == 0); Assert.assertTrue(range.compareTo(range) == 0); range2 = new DocumentRange(6, 8); Assert.assertTrue(range.compareTo(range2) < 0); range2 = new DocumentRange(5, 7); Assert.assertTrue(range.compareTo(range2) > 0); range2 = new DocumentRange(5, 9); Assert.assertTrue(range.compareTo(range2) < 0); }
@Test public void testToString() { DocumentRange range = new DocumentRange(5, 8); Assert.assertEquals("[DocumentRange: 5-8]", range.toString()); }
/** * https://github.com/bobbylight/RSyntaxTextArea/issues/38 */ @Test public void testSearchEngineRegexFindEmptyString() throws Exception { textArea.setText("how the who for what is it howhow"); String searchFor = "[how]{3}|"; SearchContext context = new SearchContext(searchFor); context.setRegularExpression(true); assertTrue(findImpl(context)); assertResult(new SearchResult(new DocumentRange(0, 3), 1, 4)); assertTrue(findImpl(context)); assertResult(new SearchResult(new DocumentRange(8, 11), 1, 4)); assertTrue(findImpl(context)); assertResult(new SearchResult(new DocumentRange(27, 30), 1, 4)); assertTrue(findImpl(context)); assertResult(new SearchResult(new DocumentRange(30, 33), 1, 4)); assertFalse(findImpl(context)); }
@Test public void testEquals() { DocumentRange range = new DocumentRange(5, 8); Assert.assertFalse(range.equals(null)); Assert.assertTrue(range.equals(range)); Assert.assertTrue(range.equals(new DocumentRange(5, 8))); Assert.assertFalse(range.equals(new DocumentRange(4, 8))); Assert.assertFalse(range.equals("Hello world")); }
@Test public void testConstructor_HappyPath() { DocumentRange range = new DocumentRange(5, 8); Assert.assertEquals(5, range.getStartOffset()); Assert.assertEquals(8, range.getEndOffset()); }
@Test public void testGetEndOffset() { DocumentRange range = new DocumentRange(5, 8); Assert.assertEquals(8, range.getEndOffset()); range.set(1, 2); Assert.assertEquals(2, range.getEndOffset()); }
@Test public void testGetStartOffset() { DocumentRange range = new DocumentRange(5, 8); Assert.assertEquals(5, range.getStartOffset()); range.set(9, 12); Assert.assertEquals(9, range.getStartOffset()); }
@Test public void testSet_HappyPath() { DocumentRange range = new DocumentRange(1, 1); range.set(5, 8); Assert.assertEquals(5, range.getStartOffset()); Assert.assertEquals(8, range.getEndOffset()); }
@Test public void testTranslate() { DocumentRange range = new DocumentRange(5, 8); range.translate(6); Assert.assertEquals(11, range.getStartOffset()); Assert.assertEquals(14, range.getEndOffset()); }