From f8c6f4136fb96777231de0a3589e72f8bd1576b5 Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Wed, 1 May 2013 15:19:29 +0400 Subject: [PATCH] align traditional for --- .../formatter/GroovyBlockGenerator.java | 25 +++++++++++-------- .../lang/formatter/FormatterTest.groovy | 13 ++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlockGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlockGenerator.java index 058ea80c4750..2f3aa4ec421b 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlockGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/formatter/GroovyBlockGenerator.java @@ -57,6 +57,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgument import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrTraditionalForClause; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList; @@ -244,19 +245,23 @@ public class GroovyBlockGenerator implements GroovyElementTypes { return generateSubBlockForCodeBlocks(classLevel, visibleChildren(myNode)); } - if (blockPsi instanceof GrMethod) { - final AlignmentProvider.Aligner parenthesesAligner = mySettings.ALIGN_MULTILINE_METHOD_BRACKETS ? myAlignmentProvider.createAligner(false) : null; - - final ArrayList subBlocks = new ArrayList(); - for (ASTNode childNode : visibleChildren(myNode)) { - final Indent indent = new GroovyIndentProcessor().getChildIndent(myBlock, childNode); - if (childNode.getElementType() == mLPAREN && parenthesesAligner != null) parenthesesAligner.append(childNode.getPsi()); - if (childNode.getElementType() == mRPAREN && parenthesesAligner != null) parenthesesAligner.append(childNode.getPsi()); - subBlocks.add(new GroovyBlock(childNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider)); + if (blockPsi instanceof GrMethod && mySettings.ALIGN_MULTILINE_METHOD_BRACKETS) { + final ASTNode lparenth = myNode.findChildByType(mLPAREN); + final ASTNode rparenth = myNode.findChildByType(mRPAREN); + if (lparenth != null && rparenth != null) { + myAlignmentProvider.addPair(lparenth, rparenth, false); } - return subBlocks; } + if (blockPsi instanceof GrTraditionalForClause && mySettings.ALIGN_MULTILINE_FOR) { + final GrTraditionalForClause clause = (GrTraditionalForClause)blockPsi; + final AlignmentProvider.Aligner parenthesesAligner = myAlignmentProvider.createAligner(false); + parenthesesAligner.append(clause.getInitialization()); + parenthesesAligner.append(clause.getCondition()); + parenthesesAligner.append(clause.getUpdate()); + } + + // For other cases final ArrayList subBlocks = new ArrayList(); for (ASTNode childNode : visibleChildren(myNode)) { 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 49a5a0ce582e..c687dc91e62d 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 @@ -600,6 +600,19 @@ def foooo( ''') } + void testAlignFor() { + groovySettings.ALIGN_MULTILINE_FOR = true + checkFormatting('''\ +for (int i = 3; +i<2; +i++) print 2 +''', '''\ +for (int i = 3; + i < 2; + i++) print 2 +''') + } + private void doGeeseTest() { GroovyCodeStyleSettings customSettings = myTempSettings.getCustomSettings(GroovyCodeStyleSettings.class); boolean oldvalue = customSettings.USE_FLYING_GEESE_BRACES;