Search in sources :

Example 1 with ClosureBodyBlock

use of org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock in project intellij-community by JetBrains.

the class GroovyIndentProcessor method getChildIndent.

/**
   * Calculates indent, based on code style, between parent block and child node
   *
   * @param parentBlock parent block
   * @param child       child node
   * @return indent
   */
@NotNull
public Indent getChildIndent(@NotNull final GroovyBlock parentBlock, @NotNull final ASTNode child) {
    myChildType = child.getElementType();
    if (parentBlock instanceof ClosureBodyBlock) {
        if (myChildType == GroovyElementTypes.PARAMETERS_LIST) {
            return Indent.getNoneIndent();
        } else if (myChildType != GroovyTokenTypes.mLCURLY && myChildType != GroovyTokenTypes.mRCURLY) {
            return Indent.getNormalIndent();
        }
    }
    if (parentBlock instanceof GrLabelBlock) {
        ASTNode first = parentBlock.getNode().getFirstChildNode();
        return child == first ? Indent.getNoneIndent() : Indent.getLabelIndent();
    }
    if (GSTRING_TOKENS_INNER.contains(myChildType)) {
        return Indent.getAbsoluteNoneIndent();
    }
    final PsiElement parent = parentBlock.getNode().getPsi();
    if (parent instanceof GroovyPsiElement) {
        myBlock = parentBlock;
        myChild = child.getPsi();
        ((GroovyPsiElement) parent).accept(this);
        if (myResult != null)
            return myResult;
    }
    return Indent.getNoneIndent();
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrLabelBlock(org.jetbrains.plugins.groovy.formatter.blocks.GrLabelBlock) ASTNode(com.intellij.lang.ASTNode) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ClosureBodyBlock

use of org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock in project intellij-community by JetBrains.

the class GroovySpacingProcessorBasic method getSpacing.

public static Spacing getSpacing(GroovyBlock child1, GroovyBlock child2, FormattingContext context) {
    ASTNode leftNode = child1.getNode();
    ASTNode rightNode = child2.getNode();
    final PsiElement left = leftNode.getPsi();
    final PsiElement right = rightNode.getPsi();
    IElementType leftType = leftNode.getElementType();
    IElementType rightType = rightNode.getElementType();
    final CommonCodeStyleSettings settings = context.getSettings();
    final GroovyCodeStyleSettings groovySettings = context.getGroovySettings();
    if (!(mirrorsAst(child1) && mirrorsAst(child2))) {
        return NO_SPACING;
    }
    if (child2 instanceof ClosureBodyBlock) {
        return settings.SPACE_WITHIN_BRACES ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (child1 instanceof ClosureBodyBlock) {
        return createDependentSpacingForClosure(settings, groovySettings, (GrClosableBlock) left.getParent(), false);
    }
    if (leftType == GroovyDocElementTypes.GROOVY_DOC_COMMENT) {
        return COMMON_SPACING_WITH_NL;
    }
    if (right instanceof GrTypeArgumentList) {
        return NO_SPACING_WITH_NEWLINE;
    }
    /********** punctuation marks ************/
    if (GroovyTokenTypes.mCOMMA == leftType) {
        return settings.SPACE_AFTER_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mCOMMA == rightType) {
        return settings.SPACE_BEFORE_COMMA ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == leftType) {
        return settings.SPACE_AFTER_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyTokenTypes.mSEMI == rightType) {
        return settings.SPACE_BEFORE_SEMICOLON ? COMMON_SPACING : NO_SPACING_WITH_NEWLINE;
    }
    // For dots, commas etc.
    if ((TokenSets.DOTS.contains(rightType)) || (GroovyTokenTypes.mCOLON.equals(rightType) && !(right.getParent() instanceof GrConditionalExpression))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (TokenSets.DOTS.contains(leftType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    //todo:check it for multiple assignments
    if ((GroovyElementTypes.VARIABLE_DEFINITION.equals(leftType) || GroovyElementTypes.VARIABLE_DEFINITION.equals(rightType)) && !(leftNode.getTreeNext() instanceof PsiErrorElement)) {
        return Spacing.createSpacing(0, 0, 1, false, 100);
    }
    // For regexes
    if (leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mREGEX_LITERAL || leftNode.getTreeParent().getElementType() == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
        return NO_SPACING;
    }
    // For << and >> ...
    if ((GroovyTokenTypes.mLT.equals(leftType) && GroovyTokenTypes.mLT.equals(rightType)) || (GroovyTokenTypes.mGT.equals(leftType) && GroovyTokenTypes.mGT.equals(rightType))) {
        return NO_SPACING_WITH_NEWLINE;
    }
    // Unary and postfix expressions
    if (SpacingTokens.PREFIXES.contains(leftType) || SpacingTokens.POSTFIXES.contains(rightType) || (SpacingTokens.PREFIXES_OPTIONAL.contains(leftType) && left.getParent() instanceof GrUnaryExpression)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (SpacingTokens.RANGES.contains(leftType) || SpacingTokens.RANGES.contains(rightType)) {
        return NO_SPACING_WITH_NEWLINE;
    }
    if (GroovyDocTokenTypes.mGDOC_ASTERISKS == leftType && GroovyDocTokenTypes.mGDOC_COMMENT_DATA == rightType) {
        String text = rightNode.getText();
        if (!text.isEmpty() && !StringUtil.startsWithChar(text, ' ')) {
            return COMMON_SPACING;
        }
        return NO_SPACING;
    }
    if (leftType == GroovyDocTokenTypes.mGDOC_TAG_VALUE_TOKEN && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) {
        return LAZY_SPACING;
    }
    if (left instanceof GrStatement && right instanceof GrStatement && left.getParent() instanceof GrStatementOwner && right.getParent() instanceof GrStatementOwner) {
        return COMMON_SPACING_WITH_NL;
    }
    if (rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || rightType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_START || leftType == GroovyDocTokenTypes.mGDOC_INLINE_TAG_END) {
        return NO_SPACING;
    }
    if ((leftType == GroovyDocElementTypes.GDOC_INLINED_TAG && rightType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA) || (leftType == GroovyDocTokenTypes.mGDOC_COMMENT_DATA && rightType == GroovyDocElementTypes.GDOC_INLINED_TAG)) {
        // Keep formatting between groovy doc text and groovy doc reference tag as is.
        return NO_SPACING;
    }
    if (leftType == GroovyElementTypes.CLASS_TYPE_ELEMENT && rightType == GroovyTokenTypes.mTRIPLE_DOT) {
        return NO_SPACING;
    }
    // diamonds
    if (rightType == GroovyTokenTypes.mLT || rightType == GroovyTokenTypes.mGT) {
        if (right.getParent() instanceof GrCodeReferenceElement) {
            PsiElement p = right.getParent().getParent();
            if (p instanceof GrNewExpression || p instanceof GrAnonymousClassDefinition) {
                return NO_SPACING;
            }
        }
    }
    return COMMON_SPACING;
}
Also used : GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement) IElementType(com.intellij.psi.tree.IElementType) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) PsiErrorElement(com.intellij.psi.PsiErrorElement) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) ClosureBodyBlock(org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock) GrConditionalExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression) GrTypeArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList) PsiElement(com.intellij.psi.PsiElement)

Aggregations

ASTNode (com.intellij.lang.ASTNode)2 PsiElement (com.intellij.psi.PsiElement)2 ClosureBodyBlock (org.jetbrains.plugins.groovy.formatter.blocks.ClosureBodyBlock)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1 GroovyCodeStyleSettings (org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings)1 GrLabelBlock (org.jetbrains.plugins.groovy.formatter.blocks.GrLabelBlock)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)1 GrConditionalExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression)1 GrNewExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression)1 GrUnaryExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression)1 GrAnonymousClassDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition)1 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)1 GrTypeArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeArgumentList)1 GrStatementOwner (org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner)1