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

The following examples show how to use org.netbeans.editor.Utilities#getRowIndent() . 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: 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 2
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected int getIndentForTagParameter(BaseDocument doc, TokenItem tag) throws BadLocationException{
    int tagStartLine = Utilities.getLineOffset(doc, tag.getOffset());
    TokenItem currentToken = tag.getNext();
    
    /*
     * Find the offset of the first attribute if it is specified on the same line as the opening of the tag
     * e.g. <tag   |attr=
     * 
     */
    while (currentToken != null && isWSTag(currentToken) && tagStartLine == Utilities.getLineOffset(doc, currentToken.getOffset())){
        currentToken = currentToken.getNext();
    }
    
    if (tag != null && !isWSTag(currentToken) && tagStartLine == Utilities.getLineOffset(doc, currentToken.getOffset())){
        return currentToken.getOffset() - Utilities.getRowIndent(doc, currentToken.getOffset()) - Utilities.getRowStart(doc, currentToken.getOffset());
    }
    
    return getShiftWidth(); // default;
}
 
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: IndentationCounter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Indentation countUnderReadLock(int caretOffset) {
    int newIndent = 0;
    try {
        final TokenSequence<JadeTokenId> ts = TokenHierarchy.get(doc).tokenSequence(JadeTokenId.jadeLanguage());
        final int caretLineStart = LineDocumentUtils.getLineStart(doc, LineDocumentUtils.getLineStart(doc, caretOffset) - 1);
        if (ts != null) {
            ts.move(caretOffset);
            ts.moveNext();
            if (ts.token() == null) {
                return Indentation.NONE;
            }
            while (ts.token().id() == JadeTokenId.EOL || ts.token().id() == JadeTokenId.WHITESPACE) {
                // find the last token on the current line before EOL or WS
                if (!ts.movePrevious()) {
                    break;
                }
            }
            if (ts.token().id() == JadeTokenId.COMMENT || ts.token().id() == JadeTokenId.UNBUFFERED_COMMENT) {
                final int firstNonWsIndex = LineDocumentUtils.getLineFirstNonWhitespace(doc, LineDocumentUtils.getLineStart(doc, caretOffset) - 1);
                ts.move(firstNonWsIndex);
                ts.moveNext();
                if (ts.token() != null
                        && (ts.token().id() == JadeTokenId.COMMENT_DELIMITER
                        || ts.token().id() == JadeTokenId.UNBUFFERED_COMMENT_DELIMITER)) {
                    return new IndentationImpl(Utilities.getRowIndent(doc, caretLineStart) + 1);
                }
            }
            newIndent = Utilities.getRowIndent(doc, caretLineStart);
        }
    } catch (BadLocationException ex) {
        Exceptions.printStackTrace(ex);
    }
    if (newIndent < 0) {
        newIndent = 0;
    }
    return new IndentationImpl(newIndent);
}
 
Example 5
Source File: TagBasedLexerFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected int getIndentForTagParameter(BaseDocument doc, JoinedTokenSequence tokenSequence, int tagOffset) throws BadLocationException {
    int originalOffset = tokenSequence.offset();
    int tagStartLine = Utilities.getLineOffset(doc, tagOffset);
    tokenSequence.move(tagOffset);
    Token<?> token;
    int tokenOffset;
    boolean thereWasWS = false;
    int shift = doc.getShiftWidth(); // default;

    /*
     * Find the offset of the first attribute if it is specified on the same line as the opening of the tag
     * e.g. <tag   |attr=
     *
     */
    while (tokenSequence.moveNext()) {
        token = tokenSequence.token();
        tokenOffset = tokenSequence.offset();
        boolean isWSToken = isWSToken(token);
        
        if (thereWasWS && (!isWSToken || tagStartLine != Utilities.getLineOffset(doc, tokenOffset))) {
            if (!isWSToken && tagStartLine == Utilities.getLineOffset(doc, tokenOffset)) {
                
                shift = tokenOffset - Utilities.getRowIndent(doc, tokenOffset)
                        - Utilities.getRowStart(doc, tokenOffset);
            }
            break;
        } else if (isWSToken){
            thereWasWS = true;
        }
    }

    tokenSequence.move(originalOffset);
    tokenSequence.moveNext();

    return shift;
}
 
Example 6
Source File: TagBasedFormatter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getInitialIndentFromNextLine(final BaseDocument doc, final int line) throws BadLocationException {
    
    // get initial indent from the next line
    int initialIndent = 0;
    
    int lineStart = Utilities.getRowStartFromLineOffset(doc, line);
    int lineEnd = Utilities.getRowEnd(doc, lineStart);
    int nextNonWhiteLineStart = Utilities.getFirstNonWhiteFwd(doc, lineEnd);
    
    if (nextNonWhiteLineStart > 0){
        initialIndent = Utilities.getRowIndent(doc, nextNonWhiteLineStart, true);
    }
    
    return initialIndent;
}
 
Example 7
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 8
Source File: PhpTypedTextInterceptor.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void reindent(BaseDocument doc, int offset, TokenId id, Caret caret) throws BadLocationException {
    TokenSequence<? extends PHPTokenId> ts = LexUtilities.getPHPTokenSequence(doc, offset);
    if (ts != null) {
        ts.move(offset);
        if (!ts.moveNext() && !ts.movePrevious()) {
            return;
        }
        Token<? extends PHPTokenId> token = ts.token();
        if ((token.id() == id)) {
            final int rowFirstNonWhite = Utilities.getRowFirstNonWhite(doc, offset);
            if (id == PHPTokenId.PHP_CURLY_OPEN && ts.offset() == rowFirstNonWhite
                    && ts.movePrevious()) {
                // The curly is at the first nonwhite char at the line.
                // Do we need to indent the { according previous line?
                int previousExprestion = LexUtilities.findStartTokenOfExpression(ts);
                int previousIndent = Utilities.getRowIndent(doc, previousExprestion);
                int currentIndent = Utilities.getRowIndent(doc, offset);
                int newIndent = IndentUtils.countIndent(doc, offset, previousIndent);
                if (newIndent != currentIndent) {
                    GsfUtilities.setLineIndentation(doc, offset, Math.max(newIndent, 0));
                }
            } else if (id == PHPTokenId.WHITESPACE || (id == PHPTokenId.PHP_TOKEN && token.text().charAt(0) == ':')) { // ":" handles "default:"
                if (id == PHPTokenId.WHITESPACE) {
                    LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_CASE));
                } else {
                    LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_DEFAULT));
                }
                if (ts.offset() >= rowFirstNonWhite) { //previous "case" or "default" on one line with typed char
                    LexUtilities.findPreviousToken(ts, Arrays.asList(PHPTokenId.PHP_SWITCH));
                    Token<? extends PHPTokenId> firstCaseInSwitch = LexUtilities.findNextToken(ts, Arrays.asList(PHPTokenId.PHP_CASE));
                    if (firstCaseInSwitch != null && firstCaseInSwitch.id() == PHPTokenId.PHP_CASE) {
                        int indentOfFirstCase = GsfUtilities.getLineIndent(doc, ts.offset());
                        GsfUtilities.setLineIndentation(doc, offset, indentOfFirstCase);
                    }
                }
            } else if (id == PHPTokenId.PHP_CURLY_CLOSE) {
                OffsetRange begin = LexUtilities.findBwd(doc, ts, PHPTokenId.PHP_CURLY_OPEN, '{', PHPTokenId.PHP_CURLY_CLOSE, '}');
                if (begin != OffsetRange.NONE) {
                    int beginOffset = begin.getStart();
                    int indent = GsfUtilities.getLineIndent(doc, beginOffset);
                    previousAdjustmentIndent = GsfUtilities.getLineIndent(doc, offset);
                    GsfUtilities.setLineIndentation(doc, offset, indent);
                    previousAdjustmentOffset = caret.getDot();
                }
            }
        }
    }
}