Element map = getDefaultRootElement(); Element elem = map.getElement(line); int startOffset = elem.getStartOffset(); getText(startOffset,endOffset-startOffset, s); } catch (BadLocationException ble) { ble.printStackTrace(); getLastTokenTypeOnLine(line-1);
if (offs==doc.getLength()) { return offs; Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offs); int end = root.getElement(line).getEndOffset() - 1; doc.getText(offs, end-offs, seg); if (doc.isIdentifierChar(languageIndex, ch)) { do { ch = seg.next(); } while (doc.isIdentifierChar(languageIndex, ch) && ch != CharacterIterator.DONE); ch = seg.next(); } while (ch!=Segment.DONE && !(doc.isIdentifierChar(languageIndex, ch) || Character.isWhitespace(ch)));
/** * Returns the token at the specified offset. * * @param doc The document. * @param offset The offset of the token. * @return The token, or <code>null</code> if the offset is not valid. * @see #getTokenAtOffset(RSyntaxTextArea, int) * @see #getTokenAtOffset(Token, int) */ public static Token getTokenAtOffset(RSyntaxDocument doc, int offset) { Element root = doc.getDefaultRootElement(); int lineIndex = root.getElementIndex(offset); Token t = doc.getTokenListForLine(lineIndex); return RSyntaxUtilities.getTokenAtOffset(t, offset); }
/** * Constructs a plain text document. A default root element is created, * and the tab size set to 5. * * @param tmf The <code>TokenMakerFactory</code> for this document. If * this is <code>null</code>, a default factory is used. * @param syntaxStyle The syntax highlighting scheme to use. */ public RSyntaxDocument(TokenMakerFactory tmf, String syntaxStyle) { putProperty(tabSizeAttribute, Integer.valueOf(5)); lastTokensOnLines = new DynamicIntArray(400); lastTokensOnLines.add(Token.NULL); // Initial (empty) line. s = new Segment(); setTokenMakerFactory(tmf); setSyntaxStyle(syntaxStyle); }
/** * Deserializes a document. * * @param in The stream to read from. * @throws ClassNotFoundException * @throws IOException */ private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException { in.defaultReadObject(); // Install default TokenMakerFactory. To support custom TokenMakers, // both JVM's should install default TokenMakerFactories that support // the language they want to use beforehand. setTokenMakerFactory(null); // Handle other transient stuff this.s = new Segment(); int lineCount = getDefaultRootElement().getElementCount(); lastTokensOnLines = new DynamicIntArray(lineCount); setSyntaxStyle(syntaxStyle); // Actually install (transient) TokenMaker }
bracket = doc.charAt(caretPosition); if (index==-1 && caretPosition<doc.getLength()-1) { bracket = doc.charAt(++caretPosition); Element map = doc.getDefaultRootElement(); int curLine = map.getElementIndex(caretPosition); Element line = map.getElement(curLine); int start = line.getStartOffset(); int end = line.getEndOffset(); Token token = doc.getTokenListForLine(curLine); token = RSyntaxUtilities.getTokenAtOffset(token, caretPosition); doc.getText(start,end-start, charSegment); int segOffset = charSegment.offset; token = doc.getTokenListForLine(curLine); haveTokenList = true; token = doc.getTokenListForLine(curLine); haveTokenList = true; doc.getText(start,end-start, charSegment); int segOffset = charSegment.offset; int iStart = segOffset + charSegment.count - 1; token = doc.getTokenListForLine(curLine); haveTokenList = true;
if (doc.getLanguageIsMarkup() && doc.getCompleteMarkupCloseTags() && !selection && rsta.getCloseMarkupTags() && dot>1) { char ch = doc.charAt(dot-2); if (ch=='<' || ch=='[') { Token t = doc.getTokenListForLine( rsta.getCaretLineNumber()); t = RSyntaxUtilities.getTokenAtOffset(t, dot-1);
@Test public void testSetTokenMakerFactory() { String syntaxStyle = SyntaxConstants.SYNTAX_STYLE_C; doc = new RSyntaxDocument(syntaxStyle); // By default, we do indeed get syntax highlighting for Java doc.setSyntaxStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); Assert.assertNotNull(doc.getLineCommentStartAndEnd(0)); // No mappings -> default to PlainTextTokenMaker doc.setTokenMakerFactory(new EmptyTokenMakerFactory()); doc.setSyntaxStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); // Ghetto test to show we are not picking up a JavaTokenMaker Assert.assertNull(doc.getLineCommentStartAndEnd(0)); // Verify restoring default instance doc.setTokenMakerFactory(null); doc.setSyntaxStyle(SyntaxConstants.SYNTAX_STYLE_JAVA); Assert.assertNotNull(doc.getLineCommentStartAndEnd(0)); }
/** * Makes our private <code>Segment s</code> point to the text in our * document referenced by the specified element. Note that * <code>line</code> MUST be a valid line number in the document. * * @param line The line number you want to get. */ private void setSharedSegment(int line) { Element map = getDefaultRootElement(); //int numLines = map.getElementCount(); Element element = map.getElement(line); if (element==null) { throw new InternalError("Invalid line number: " + line); } int startOffset = element.getStartOffset(); //int endOffset = (line==numLines-1 ? // element.getEndOffset()-1 : element.getEndOffset() - 1); int endOffset = element.getEndOffset()-1; // Why always "-1"? try { getText(startOffset, endOffset-startOffset, s); } catch (BadLocationException ble) { throw new InternalError("Text range not in document: " + startOffset + "-" + endOffset); } }
int p1 = Math.max(dot, mark); RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument(); Element map = doc.getDefaultRootElement(); String beforeText = getBeforeTextIndented(s); String afterText = getAfterTextIndented(s); doc.replace(p0,p1-p0, beforeText+afterText, null); textArea.setCaretPosition(p0+beforeText.length());
throw new BadLocationException("Invalid document position", p0); else if (p1>doc.getLength()) { throw new BadLocationException("Invalid document position", p1); Element map = doc.getDefaultRootElement(); int lineNum = map.getElementIndex(p0); Token t = doc.getTokenListForLine(lineNum);
RSyntaxDocument doc = new RSyntaxDocument( SyntaxConstants.SYNTAX_STYLE_JAVA); RSyntaxTextArea textArea = new RSyntaxTextArea(doc); Assert.assertEquals(7, action.getWordStart(textArea, 7)); doc.replace(0, doc.getLength(), "", null); Assert.assertEquals(0, action.getWordStart(textArea, 0));
RSyntaxDocument doc = (RSyntaxDocument) rsta.getDocument(); int line = rsta.getCaretLineNumber(); Token t = doc.getTokenListForLine(line); int type = doc.getLastTokenTypeOnLine(line); if (type == Token.NULL) type = doc.getClosestStandardTokenTypeForInternalType(type);
@Test public void testSetSyntaxStyle() { String syntaxStyle = SyntaxConstants.SYNTAX_STYLE_C; doc = new RSyntaxDocument(syntaxStyle); Assert.assertEquals(syntaxStyle, doc.getSyntaxStyle()); syntaxStyle = SyntaxConstants.SYNTAX_STYLE_XML; doc.setSyntaxStyle(syntaxStyle); Assert.assertEquals(syntaxStyle, doc.getSyntaxStyle()); syntaxStyle = "text/custom"; doc.setSyntaxStyle(syntaxStyle); Assert.assertEquals(syntaxStyle, doc.getSyntaxStyle()); }
@Test public void testFireDocumentEvent_InsertWithTwoNewLinesOneReplaced() throws Exception { String syntaxStyle = SyntaxConstants.SYNTAX_STYLE_C; doc = new RSyntaxDocument(syntaxStyle); insertHelloWorldC(doc); TestDocumentListener l = new TestDocumentListener(); doc.addDocumentListener(l); int oldLen = doc.getLength(); String text = "// Inserted line 1\nprintf(\"This is working\n\");"; doc.replace(oldLen - 4, 1, text, null); // Four events sent - One change/remove pair and one change/insert // pair. Remove is the one line replaced. Assert.assertEquals(4, l.events.size()); DocumentEvent e = l.events.get(0); // offset and length == start and end lines AFTER remove with new EOL // tokens. In this case a new line was "added" by this change. assertDocumentEvent(e, DocumentEvent.EventType.CHANGE, 6, 6); e = l.events.get(1); assertDocumentEvent(e, DocumentEvent.EventType.REMOVE, oldLen - 4, 1); e = l.events.get(2); // offset and length == start and end lines AFTER insert with new EOL // tokens. In this case a new line was "added" by this change. assertDocumentEvent(e, DocumentEvent.EventType.CHANGE, 8, 8); e = l.events.get(3); assertDocumentEvent(e, DocumentEvent.EventType.INSERT, oldLen - 4, text.length()); }
@Override protected int getWordEnd(RTextArea textArea, int offs) throws BadLocationException { RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument(); Element root = doc.getDefaultRootElement(); int line = root.getElementIndex(offs); Element elem = root.getElement(line); int end = elem.getEndOffset() - 1; int wordEnd = offs; while (wordEnd <= end) { if (!isIdentifierChar(doc.charAt(wordEnd))) { break; } wordEnd++; } return wordEnd; }
if (offs==doc.getLength()) { return offs; return end; doc.getText(offs, end-offs, seg); if (doc.isIdentifierChar(languageIndex, ch)) { do { ch = seg.next(); } while (doc.isIdentifierChar(languageIndex, ch) && ch != CharacterIterator.DONE);
@Test public void testGetLanguageIsMarkup() { String syntaxStyle = SyntaxConstants.SYNTAX_STYLE_C; doc = new RSyntaxDocument(syntaxStyle); Assert.assertFalse(doc.getLanguageIsMarkup()); // Language that does not use curly braces syntaxStyle = SyntaxConstants.SYNTAX_STYLE_XML; doc.setSyntaxStyle(syntaxStyle); Assert.assertTrue(doc.getLanguageIsMarkup()); }
/** * Tests empty documents, documents with lots of blank lines, etc. */ @Test public void testEmptyLines() throws Exception { RSyntaxDocument doc = new RSyntaxDocument( SyntaxConstants.SYNTAX_STYLE_JAVA); // An empty document. doc.remove(0, doc.getLength()); assertIteratorMatchesList(doc); // A document with nothing but empty lines. doc.insertString(0, "\n\n\n\n", null); assertIteratorMatchesList(doc); // A document with nothing lots of empty lines before text. doc.insertString(0, "\n\n\n\nfor if while\n\n\n\n", null); assertIteratorMatchesList(doc); }
/** * {@inheritDoc} */ @Override public ParseResult parse(RSyntaxDocument doc, String style) { result.clearNotices(); Element root = doc.getDefaultRootElement(); result.setParsedLines(0, root.getElementCount()-1); if (spf==null || doc.getLength()==0) { return result; } try { SAXParser sp = spf.newSAXParser(); Handler handler = new Handler(doc); DocumentReader r = new DocumentReader(doc); InputSource input = new InputSource(r); sp.parse(input, handler); r.close(); } catch (SAXParseException spe) { // A fatal parse error - ignore; a ParserNotice was already created. } catch (Exception e) { //e.printStackTrace(); // Will print if DTD specified and can't be found result.addNotice(new DefaultParserNotice(this, "Error parsing XML: " + e.getMessage(), 0, -1, -1)); } return result; }