Search in sources :

Example 1 with PerlPolyNamedNestedCallElementBase

use of com.perl5.lang.perl.psi.impl.PerlPolyNamedNestedCallElementBase in project Perl5-IDEA by Camelcade.

the class PerlIndentProcessor method getNodeIndent.

@NotNull
public Indent getNodeIndent(@NotNull ASTNode node) {
    IElementType nodeType = node.getElementType();
    ASTNode parent = node.getTreeParent();
    ASTNode grandParent = parent != null ? parent.getTreeParent() : null;
    IElementType parentNodeType = parent != null ? parent.getElementType() : null;
    IElementType grandParentNodeType = grandParent != null ? grandParent.getElementType() : null;
    ASTNode prevSibling = FormatterUtil.getPreviousNonWhitespaceSibling(node);
    IElementType prevSiblingElementType = prevSibling != null ? prevSibling.getElementType() : null;
    ASTNode nextSibling = FormatterUtil.getNextNonWhitespaceSibling(node);
    IElementType nextSiblingElementType = nextSibling != null ? nextSibling.getElementType() : null;
    boolean isFirst = prevSibling == null;
    boolean isLast = nextSibling == null;
    if (parentNodeType == ATTRIBUTES) {
        return Indent.getContinuationIndent();
    }
    if (isFirst && BLOCK_OPENERS.contains(nodeType) || isLast && BLOCK_CLOSERS.contains(nodeType)) {
        return Indent.getNoneIndent();
    }
    if (VARIABLE_DECLARATIONS.contains(parentNodeType) && (nodeType == LEFT_PAREN || nodeType == RIGHT_PAREN)) {
        return Indent.getNoneIndent();
    }
    boolean forceFirstIndent = false;
    if (HEREDOC_BODIES_TOKENSET.contains(nodeType)) {
        PsiElement psi = node.getPsi();
        assert psi instanceof PerlHeredocElementImpl;
        if (!((PerlHeredocElementImpl) psi).isIndentable()) {
            return Indent.getAbsoluteNoneIndent();
        }
        forceFirstIndent = true;
    }
    if (getAbsoluteUnindentableTokens().contains(nodeType)) {
        return Indent.getAbsoluteNoneIndent();
    }
    if (nodeType == BLOCK && MULTI_PARAM_BLOCK_CONTAINERS.contains(parentNodeType)) {
        return Indent.getNoneIndent();
    }
    if (parent == null || grandParent == null && nodeType != HEREDOC_END_INDENTABLE && !HEREDOC_BODIES_TOKENSET.contains(nodeType)) {
        return Indent.getNoneIndent();
    }
    if (getUnindentableTokens().contains(nodeType) || (nodeType instanceof PerlPolyNamedElementType && !(node.getPsi() instanceof PerlPolyNamedNestedCallElementBase))) {
        return Indent.getNoneIndent();
    }
    if (parentNodeType == STRING_LIST && (nodeType == QUOTE_SINGLE_OPEN || nodeType == QUOTE_SINGLE_CLOSE)) {
        return Indent.getNoneIndent();
    }
    if (nodeType == STRING_CONTENT && (parentNodeType == STRING_LIST || parentNodeType == LP_STRING_QW)) {
        return Indent.getContinuationIndent();
    }
    // defined by parent
    if (getUnindentableContainers().contains(parentNodeType)) {
        // a little magic for sub attributes
        if (parentNodeType == SUB_DEFINITION) {
            if (nodeType == COLON && nextSiblingElementType == ATTRIBUTE || nodeType == ATTRIBUTE && prevSiblingElementType != COLON) {
                return Indent.getContinuationIndent();
            }
        }
        return Indent.getNoneIndent();
    }
    if (PerlFormattingContext.COMMA_LIKE_SEQUENCES.contains(parentNodeType)) {
        return grandParentNodeType == STATEMENT ? Indent.getContinuationWithoutFirstIndent() : Indent.getContinuationIndent();
    }
    if (parentNodeType == CALL_ARGUMENTS) {
        return Indent.getContinuationIndent();
    }
    if (getBlockLikeContainers().contains(parentNodeType)) {
        return Indent.getNormalIndent();
    }
    return forceFirstIndent ? Indent.getContinuationIndent() : Indent.getContinuationWithoutFirstIndent();
}
Also used : IElementType(com.intellij.psi.tree.IElementType) PerlPolyNamedElementType(com.perl5.lang.perl.psi.stubs.PerlPolyNamedElementType) ASTNode(com.intellij.lang.ASTNode) PsiElement(com.intellij.psi.PsiElement) PerlHeredocElementImpl(com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl) PerlPolyNamedNestedCallElementBase(com.perl5.lang.perl.psi.impl.PerlPolyNamedNestedCallElementBase) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 PsiElement (com.intellij.psi.PsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlHeredocElementImpl (com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl)1 PerlPolyNamedNestedCallElementBase (com.perl5.lang.perl.psi.impl.PerlPolyNamedNestedCallElementBase)1 PerlPolyNamedElementType (com.perl5.lang.perl.psi.stubs.PerlPolyNamedElementType)1 NotNull (org.jetbrains.annotations.NotNull)1