int pos = SearchEngine.getNextMatchPos(searchText, rTextArea.getText(), forward, matchCase, wholeWord); if (pos != -1) { rTextArea.setCaretPosition(forward ? 0 : rTextArea.getDocument().getLength() - 1); search(direction); searchField.setBackground(COLOR_BG_WARN);
/** * Returns whether tokens of the specified type should have "mark * occurrences" enabled for the current programming language. * * @param type The token type. * @return Whether tokens of this type should have "mark occurrences" * enabled. */ boolean getMarkOccurrencesOfTokenType(int type) { RSyntaxDocument doc = (RSyntaxDocument)getDocument(); return doc.getMarkOccurrencesOfTokenType(type); }
public Fold(int type, RSyntaxTextArea textArea, int startOffs) throws BadLocationException { this.type = type; this.textArea = textArea; this.startOffs = textArea.getDocument().createPosition(startOffs); }
/** * Returns a list of tokens representing the given line. * * @param line The line number to get tokens for. * @return A linked list of tokens representing the line's text. */ public Token getTokenListForLine(int line) { return ((RSyntaxDocument)getDocument()).getTokenListForLine(line); }
/** * Returns the token at the specified offset. * * @param textArea The text area. * @param offset The offset of the token. * @return The token, or <code>null</code> if the offset is not valid. * @see #getTokenAtOffset(RSyntaxDocument, int) * @see #getTokenAtOffset(Token, int) */ public static Token getTokenAtOffset(RSyntaxTextArea textArea, int offset) { RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument(); return RSyntaxUtilities.getTokenAtOffset(doc, offset); }
/** * Sets the ending offset of this fold, such as the closing curly brace * of a code block in C or Java. {@link FoldParser} implementations should * call this on an existing <code>Fold</code> upon finding its end. If * this method isn't called, then this <code>Fold</code> is considered to * have no end, i.e., it will collapse everything to the end of the file. * * @param endOffs The end offset of this fold. * @throws BadLocationException If <code>endOffs</code> is not a valid * location in the text area. */ public void setEndOffset(int endOffs) throws BadLocationException { this.endOffs = textArea.getDocument().createPosition(endOffs); }
/** * Constructor. * * @param delay The delay between the last key press and when the document * is parsed. * @param textArea The text area whose document the parser will be * parsing. */ ParserManager(int delay, RSyntaxTextArea textArea) { this.textArea = textArea; textArea.getDocument().addDocumentListener(this); textArea.addPropertyChangeListener("document", this); parsers = new ArrayList<Parser>(1); // Usually small timer = new Timer(delay, this); timer.setRepeats(false); running = true; }
/** * Returns the template that should be inserted at the current caret * position, assuming the trigger character was pressed. * * @param textArea The text area that's getting text inserted into it. * @return A template that should be inserted, if appropriate, or * <code>null</code> if no template should be inserted. */ public synchronized CodeTemplate getTemplate(RSyntaxTextArea textArea) { int caretPos = textArea.getCaretPosition(); int charsToGet = Math.min(caretPos, maxTemplateIDLength); try { Document doc = textArea.getDocument(); doc.getText(caretPos-charsToGet, charsToGet, s); @SuppressWarnings("unchecked") int index = Collections.binarySearch(templates, s, comparator); return index>=0 ? (CodeTemplate)templates.get(index) : null; } catch (BadLocationException ble) { ble.printStackTrace(); throw new InternalError("Error in CodeTemplateManager"); } }
/** * Constructor. * * @param textArea The text area whose folds we are managing. */ public DefaultFoldManager(RSyntaxTextArea textArea) { this.textArea = textArea; support = new PropertyChangeSupport(this); l = new Listener(); textArea.getDocument().addDocumentListener(l); textArea.addPropertyChangeListener(RSyntaxTextArea.SYNTAX_STYLE_PROPERTY, l); textArea.addPropertyChangeListener("document", l); folds = new ArrayList<Fold>(); updateFoldParser(); }
/** * If auto-indent is enabled, this method returns whether a new line after * this one should be indented (based on the standard indentation rules for * the current programming language). For example, in Java, for a line * containing: * * <pre> * for (int i=0; i<10; i++) { * </pre> * * the following line should be indented. * * @param line The line to check. * @return Whether a line inserted after this one should be auto-indented. * If auto-indentation is disabled, this will always return * <code>false</code>. * @see #isAutoIndentEnabled() */ public boolean getShouldIndentNextLine(int line) { if (isAutoIndentEnabled()) { RSyntaxDocument doc = (RSyntaxDocument)getDocument(); return doc.getShouldIndentNextLine(line); } return false; }
/** * Sets what type of syntax highlighting this editor is doing. This method * fires a property change of type {@link #SYNTAX_STYLE_PROPERTY}. * * @param styleKey The syntax editing style to use, for example, * {@link SyntaxConstants#SYNTAX_STYLE_NONE} or * {@link SyntaxConstants#SYNTAX_STYLE_JAVA}. * @see #getSyntaxEditingStyle() * @see SyntaxConstants */ public void setSyntaxEditingStyle(String styleKey) { if (styleKey==null) { styleKey = SYNTAX_STYLE_NONE; } if (!styleKey.equals(syntaxStyleKey)) { String oldStyle = syntaxStyleKey; syntaxStyleKey = styleKey; ((RSyntaxDocument)getDocument()).setSyntaxStyle(styleKey); firePropertyChange(SYNTAX_STYLE_PROPERTY, oldStyle, styleKey); setActiveLineRange(-1, -1); } }
/** * Returns the end line of this fold. For example, in languages such as * C and Java, this might be the line containing the closing curly brace of * a code block.<p> * * The value returned by this method will automatically update as the * text area's contents are modified, to track the ending line of the * code block. * * @return The end line of this code block. * @see #getEndOffset() * @see #getStartLine() */ public int getEndLine() { int endOffs = getEndOffset(); if (lastEndOffs==endOffs) { return cachedEndLine; } lastEndOffs = endOffs; Element root = textArea.getDocument().getDefaultRootElement(); return cachedEndLine = root.getElementIndex(endOffs); }
/** * Returns the starting line of this fold region. This is the only line * in the fold region that is not hidden when a fold is collapsed.<p> * * The value returned by this method will automatically update as the * text area's contents are modified, to track the starting line of the * code block. * * @return The starting line of the code block. * @see #getEndLine() * @see #getStartOffset() */ public int getStartLine() { int startOffs = getStartOffset(); if (lastStartOffs==startOffs) { return cachedStartLine; } lastStartOffs = startOffs; Element root = textArea.getDocument().getDefaultRootElement(); return cachedStartLine = root.getElementIndex(startOffs); }
/** * Returns the last visible offset in this text area. This may not be the * length of the document if code folding is enabled. * * @return The last visible offset in this text area. */ public int getLastVisibleOffset() { if (isCodeFoldingEnabled()) { int lastVisibleLine = foldManager.getLastVisibleLine(); if (lastVisibleLine<getLineCount()-1) { // Not the last line try { return getLineEndOffset(lastVisibleLine) - 1; } catch (BadLocationException ble) { // Never happens ble.printStackTrace(); } } } return getDocument().getLength(); }
/** * Returns whether the specified offset is "inside" the fold. This method * returns <code>true</code> if the offset is greater than the fold start * offset, and no further than the last offset of the last folded line. * * @param offs The offset to check. * @return Whether the offset is "inside" the fold. * @see #containsLine(int) */ public boolean containsOffset(int offs) { boolean contained = false; if (offs>getStartOffset()) { // Use Elements to avoid BadLocationExceptions Element root = textArea.getDocument().getDefaultRootElement(); int line = root.getElementIndex(offs); contained = line<=getEndLine(); } return contained; }
@Test public void testGetPreviousWord_manyLinesInBetween() throws Exception { DumbCompleteWordAction action = new DumbCompleteWordAction(); RSyntaxDocument doc = new RSyntaxDocument( SyntaxConstants.SYNTAX_STYLE_JAVA); RSyntaxTextArea textArea = new RSyntaxTextArea(doc); textArea.setText("aaron arthur aardvark\nfoo bar\n// bad code\namazing\n a"); Assert.assertEquals(textArea.getDocument().getLength()-1, action.getPreviousWord(textArea, textArea.getDocument().getLength())); Assert.assertEquals("aaron arthur aardvark\nfoo bar\n// bad code\n".length(), action.getPreviousWord(textArea, textArea.getDocument().getLength()-2)); Assert.assertEquals("aaron arthur ".length(), action.getPreviousWord(textArea, 22)); Assert.assertEquals("aaron ".length(), action.getPreviousWord(textArea, 8)); }
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea sta = (RSyntaxTextArea)textArea; boolean noSelection= sta.getSelectionStart()==sta.getSelectionEnd(); // First, see if this language wants to handle inserting newlines // itself. boolean handled = false; if (noSelection) { RSyntaxDocument doc = (RSyntaxDocument)sta.getDocument(); handled = doc.insertBreakSpecialHandling(e); } // If not... if (!handled) { handleInsertBreak(sta, noSelection); } }
private void possiblyCloseCurlyBrace(RSyntaxTextArea textArea, String leadingWS) { RSyntaxDocument doc = (RSyntaxDocument)textArea.getDocument();
@Override public void actionPerformedImpl(ActionEvent e, RTextArea textArea) { if (!textArea.isEditable() || !textArea.isEnabled()) { UIManager.getLookAndFeel().provideErrorFeedback(textArea); return; } RSyntaxTextArea rsta = (RSyntaxTextArea)getTextComponent(e); RSyntaxDocument doc = (RSyntaxDocument)rsta.getDocument(); int line = textArea.getCaretLineNumber(); int type = doc.getLastTokenTypeOnLine(line); if (type<0) { type = doc.getClosestStandardTokenTypeForInternalType(type); } // Only in MLC's should we try this if (type==Token.COMMENT_DOCUMENTATION || type==Token.COMMENT_MULTILINE) { insertBreakInMLC(e, rsta, line); } else { handleInsertBreak(rsta, true); } }
/** * Returns whether a parser notice contains the specified offset. * * @param notice The notice. * @param offs The offset. * @return Whether the notice contains the offset. */ private boolean noticeContainsPosition(ParserNotice notice, int offs){ if (notice.getKnowsOffsetAndLength()) { return notice.containsPosition(offs); } Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); int line = notice.getLine(); if (line<0) { // Defensive against possible bad user-defined notices. return false; } Element elem = root.getElement(line); return elem != null && offs>=elem.getStartOffset() && offs<elem.getEndOffset(); }