Search in sources :

Example 86 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class CommentsIndentationCheck method findPreviousStatement.

/**
     * Finds a previous statement of the comment.
     * Uses root token of the line while searching.
     * @param comment comment.
     * @param root root token of the line.
     * @return previous statement of the comment or null if previous statement was not found.
     */
private DetailAST findPreviousStatement(DetailAST comment, DetailAST root) {
    DetailAST previousStatement = null;
    if (root.getLineNo() >= comment.getLineNo()) {
        // ATTENTION: parent of the comment is below the comment in case block
        // See https://github.com/checkstyle/checkstyle/issues/851
        previousStatement = getPrevStatementFromSwitchBlock(comment);
    }
    final DetailAST tokenWhichBeginsTheLine;
    if (root.getType() == TokenTypes.EXPR && root.getFirstChild().getFirstChild() != null) {
        if (root.getFirstChild().getType() == TokenTypes.LITERAL_NEW) {
            tokenWhichBeginsTheLine = root.getFirstChild();
        } else {
            tokenWhichBeginsTheLine = findTokenWhichBeginsTheLine(root);
        }
    } else if (root.getType() == TokenTypes.PLUS) {
        tokenWhichBeginsTheLine = root.getFirstChild();
    } else {
        tokenWhichBeginsTheLine = root;
    }
    if (tokenWhichBeginsTheLine != null && !isComment(tokenWhichBeginsTheLine) && isOnPreviousLineIgnoringComments(comment, tokenWhichBeginsTheLine)) {
        previousStatement = tokenWhichBeginsTheLine;
    }
    return previousStatement;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 87 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class CommentsIndentationCheck method getPrevCaseToken.

/**
     * Gets previous case-token for comment.
     * @param parentStatement comment's parent statement.
     * @return previous case-token or null if previous case-token is absent.
     */
private static DetailAST getPrevCaseToken(DetailAST parentStatement) {
    final DetailAST prevCaseToken;
    final DetailAST parentBlock = parentStatement.getParent();
    if (parentBlock.getParent() != null && parentBlock.getParent().getPreviousSibling() != null && parentBlock.getParent().getPreviousSibling().getType() == TokenTypes.LITERAL_CASE) {
        prevCaseToken = parentBlock.getParent().getPreviousSibling();
    } else {
        prevCaseToken = null;
    }
    return prevCaseToken;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 88 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class CommentsIndentationCheck method isCommentForMultiblock.

/**
     * Whether the comment might have been used for the next block in a multi-block structure.
     * @param endBlockStmt the end of the current block.
     * @return true, if the comment might have been used for the next
     *     block in a multi-block structure.
     */
private static boolean isCommentForMultiblock(DetailAST endBlockStmt) {
    final DetailAST nextBlock = endBlockStmt.getParent().getNextSibling();
    final int endBlockLineNo = endBlockStmt.getLineNo();
    final DetailAST catchAst = endBlockStmt.getParent().getParent();
    final DetailAST finallyAst = catchAst.getNextSibling();
    return nextBlock != null && nextBlock.getLineNo() == endBlockLineNo || finallyAst != null && catchAst.getType() == TokenTypes.LITERAL_CATCH && finallyAst.getLineNo() == endBlockLineNo;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 89 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class LineWrappingHandler method checkAnnotationIndentation.

/**
     * Checks line wrapping into annotations.
     *
     * @param atNode at-clause node.
     * @param firstNodesOnLines map which contains
     *     first nodes as values and line numbers as keys.
     * @param indentLevel line wrapping indentation.
     */
private void checkAnnotationIndentation(DetailAST atNode, NavigableMap<Integer, DetailAST> firstNodesOnLines, int indentLevel) {
    final int firstNodeIndent = getLineStart(atNode);
    final int currentIndent = firstNodeIndent + indentLevel;
    final Collection<DetailAST> values = firstNodesOnLines.values();
    final DetailAST lastAnnotationNode = atNode.getParent().getLastChild();
    final int lastAnnotationLine = lastAnnotationNode.getLineNo();
    final Iterator<DetailAST> itr = values.iterator();
    while (firstNodesOnLines.size() > 1) {
        final DetailAST node = itr.next();
        final DetailAST parentNode = node.getParent();
        final boolean isCurrentNodeCloseAnnotationAloneInLine = node.getLineNo() == lastAnnotationLine && isEndOfScope(lastAnnotationNode, node);
        if (isCurrentNodeCloseAnnotationAloneInLine || node.getType() == TokenTypes.AT && (parentNode.getParent().getType() == TokenTypes.MODIFIERS || parentNode.getParent().getType() == TokenTypes.ANNOTATIONS)) {
            logWarningMessage(node, firstNodeIndent);
        } else {
            logWarningMessage(node, currentIndent);
        }
        itr.remove();
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 90 with DetailAST

use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.

the class MemberDefHandler method checkType.

/**
     * Check the indentation of the method type.
     */
private void checkType() {
    final DetailAST type = getMainAst().findFirstToken(TokenTypes.TYPE);
    final DetailAST ident = AbstractExpressionHandler.getFirstToken(type);
    final int columnNo = expandedTabsColumnNo(ident);
    if (isOnStartOfLine(ident) && !getIndent().isAcceptable(columnNo)) {
        logError(ident, "type", columnNo);
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Aggregations

DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)397 Test (org.junit.Test)74 CommonHiddenStreamToken (antlr.CommonHiddenStreamToken)14 FullIdent (com.puppycrawl.tools.checkstyle.api.FullIdent)14 ArrayList (java.util.ArrayList)13 AST (antlr.collections.AST)8 Method (java.lang.reflect.Method)7 LinkedList (java.util.LinkedList)7 Scope (com.puppycrawl.tools.checkstyle.api.Scope)6 HashSet (java.util.HashSet)6 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)5 ArrayDeque (java.util.ArrayDeque)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 AbstractCheck (com.puppycrawl.tools.checkstyle.api.AbstractCheck)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)3 SimpleEntry (java.util.AbstractMap.SimpleEntry)3 HashMap (java.util.HashMap)3