From f08544d1194d557d4c4c4e56cc53da6d306ebe33 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Wed, 1 May 2013 17:34:16 +0400 Subject: [PATCH] format binary & parenthesized expressions --- .../processors/GroovyIndentProcessor.java | 11 +++ .../processors/GroovySpacingProcessor.java | 36 ++++------ .../lang/formatter/FormatterTest.groovy | 68 +++++++++++++++++++ 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java index a470ab6b348e..4217ddfe1f5f 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovyIndentProcessor.java @@ -49,6 +49,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSectio import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrConditionalExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrElvisExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrParenthesizedExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrExtendsClause; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrImplementsClause; import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody; @@ -332,6 +333,16 @@ public class GroovyIndentProcessor extends GroovyElementVisitor { return CodeStyleSettingsManager.getSettings(parent.getProject()).getCommonSettings(GroovyFileType.GROOVY_LANGUAGE); } + @Override + public void visitParenthesizedExpression(GrParenthesizedExpression expression) { + if (myChildType == mLPAREN || myChildType == mRPAREN) { + myResult = Indent.getNoneIndent(); + } + else { + myResult = Indent.getContinuationIndent(); + } + } + public static Indent getSwitchCaseIndent(PsiElement psiParent) { if (getGroovySettings(psiParent).INDENT_CASE_FROM_SWITCH) { return Indent.getNormalIndent(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java index 1d8ce5926390..d292539db746 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/processors/GroovySpacingProcessor.java @@ -670,26 +670,19 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { @Override public void visitBinaryExpression(GrBinaryExpression expression) { - if (isLeftOrRight(LOGICAL_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_LOGICAL_OPERATORS); + boolean spaceAround = isLeftOrRight(LOGICAL_OPERATORS) ? mySettings.SPACE_AROUND_LOGICAL_OPERATORS : + isLeftOrRight(EQUALITY_OPERATORS) ? mySettings.SPACE_AROUND_EQUALITY_OPERATORS : + isLeftOrRight(RELATIONAL_OPERATORS) ? mySettings.SPACE_AROUND_RELATIONAL_OPERATORS : + isLeftOrRight(BITWISE_OPERATORS) ? mySettings.SPACE_AROUND_BITWISE_OPERATORS : + isLeftOrRight(ADDITIVE_OPERATORS) ? mySettings.SPACE_AROUND_ADDITIVE_OPERATORS : + isLeftOrRight(MULTIPLICATIVE_OPERATORS) ? mySettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS : + isLeftOrRight(SHIFT_OPERATORS) ? mySettings.SPACE_AROUND_SHIFT_OPERATORS : + false; + if (TokenSets.BINARY_OP_SET.contains(myType2)) { + createDependentLFSpacing(mySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE, spaceAround, expression.getTextRange()); } - else if (isLeftOrRight(EQUALITY_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_EQUALITY_OPERATORS); - } - else if (isLeftOrRight(RELATIONAL_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_RELATIONAL_OPERATORS); - } - else if (isLeftOrRight(BITWISE_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_BITWISE_OPERATORS); - } - else if (isLeftOrRight(ADDITIVE_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_ADDITIVE_OPERATORS); - } - else if (isLeftOrRight(MULTIPLICATIVE_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_MULTIPLICATIVE_OPERATORS); - } - else if (isLeftOrRight(SHIFT_OPERATORS)) { - createSpaceInCode(mySettings.SPACE_AROUND_SHIFT_OPERATORS); + else { + createSpaceInCode(spaceAround); } } @@ -772,9 +765,8 @@ public class GroovySpacingProcessor extends GroovyElementVisitor { @Override public void visitParenthesizedExpression(GrParenthesizedExpression expression) { - if (myType1 == mLPAREN || myType2 == mRPAREN) { - createSpaceInCode(mySettings.SPACE_WITHIN_PARENTHESES); - } + processParentheses(mLPAREN, mRPAREN, mySettings.SPACE_WITHIN_PARENTHESES, null, mySettings.PARENTHESES_EXPRESSION_LPAREN_WRAP, + mySettings.PARENTHESES_EXPRESSION_RPAREN_WRAP); } @Override diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy index c687dc91e62d..95c7106f5473 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/formatter/FormatterTest.groovy @@ -613,6 +613,74 @@ for (int i = 3; ''') } + void testBinaryOperationSingOnNewLine() { + groovySettings.BINARY_OPERATION_SIGN_ON_NEXT_LINE = true + + checkFormatting('''\ +(1 + + 2) + 3 +''', '''\ +(1 + + + 2) + + 3 +''') + } + + void testParenthesized0() { + groovySettings.PARENTHESES_EXPRESSION_LPAREN_WRAP = true + checkFormatting('''\ +(2+ +3) +''', '''\ +( + 2 + + 3) +''') + } + + void testParenthesized1() { + groovySettings.PARENTHESES_EXPRESSION_RPAREN_WRAP = true + checkFormatting('''\ +(2+ +3) +''', '''\ +(2 + + 3 +) +''') + } + + void testParenthesized2() { + groovySettings.PARENTHESES_EXPRESSION_LPAREN_WRAP = true + groovySettings.PARENTHESES_EXPRESSION_RPAREN_WRAP = true + checkFormatting('''\ +(2+ +3) +''', '''\ +( + 2 + + 3 +) +''') + } + + void testParenthesized3() { + groovySettings.PARENTHESES_EXPRESSION_LPAREN_WRAP = true + groovySettings.PARENTHESES_EXPRESSION_RPAREN_WRAP = true + groovySettings.ALIGN_MULTILINE_BINARY_OPERATION = true + checkFormatting('''\ +(2+ +3) +''', '''\ +( + 2 + + 3 +) +''') + } + + private void doGeeseTest() { GroovyCodeStyleSettings customSettings = myTempSettings.getCustomSettings(GroovyCodeStyleSettings.class); boolean oldvalue = customSettings.USE_FLYING_GEESE_BRACES;