public void testRemoveRedundantTagsFromDoc() throws BadLocationException { m_doc.insertString(0, "Foobar", SimpleAttributeSet.EMPTY); SimpleAttributeSet actualAttr = new SimpleAttributeSet(); actualAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE); m_doc.setCharacterAttributes(1, 2, actualAttr, true); actualAttr = new SimpleAttributeSet(); actualAttr.addAttribute(StyleConstants.Bold, Boolean.TRUE); m_doc.setCharacterAttributes(3, 2, actualAttr, true); assertEquals("F<b>ooba</b>r", FormattedText.formatted(m_doc).getFormatted()); }
/** * Searches for a quote token. * * @param content the content to search * @param startOffset the start of the search * @param endOffset the end of the search * @return the new position */ protected int getQuoteToken(String content, int startOffset, int endOffset) { String quoteDelimiter = content.substring(startOffset, startOffset + 1); String escapeString = escapeQuote(quoteDelimiter); int index; int endOfQuote = startOffset; // skip over the escape quotes in this quote index = content.indexOf(escapeString, endOfQuote + 1); while ((index > -1) && (index < endOffset)) { endOfQuote = index + 1; index = content.indexOf(escapeString, endOfQuote); } // now find the matching delimiter index = content.indexOf(quoteDelimiter, endOfQuote + 1); if ((index < 0) || (index > endOffset)) endOfQuote = endOffset; else endOfQuote = index; m_Self.setCharacterAttributes(startOffset, endOfQuote - startOffset + 1, DEFAULT_STRING, false); return endOfQuote + 1; }
/** * Parse the line to determine the appropriate highlighting. * * @param content the content to parse * @param line the line number * @throws BadLocationException if offsets are invalid */ protected void applyHighlighting(String content, int line) throws BadLocationException { int startOffset = m_RootElement.getElement(line).getStartOffset(); int endOffset = m_RootElement.getElement(line).getEndOffset() - 1; int lineLength = endOffset - startOffset; int contentLength = content.length(); if (endOffset >= contentLength) endOffset = contentLength - 1; // check for multi line comments // (always set the comment attribute for the entire line) if (getMultiLineComment()) { if (endingMultiLineComment(content, startOffset, endOffset) || isMultiLineComment() || startingMultiLineComment(content, startOffset, endOffset)) { m_Self.setCharacterAttributes( startOffset, endOffset - startOffset + 1, DEFAULT_COMMENT, false); return; } } // set normal attributes for the line m_Self.setCharacterAttributes(startOffset, lineLength, DEFAULT_NORMAL, true); // check for single line comment int index = content.indexOf(getSingleLineCommentStart(), startOffset); if ((index > -1) && (index < endOffset)) { m_Self.setCharacterAttributes(index, endOffset - index + 1, DEFAULT_COMMENT, false); endOffset = index - 1; } // check for tokens checkForTokens(content, startOffset, endOffset); }
/** * Searches for a keyword token. * * @param content the content to search in * @param startOffset the position to start the search fromm * @param endOffset the position to end the search * @return the new position */ protected int getOtherToken(String content, int startOffset, int endOffset) { int endOfToken = startOffset + 1; while (endOfToken <= endOffset) { if (isDelimiter(content.substring(endOfToken, endOfToken + 1))) break; endOfToken++; } String token = content.substring(startOffset, endOfToken); // see if this token has a highlighting format associated with it MutableAttributeSet attr = getKeywordFormatting(token); if (attr != null) m_Self.setCharacterAttributes(startOffset, endOfToken - startOffset, attr, false); return endOfToken + 1; }
/** * Asserts that the style is still set correctly on a styled document after encoding the document * into a string representation and decoding it back into a styled document again. */ public void assertStyleRoundtrip(Object style) throws BadLocationException { m_doc.insertString(0, "Foobar Test", SimpleAttributeSet.EMPTY); SimpleAttributeSet actualAttr = new SimpleAttributeSet(); actualAttr.addAttribute(style, Boolean.TRUE); m_doc.setCharacterAttributes(2, 2, actualAttr, true); StyledDocument doc = roundtrip(m_doc); AttributeSet exceptedAttr = doc.getCharacterElement(2).getAttributes(); Boolean hasStyle = (Boolean) exceptedAttr.getAttribute(style); if (hasStyle != null) { assertTrue(hasStyle.booleanValue()); } else { fail("Style not set"); } }
/** * Highlight comment lines to matching end delimiter. * * @param content the content to parse * @param line the line number */ protected void commentLinesAfter(String content, int line) { int offset = m_RootElement.getElement(line).getEndOffset(); // End of comment not found, nothing to do int endDelimiter = -1; if (getMultiLineComment()) endDelimiter = indexOf(content, getMultiLineCommentEnd(), offset); if (endDelimiter < 0) return; // Matching start/end of comment found, comment the lines int startDelimiter = lastIndexOf(content, getMultiLineCommentStart(), endDelimiter); if (startDelimiter < 0 || startDelimiter <= offset) { m_Self.setCharacterAttributes(offset, endDelimiter - offset + 1, DEFAULT_COMMENT, false); } }
/** * Highlight lines when a multi line comment is still 'open' (ie. matching end delimiter has not * yet been encountered). * * @param content the content to check * @param line the line number * @return true if there are comment lines before */ protected boolean commentLinesBefore(String content, int line) { int offset = m_RootElement.getElement(line).getStartOffset(); // Start of comment not found, nothing to do int startDelimiter = -1; if (getMultiLineComment()) startDelimiter = lastIndexOf(content, getMultiLineCommentStart(), offset - 2); if (startDelimiter < 0) return false; // Matching start/end of comment found, nothing to do int endDelimiter = indexOf(content, getMultiLineCommentEnd(), startDelimiter); if (endDelimiter < offset & endDelimiter != -1) return false; // End of comment not found, highlight the lines m_Self.setCharacterAttributes( startDelimiter, offset - startDelimiter + 1, DEFAULT_COMMENT, false); return true; }
private void highlightArea(int pos, int len, String col) { SimpleAttributeSet attrs = new SimpleAttributeSet(); StyleConstants.setForeground(attrs, getColour(col)); document.setCharacterAttributes(pos, len, attrs, true); }