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;
}
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;
}
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;
}
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();
}
}
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);
}
}
Aggregations