private static boolean isInJspHolderMethod(PsiExpression expr) { final PsiElement parent1 = expr.getParent(); if (parent1 == null) { return false; } final PsiElement parent2 = parent1.getParent(); if (!(parent2 instanceof JspCodeBlock)) return false; final PsiElement parent3 = parent2.getParent(); return parent3 instanceof JspHolderMethod; }
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(0, 0, 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(0, 0, 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(); // We want to avoid situations like below: // 1. Call 'introduce variable' refactoring for the code like 'System.out.println(1);'; // 2. When KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE is on, the output looks like 'int i = 1; System.out.println(i);'; // 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); } }
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( 0, 0, 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(0, 0, 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(mySettings.SPACE_BEFORE_SWITCH_LBRACE, mySettings.BRACE_STYLE, null, false, true); } else if (lhsStatement && rhsStatement) { int minSpaces = 0; int minLineFeeds = 1; PsiElement psi = myChild1.getPsi(); // We want to avoid situations like below: // 1. Call 'introduce variable' refactoring for the code like 'System.out.println(1);'; // 2. When KEEP_MULTIPLE_EXPRESSIONS_IN_ONE_LINE is on, the output looks like 'int i = 1; System.out.println(i);'; // 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; } myResult = Spacing.createSpacing(minSpaces, 0, minLineFeeds, mySettings.KEEP_LINE_BREAKS, mySettings.KEEP_BLANK_LINES_IN_CODE); } }