public void removeTrailingWhitespace() { int end = textArea.getDocument().getLength(); // remove trailing empty lines int realEnd = end; if (eolAt(end - 1)) { while (eolAt(end - 2) || getText(end - 2, end - 1).equals("\r")) end--; if (end < realEnd) textArea.replaceRange("", end - 1, realEnd - 1); } // remove trailing white space from each line for (int i = textArea.getLineCount() - 1; i >= 0; i--) try { int start = textArea.getLineStartOffset(i); if (eolAt(end - 1)) end--; if (start == end) continue; String line = getText(start, end); realEnd = end; while (start < end && Character.isWhitespace(line.charAt(end - start - 1))) end--; if (end < realEnd) textArea.replaceRange("", end, realEnd); end = start; } catch (BadLocationException e) { /* cannot happen */ } }
int skipToEOL(TokenIterator iter, Token current) { int end = textArea.getDocument().getLength(); for (; ; ) { if (current.type == current.NULL || !iter.hasNext()) return end; end = current.offset + current.textCount; current = iter.next(); } }
/** * 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 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 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; }
private void search(int direction) { String searchText = searchField.getText(); if (searchText == null || searchText.length() == 0 || rTextArea.getText() == null) { return; } boolean forward = direction >= 0; boolean matchCase = matchCaseCB.isSelected(); boolean regex = regexCB.isSelected(); boolean wholeWord = wholeWordCB.isSelected(); if (markAllCB.isSelected()) { rTextArea.markAll(searchText, matchCase, wholeWord, regex); } else { rTextArea.clearMarkAllHighlights(); } SearchContext context = new SearchContext(); context.setSearchFor(searchText); context.setMatchCase(matchCase); context.setRegularExpression(regex); context.setSearchForward(forward); context.setWholeWord(wholeWord); // TODO hack: move cursor before previous search for not jump to next occurrence if (direction == 0 && !COLOR_BG_ERROR.equals(searchField.getBackground())) { try { int caretPos = rTextArea.getCaretPosition(); int lineNum = rTextArea.getLineOfOffset(caretPos) - 1; if (lineNum > 1) { rTextArea.setCaretPosition(rTextArea.getLineStartOffset(lineNum)); } } catch (BadLocationException e) { LOG.error("Caret move error", e); } } boolean found = SearchEngine.find(rTextArea, context); if (!found) { 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); return; } searchField.setBackground(COLOR_BG_ERROR); } else { searchField.setBackground(COLOR_BG_NORMAL); } }
private void init(final DesignTimeContext designTimeContext) { if (designTimeContext == null) { throw new NullPointerException(); } this.designTimeContext = designTimeContext; setTitle(Messages.getString("ScriptableDataSourceEditor.Title")); setModal(true); previewAction = new PreviewAction(); queryNameTextField = new JTextField(null, 0); queryNameTextField.setColumns(35); queryNameTextField.getDocument().addDocumentListener(new QueryNameTextFieldDocumentListener()); queryTextArea = new RSyntaxTextArea(); queryTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); queryTextArea.getDocument().addDocumentListener(new QueryDocumentListener()); initScriptTextArea = new RSyntaxTextArea(); initScriptTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); shutdownScriptTextArea = new RSyntaxTextArea(); shutdownScriptTextArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE); languageField = new JList(new DefaultComboBoxModel(InternalBSFManager.getRegisteredLanguages())); languageField.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); languageField.getSelectionModel().addListSelectionListener(new UpdateLanguageHandler()); queryNameList = new JList(); queryNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); queryNameList.setVisibleRowCount(5); queryNameList.addListSelectionListener(new QueryNameListSelectionListener()); queryNameList.setCellRenderer(new QueryNameListCellRenderer()); final QueryRemoveAction removeQueryAction = new QueryRemoveAction(); queryNameList.addListSelectionListener(removeQueryAction); super.init(); }
/** * Sets whether this <code>Fold</code> is collapsed. Calling this method will update both the text * area and all <code>Gutter</code> components. * * @param collapsed Whether this fold should be collapsed. * @see #isCollapsed() * @see #toggleCollapsedState() */ public void setCollapsed(boolean collapsed) { if (collapsed != this.collapsed) { // Change our fold state and cached info about folded line count. int lineCount = getLineCount(); int linesToCollapse = lineCount - childCollapsedLineCount; if (!collapsed) { // If we're expanding linesToCollapse = -linesToCollapse; } // System.out.println("Hiding lines: " + linesToCollapse + // " (" + lineCount + ", " + linesToCollapse + ")"); this.collapsed = collapsed; if (parent != null) { parent.updateChildCollapsedLineCount(linesToCollapse); } // If an end point of the selection is being hidden, move the caret // "out" of the fold. if (collapsed) { int dot = textArea.getSelectionStart(); // Forgive variable name Element root = textArea.getDocument().getDefaultRootElement(); int dotLine = root.getElementIndex(dot); boolean updateCaret = containsLine(dotLine); if (!updateCaret) { int mark = textArea.getSelectionEnd(); if (mark != dot) { int markLine = root.getElementIndex(mark); updateCaret = containsLine(markLine); } } if (updateCaret) { dot = root.getElement(getStartLine()).getEndOffset() - 1; textArea.setCaretPosition(dot); } } textArea.foldToggled(this); } }
public Fold(int type, RSyntaxTextArea textArea, int startOffs) throws BadLocationException { this.type = type; this.textArea = textArea; this.startOffs = textArea.getDocument().createPosition(startOffs); }
/** * 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); }