Search in sources :

Example 1 with PerlFormattingBlock

use of com.perl5.lang.perl.idea.formatter.blocks.PerlFormattingBlock 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)

Example 2 with PerlFormattingBlock

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

the class PerlFormattingModelBuilder method createModel.

@Override
@NotNull
public FormattingModel createModel(@NotNull FormattingContext formattingContext) {
    var element = formattingContext.getPsiElement();
    var range = formattingContext.getFormattingRange();
    var settings = formattingContext.getCodeStyleSettings();
    var mode = formattingContext.getFormattingMode();
    PerlTimeLogger logger = PerlTimeLogger.create(LOG);
    TextRange computedRange = COMPUTED_RANGE_KEY.get(element);
    COMPUTED_RANGE_KEY.set(element, null);
    if (computedRange != null && range.contains(computedRange)) {
        LOG.debug("Got pre-computed range: ", computedRange);
        range = computedRange;
    } else {
        LOG.debug("No pre-computed range, trying to reduce provided:", range);
        range = computeFormattingRange(element.getContainingFile(), range);
    }
    PerlFormattingBlock rootBlock = new PerlFormattingBlock(element.getNode(), new PurePerlFormattingContext(formattingContext, range));
    PerlDocumentBasedFormattingModel model = new PerlDocumentBasedFormattingModel(rootBlock, element, settings);
    if (LOG.isDebugEnabled()) {
        logger.debug("Created model for: ", element.getNode(), "; range: ", range, "; settings: ", settings, "; mode: ", mode);
        if (LOG.isTraceEnabled()) {
            LOG.trace("Tree size: " + computeTreeSize(rootBlock));
        }
    }
    return model;
}
Also used : PerlTimeLogger(com.perl5.lang.perl.util.PerlTimeLogger) TextRange(com.intellij.openapi.util.TextRange) PerlFormattingBlock(com.perl5.lang.perl.idea.formatter.blocks.PerlFormattingBlock) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)1 IElementType (com.intellij.psi.tree.IElementType)1 PerlFormattingBlock (com.perl5.lang.perl.idea.formatter.blocks.PerlFormattingBlock)1 PerlSyntheticBlock (com.perl5.lang.perl.idea.formatter.blocks.PerlSyntheticBlock)1 PsiPerlStatementModifier (com.perl5.lang.perl.psi.PsiPerlStatementModifier)1 PerlTimeLogger (com.perl5.lang.perl.util.PerlTimeLogger)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1