Search in sources :

Example 36 with DetailNode

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

the class JavadocUtil method getNextSibling.

/**
 * Gets next sibling of specified node.
 *
 * @param node DetailNode
 * @return next sibling.
 */
public static DetailNode getNextSibling(DetailNode node) {
    DetailNode nextSibling = null;
    final DetailNode parent = node.getParent();
    if (parent != null) {
        final int nextSiblingIndex = node.getIndex() + 1;
        final DetailNode[] children = parent.getChildren();
        if (nextSiblingIndex <= children.length - 1) {
            nextSibling = children[nextSiblingIndex];
        }
    }
    return nextSibling;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Example 37 with DetailNode

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

the class JavadocUtil method containsInBranch.

/**
 * Checks whether node contains any node of specified type among children on any deep level.
 *
 * @param node DetailNode
 * @param type token type
 * @return true if node contains any node of type type among children on any deep level.
 */
public static boolean containsInBranch(DetailNode node, int type) {
    boolean result = true;
    DetailNode curNode = node;
    while (type != curNode.getType()) {
        DetailNode toVisit = getFirstChild(curNode);
        while (curNode != null && toVisit == null) {
            toVisit = getNextSibling(curNode);
            if (toVisit == null) {
                curNode = curNode.getParent();
            }
        }
        if (curNode == toVisit) {
            result = false;
            break;
        }
        curNode = toVisit;
    }
    return result;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Example 38 with DetailNode

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

the class JavadocMetadataScraper method getTagTextFromProperty.

/**
 * Get tag text from property data.
 *
 * @param nodeLi javadoc li item node
 * @param propertyMeta property javadoc node
 * @return property metadata text
 */
private static String getTagTextFromProperty(DetailNode nodeLi, DetailNode propertyMeta) {
    final Optional<DetailNode> tagNodeOpt = getFirstChildOfType(nodeLi, JavadocTokenTypes.JAVADOC_INLINE_TAG, propertyMeta.getIndex() + 1);
    DetailNode tagNode = null;
    if (tagNodeOpt.isPresent()) {
        tagNode = tagNodeOpt.get();
    }
    return getTextFromTag(tagNode);
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Example 39 with DetailNode

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

the class JavadocMissingWhitespaceAfterAsteriskCheck method visitJavadocToken.

@Override
public void visitJavadocToken(DetailNode detailNode) {
    final DetailNode textNode;
    if (detailNode.getType() == JavadocTokenTypes.JAVADOC) {
        textNode = JavadocUtil.getFirstChild(detailNode);
    } else {
        textNode = JavadocUtil.getNextSibling(detailNode);
    }
    if (textNode != null && textNode.getType() != JavadocTokenTypes.EOF) {
        final String text = textNode.getText();
        final int lastAsteriskPosition = getLastLeadingAsteriskPosition(text);
        if (!isLast(lastAsteriskPosition, text) && !Character.isWhitespace(text.charAt(lastAsteriskPosition + 1))) {
            log(textNode.getLineNumber(), textNode.getColumnNumber(), MSG_KEY);
        }
    }
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Example 40 with DetailNode

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

the class JavadocTagContinuationIndentationCheck method isViolation.

/**
 * Checks if a text node meets the criteria for a violation.
 * If the text is shorter than {@code offset} characters, then a violation is
 * detected if the text is not blank or the next node is not a newline.
 * If the text is longer than {@code offset} characters, then a violation is
 * detected if any of the first {@code offset} characters are not blank.
 *
 * @param textNode the node to check.
 * @return true if the node has a violation.
 */
private boolean isViolation(DetailNode textNode) {
    boolean result = false;
    final String text = textNode.getText();
    if (text.length() <= offset) {
        if (CommonUtil.isBlank(text)) {
            final DetailNode nextNode = JavadocUtil.getNextSibling(textNode);
            if (nextNode != null && nextNode.getType() != JavadocTokenTypes.NEWLINE) {
                // text is blank but line hasn't ended yet
                result = true;
            }
        } else {
            // text is not blank
            result = true;
        }
    } else if (!CommonUtil.isBlank(text.substring(1, offset + 1))) {
        // first offset number of characters are not blank
        result = true;
    }
    return result;
}
Also used : DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode)

Aggregations

DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)48 Test (org.junit.jupiter.api.Test)7 DetailAST (com.puppycrawl.tools.checkstyle.api.DetailAST)5 JavadocNodeImpl (com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocNodeImpl)4 ArrayDeque (java.util.ArrayDeque)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 Matcher (java.util.regex.Matcher)2 FileStatefulCheck (com.puppycrawl.tools.checkstyle.FileStatefulCheck)1 JavadocDetailNodeParser (com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser)1 JavadocTokenTypes (com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes)1 TokenTypes (com.puppycrawl.tools.checkstyle.api.TokenTypes)1 AbstractJavadocCheck (com.puppycrawl.tools.checkstyle.checks.javadoc.AbstractJavadocCheck)1 JavadocParser (com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser)1 TokenUtil (com.puppycrawl.tools.checkstyle.utils.TokenUtil)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1