Search in sources :

Example 1 with PerlSyntheticBlock

use of com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock in project Perl5-IDEA by Camelcade.

the class PerlIndentProcessor method getChildIndent.

@Nullable
public Indent getChildIndent(@NotNull PerlAstBlock block, int newChildIndex) {
    IElementType elementType = block.getElementType();
    if (getUnindentableContainers().contains(elementType)) {
        return Indent.getNoneIndent();
    }
    if (getBlockLikeContainers().contains(elementType)) {
        return Indent.getNormalIndent();
    }
    if (block instanceof PerlSyntheticBlock && block.getSubBlocks().size() == newChildIndex) {
        ASTNode parentNode = block.getNode();
        ASTNode grandParentNode = parentNode.getTreeParent();
        return PsiUtilCore.getElementType(grandParentNode) == STATEMENT ? Indent.getNormalIndent() : Indent.getNoneIndent();
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) PerlSyntheticBlock(com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PerlSyntheticBlock

use of com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock in project Perl5-IDEA by Camelcade.

the class PerlFormattingContext method getSpacing.

@Nullable
public Spacing getSpacing(@NotNull ASTBlock parent, @Nullable Block child1, @NotNull Block child2) {
    if (parent instanceof PerlSyntheticBlock) {
        parent = ((PerlSyntheticBlock) parent).getRealBlock();
    }
    if (child1 instanceof PerlSyntheticBlock) {
        child1 = ((PerlSyntheticBlock) child1).getLastRealBlock();
    }
    if (child2 instanceof PerlSyntheticBlock) {
        child2 = ((PerlSyntheticBlock) child2).getFirstRealBlock();
    }
    if (child1 instanceof ASTBlock && child2 instanceof ASTBlock) {
        ASTNode parentNode = parent.getNode();
        IElementType parentNodeType = PsiUtilCore.getElementType(parentNode);
        ASTNode child1Node = ((ASTBlock) child1).getNode();
        IElementType child1Type = child1Node.getElementType();
        ASTNode child2Node = ((ASTBlock) child2).getNode();
        IElementType child2Type = child2Node.getElementType();
        if (parentNodeType == PARENTHESISED_EXPR && (child1Type == LEFT_PAREN || child2Type == RIGHT_PAREN) && parentNode.getPsi().getParent() instanceof PsiPerlStatementModifier) {
            return getSettings().SPACE_WITHIN_IF_PARENTHESES ? Spacing.createSpacing(1, 1, 0, true, 1) : Spacing.createSpacing(0, 0, 0, true, 1);
        }
        // fix for number/concat
        if (child2Type == OPERATOR_CONCAT) {
            ASTNode run = child1Node;
            while (run instanceof CompositeElement) {
                run = run.getLastChildNode();
            }
            if (run != null) {
                IElementType runType = run.getElementType();
                if (runType == NUMBER_SIMPLE || runType == NUMBER && StringUtil.endsWith(run.getText(), ".")) {
                    return Spacing.createSpacing(1, 1, 0, true, 1);
                }
            }
        }
        // LF after opening brace and before closing need to check if here-doc opener is in the line
        if (LF_ELEMENTS.contains(child1Type) && LF_ELEMENTS.contains(child2Type)) {
            if (!isNewLineForbiddenAt(child1Node)) {
                return Spacing.createSpacing(0, 0, 1, true, 1);
            } else {
                return Spacing.createSpacing(1, Integer.MAX_VALUE, 0, true, 1);
            }
        }
        // small inline blocks
        if (parentNodeType == BLOCK && !inGrepMapSort(parentNode) && !blockHasLessChildrenThan(parentNode, 2) && (BLOCK_OPENERS.contains(child1Type) && ((PerlFormattingBlock) child1).isFirst() || BLOCK_CLOSERS.contains(child2Type) && ((PerlFormattingBlock) child2).isLast()) && !isNewLineForbiddenAt(child1Node)) {
            return Spacing.createSpacing(0, 0, 1, true, 1);
        }
        if (parentNodeType == PARENTHESISED_CALL_ARGUMENTS && child2Type == RIGHT_PAREN && PsiUtilCore.getElementType(PsiTreeUtil.getDeepestLast(child1Node.getPsi())) == RIGHT_PAREN) {
            return Spacing.createSpacing(0, 0, 0, true, 0);
        }
        // hack for + ++/- --/~~ ~
        if ((child2Type == PREFIX_UNARY_EXPR || child2Type == PREF_PP_EXPR) && OPERATOR_COLLISIONS_MAP.containsKey(child1Type)) {
            IElementType rightSignType = PsiUtilCore.getElementType(child2Node.getFirstChildNode());
            if (OPERATOR_COLLISIONS_MAP.get(child1Type).contains(rightSignType)) {
                return Spacing.createSpacing(1, 1, 0, true, 1);
            }
        } else // hack for ++ +/-- -
        if (child1Type == SUFF_PP_EXPR && OPERATOR_COLLISIONS_MAP.containsKey(child2Type)) {
            IElementType leftSignType = PsiUtilCore.getElementType(child1Node.getLastChildNode());
            if (OPERATOR_COLLISIONS_MAP.get(child2Type).contains(leftSignType)) {
                return Spacing.createSpacing(1, 1, 0, true, 1);
            }
        }
    }
    return getSpacingBuilder().getSpacing(parent, child1, child2);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) PerlSyntheticBlock(com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock) PsiPerlStatementModifier(com.perl5.lang.perl.psi.PsiPerlStatementModifier) CompositeElement(com.intellij.psi.impl.source.tree.CompositeElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ASTNode (com.intellij.lang.ASTNode)2 IElementType (com.intellij.psi.tree.IElementType)2 PerlSyntheticBlock (com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock)2 Nullable (org.jetbrains.annotations.Nullable)2 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)1 PsiPerlStatementModifier (com.perl5.lang.perl.psi.PsiPerlStatementModifier)1