Search in sources :

Example 91 with DetailAST

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

the class MethodCallHandler method checkIndentation.

@Override
public void checkIndentation() {
    final DetailAST exprNode = getMainAst().getParent();
    if (exprNode.getParent().getType() == TokenTypes.SLIST) {
        final DetailAST methodName = getMainAst().getFirstChild();
        checkExpressionSubtree(methodName, getIndent(), false, false);
        final DetailAST lparen = getMainAst();
        final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN);
        checkLeftParen(lparen);
        if (rparen.getLineNo() != lparen.getLineNo()) {
            checkExpressionSubtree(getMainAst().findFirstToken(TokenTypes.ELIST), new IndentLevel(getIndent(), getBasicOffset()), false, true);
            checkRightParen(lparen, rparen);
            checkWrappingIndentation(getMainAst(), getMethodCallLastNode(getMainAst()));
        }
    }
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 92 with DetailAST

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

the class MethodCallHandler method getSuggestedChildIndent.

@Override
public IndentLevel getSuggestedChildIndent(AbstractExpressionHandler child) {
    // for whatever reason a method that crosses lines, like asList
    // here:
    //            System.out.println("methods are: " + Arrays.asList(
    //                new String[] {"method"}).toString());
    // will not have the right line num, so just get the child name
    final DetailAST first = getMainAst().getFirstChild();
    IndentLevel suggestedLevel = new IndentLevel(getLineStart(first));
    if (!areOnSameLine(child.getMainAst().getFirstChild(), getMainAst().getFirstChild())) {
        suggestedLevel = new IndentLevel(suggestedLevel, getBasicOffset(), getIndentCheck().getLineWrappingIndentation());
    }
    // If the right parenthesis is at the start of a line;
    // include line wrapping in suggested indent level.
    final DetailAST rparen = getMainAst().findFirstToken(TokenTypes.RPAREN);
    if (getLineStart(rparen) == rparen.getColumnNo()) {
        suggestedLevel.addAcceptedIndent(new IndentLevel(getParent().getSuggestedChildIndent(this), getIndentCheck().getLineWrappingIndentation()));
    }
    return suggestedLevel;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 93 with DetailAST

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

the class MethodCallHandler method isChainedMethodCallWrapped.

/**
     * If this is the first chained method call which was moved to the next line.
     * @return true if chained class are wrapped
     */
private boolean isChainedMethodCallWrapped() {
    boolean result = false;
    final DetailAST main = getMainAst();
    final DetailAST dot = main.getFirstChild();
    final DetailAST target = dot.getFirstChild();
    final DetailAST dot1 = target.getFirstChild();
    final DetailAST target1 = dot1.getFirstChild();
    if (dot1.getType() == TokenTypes.DOT && target1.getType() == TokenTypes.METHOD_CALL) {
        result = true;
    }
    return result;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 94 with DetailAST

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

the class MethodDefHandler method getMethodDefLineStart.

/**
     * Gets the start line of the method, excluding any annotations. This is required because the
     * current {@link TokenTypes#METHOD_DEF} may not always be the start as seen in
     * https://github.com/checkstyle/checkstyle/issues/3145.
     *
     * @param mainAst
     *            The method definition ast.
     * @return The start column position of the method.
     */
private int getMethodDefLineStart(DetailAST mainAst) {
    // get first type position
    int lineStart = mainAst.findFirstToken(TokenTypes.IDENT).getLineNo();
    // check if there is a type before the indent
    final DetailAST typeNode = mainAst.findFirstToken(TokenTypes.TYPE);
    if (typeNode != null) {
        lineStart = getFirstLine(lineStart, typeNode);
    }
    // check if there is a modifier before the type
    for (DetailAST node = mainAst.findFirstToken(TokenTypes.MODIFIERS).getFirstChild(); node != null; node = node.getNextSibling()) {
        // skip annotations as we check them else where as outside the method
        if (node.getType() == TokenTypes.ANNOTATION) {
            continue;
        }
        if (node.getLineNo() < lineStart) {
            lineStart = node.getLineNo();
        }
    }
    return lineStart;
}
Also used : DetailAST(com.puppycrawl.tools.checkstyle.api.DetailAST)

Example 95 with DetailAST

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

the class NewHandler method checkIndentation.

@Override
public void checkIndentation() {
    final DetailAST type = getMainAst().getFirstChild();
    if (type != null) {
        checkExpressionSubtree(type, getIndent(), false, false);
    }
    final DetailAST lparen = getMainAst().findFirstToken(TokenTypes.LPAREN);
    checkLeftParen(lparen);
}
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