Search in sources :

Example 1 with JspCodeBlock

use of com.intellij.psi.impl.source.jsp.jspJava.JspCodeBlock in project intellij-community by JetBrains.

the class JavaSpacePropertyProcessor method processCodeBlock.

private void processCodeBlock(final boolean keepInOneLine, final TextRange textRange) {
    final boolean lhsStatement = myChild1.getPsi() instanceof PsiStatement;
    final boolean rhsStatement = myChild2.getPsi() instanceof PsiStatement;
    if (myParent instanceof JspCodeBlock) {
        myResult = Spacing.createSpacing(0, 0, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
    } else if (myRole1 == ChildRoleBase.NONE && !lhsStatement || myRole2 == ChildRoleBase.NONE && !rhsStatement) {
        final IElementType firstElementType = myChild1.getElementType();
        if (firstElementType == JavaTokenType.END_OF_LINE_COMMENT || firstElementType == JavaTokenType.C_STYLE_COMMENT) {
            myResult = Spacing.createDependentLFSpacing(0, 1, myParent.getTextRange(), mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
        } else {
            myResult = null;
        }
    } else if (myRole1 == ChildRole.LBRACE) {
        if (!keepInOneLine) {
            int blankLines = 1;
            if (myParent != null) {
                ASTNode parentNode = myParent.getNode();
                if (parentNode != null) {
                    ASTNode grandPa = parentNode.getTreeParent();
                    if (grandPa != null && grandPa.getElementType() == JavaElementType.METHOD) {
                        blankLines += mySettings.BLANK_LINES_BEFORE_METHOD_BODY;
                    }
                }
            }
            myResult = Spacing.createSpacing(1, 1, blankLines, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
        } else {
            myResult = Spacing.createDependentLFSpacing(0, 1, textRange, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
        }
    } else if (myRole2 == ChildRole.RBRACE) {
        if (!keepInOneLine) {
            myResult = Spacing.createSpacing(1, 1, 1, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_BEFORE_RBRACE);
        } else {
            myResult = Spacing.createDependentLFSpacing(0, 1, textRange, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_BEFORE_RBRACE);
        }
    } else if (myChild1.getElementType() == JavaElementType.SWITCH_LABEL_STATEMENT && myChild2.getElementType() == JavaElementType.BLOCK_STATEMENT) {
        myResult = getSpaceBeforeLBrace(myChild2, mySettings.SPACE_BEFORE_SWITCH_LBRACE, null);
    } else if (lhsStatement && rhsStatement) {
        int minSpaces = 0;
        int minLineFeeds = 1;
        PsiElement psi = myChild1.getPsi();
        // That's why we process the option only during the explicit reformat (directly invoked by an user).
        if ((mySettings.KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE && (FormatterUtil.isFormatterCalledExplicitly() || ApplicationManager.getApplication().isUnitTestMode())) || psi != null && PsiTreeUtil.hasErrorElements(psi)) {
            minSpaces = 1;
            minLineFeeds = 0;
            if (myChild1 != null) {
                ASTNode lastElement = myChild1;
                while (lastElement.getLastChildNode() != null) lastElement = lastElement.getLastChildNode();
                //Not to place second statement on the same line with first one, if last ends with single line comment
                if (lastElement instanceof PsiComment && lastElement.getElementType() == JavaTokenType.END_OF_LINE_COMMENT)
                    minLineFeeds = 1;
            }
        }
        myResult = Spacing.createSpacing(minSpaces, 0, minLineFeeds, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) JspCodeBlock(com.intellij.psi.impl.source.jsp.jspJava.JspCodeBlock)

Aggregations

ASTNode (com.intellij.lang.ASTNode)1 JspCodeBlock (com.intellij.psi.impl.source.jsp.jspJava.JspCodeBlock)1 IElementType (com.intellij.psi.tree.IElementType)1