Java Code Examples for org.netbeans.editor.Utilities#getFirstNonWhiteBwd()

The following examples show how to use org.netbeans.editor.Utilities#getFirstNonWhiteBwd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FixNamespacesPerformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void removeUnusedNamespaces() {
    try {
        List<PositionRange> ranges = new ArrayList<>(importData.getItemsToRemove().size());
        for (Attribute attribute : importData.getItemsToRemove()) {
            int from = attribute.from();
            int to = attribute.to();
            //check if the line before the area is white
            int lineBeginning = Utilities.getRowStart(baseDocument, attribute.from());
            int firstNonWhite = Utilities.getFirstNonWhiteBwd(baseDocument, attribute.from());
            if (lineBeginning > firstNonWhite) {
                //delete the white content before the area inclusing the newline
                from = lineBeginning - 1; // (-1 => includes the line end)
            }
            ranges.add(new PositionRange(baseDocument, from, to));
        }
        for (PositionRange positionRange : ranges) {
            baseDocument.remove(positionRange.getFrom(), positionRange.getTo() - positionRange.getFrom());
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
}
 
Example 2
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected int getInitialIndentFromPreviousLine(final BaseDocument doc, final int line) throws BadLocationException {

        // get initial indent from the previous line
        int initialIndent = 0;

        if (line > 0) {
            int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
            int previousNonWhiteLineEnd = Utilities.getFirstNonWhiteBwd(doc, lineStart);

            if (previousNonWhiteLineEnd > 0) {
                initialIndent = Utilities.getRowIndent(doc, previousNonWhiteLineEnd);
            }
        }

        return initialIndent;
    }
 
Example 3
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected int getInitialIndentFromPreviousLine(final BaseDocument doc, final int line) throws BadLocationException {
    
    // get initial indent from the previous line
    int initialIndent = 0;
    
    if (line > 0){
        int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
        int previousNonWhiteLineEnd = Utilities.getFirstNonWhiteBwd(doc, lineStart);
        
        if (previousNonWhiteLineEnd > 0){
            initialIndent = Utilities.getRowIndent(doc, previousNonWhiteLineEnd);
        }
    }
    
    return initialIndent;
}
 
Example 4
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int findCommentEnd(BaseDocument doc, CommentHandler handler, int offsetFrom, int offsetTo) throws BadLocationException {
    int to = Utilities.getFirstNonWhiteBwd(doc, offsetTo, offsetFrom);
    if (to == -1) {
        return offsetTo;
    }
    String endDelim = handler.getCommentEndDelimiter();
    if (DocumentUtilities.getText(doc).subSequence(
            Math.max(offsetFrom, to - endDelim.length() + 1), to + 1).equals(endDelim)) {
        // after end of the delimiter
        return to + 1;
    }
    
    return offsetFrom;

}
 
Example 5
Source File: DTDFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Inserts new line at given position and indents the new line with
    * spaces.
    *
    * @param doc the document to work on
    * @param offset the offset of a character on the line
    * @return new offset to place cursor to
    */
    public int indentNewLine (Document doc, int offset) {
//        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\n+ XMLFormatter::indentNewLine: doc = " + doc); // NOI18N
//        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset = " + offset); // NOI18N

        if (doc instanceof BaseDocument) {
            BaseDocument bdoc = (BaseDocument)doc;

            bdoc.atomicLock();
            try {
                bdoc.insertString (offset, "\n", null); // NOI18N
                offset++;

                int fullLine = Utilities.getFirstNonWhiteBwd (bdoc, offset - 1);
                int indent = Utilities.getRowIndent (bdoc, fullLine);
                
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: fullLine = " + fullLine); // NOI18N
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: indent   = " + indent); // NOI18N
//
//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset   = " + offset); // NOI18N
//                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: sb       = '" + sb.toString() + "'"); // NOI18N

                String indentation = getIndentString(bdoc, indent);
                bdoc.insertString (offset, indentation, null);
                offset += indentation.length();

//                if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("+             ::indentNewLine: offset = " + offset); // NOI18N
            } catch (BadLocationException e) {
                if (Boolean.getBoolean ("netbeans.debug.exceptions")) { // NOI18N
                    e.printStackTrace();
                }
            } finally {
                bdoc.atomicUnlock();
            }

            return offset;
        }

        return super.indentNewLine (doc, offset);
    }
 
Example 6
Source File: LineBreakHook.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void insert(MutableContext context) throws BadLocationException {
    if (!(context.getDocument() instanceof BaseDocument)) {
        return;
    }
    BaseDocument doc = (BaseDocument)context.getDocument();

    int insertPos = context.getCaretOffset();
    int caretPos = context.getComponent().getCaretPosition();
    int lineStartPos = Utilities.getRowStart(doc, insertPos);

    TokenHierarchy h = TokenHierarchy.get(doc);
    TokenSequence seq = h.tokenSequence();
    // check the actual tokens
    seq.move(context.getCaretOffset());
    int openOffset = followsOpeningTag(seq);
    
    int nonWhiteBefore = Utilities.getFirstNonWhiteBwd(doc, insertPos, lineStartPos);

    int lineEndPos = Utilities.getRowEnd(doc, caretPos);
    int nonWhiteAfter = Utilities.getFirstNonWhiteFwd(doc, caretPos, lineEndPos);

    // there is a opening tag preceding on the line && something following the insertion point
    if (nonWhiteBefore != -1 && nonWhiteAfter != -1 && openOffset >= 0) {
        // check that the following token (after whitespace(s)) is a 
        // opening tag
        seq.move(nonWhiteAfter);
        // now we need to position the caret at the END of the line immediately 
        // preceding the closing tag. Assuming it's already indented
        if (precedesClosingTag(seq)) {
            int startClosingLine = Utilities.getRowStart(doc, nonWhiteAfter);
            int nextLineStart = Utilities.getRowStart(doc, insertPos, 1);
            if (nextLineStart >= startClosingLine - 1) {
                insertBlankBetweenTabs(context, openOffset);
            }
            return;
        }
    }
    // if the rest of the line is blank, we must insert newline + indent, so the cursor
    // appears at the correct place
    if (nonWhiteAfter != -1) {
        // will be handled by the formatter automatically
        return;
    }

    int desiredIndent;

    if (openOffset != Integer.MIN_VALUE) {
        desiredIndent = IndentUtils.lineIndent(doc, Utilities.getRowStart(doc, Math.abs(openOffset)));
        if (openOffset >= 0) {
            desiredIndent += IndentUtils.indentLevelSize(doc);
        }
    } else {
        // align with the current line
        desiredIndent = IndentUtils.lineIndent(doc, lineStartPos);
    }
    String blankLine = "\n" + 
        IndentUtils.createIndentString(doc, desiredIndent);
    context.setText(blankLine, -1, blankLine.length(), 1, blankLine.length());
}