Pass alignment strategy into PsiCodeBlock blocks, in order to align consecutive variable declarations inside code blocks (if/for/try/...). Fixes IDEA-143834

This commit is contained in:
Yaroslav Lepenkin
2015-08-27 10:49:14 +03:00
parent a68df2dc3e
commit b59ac4105d
3 changed files with 83 additions and 28 deletions
@@ -1213,14 +1213,15 @@ public abstract class AbstractJavaBlock extends AbstractBlock implements JavaBlo
return null;
}
private ChildAlignmentStrategyProvider getStrategyProvider() {
protected ChildAlignmentStrategyProvider getStrategyProvider() {
if (mySettings.ALIGN_GROUP_FIELD_DECLARATIONS && myNode.getElementType() == JavaElementType.CLASS) {
return new SubsequentFieldAligner(mySettings);
}
ASTNode parent = myNode.getTreeParent();
IElementType parentType = parent != null ? parent.getElementType() : null;
if (mySettings.ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS && parentType == JavaElementType.METHOD) {
if (mySettings.ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS
&& (parentType == JavaElementType.METHOD || myNode instanceof PsiCodeBlock)) {
return new SubsequentVariablesAligner();
}
@@ -18,11 +18,7 @@ package com.intellij.psi.formatter.java;
import com.intellij.formatting.*;
import com.intellij.formatting.alignment.AlignmentStrategy;
import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaTokenType;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiSyntheticClass;
import com.intellij.psi.TokenType;
import com.intellij.psi.*;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
import com.intellij.psi.formatter.FormatterUtil;
@@ -115,8 +111,10 @@ public class CodeBlockBlock extends AbstractJavaBlock {
state = INSIDE_BODY;
}
ChildAlignmentStrategyProvider provider = getStrategyProvider();
while (child != null) {
if (!FormatterUtil.containsWhiteSpacesOnly(child) && child.getTextLength() > 0) {
AlignmentStrategy alignmentStrategy = provider.getNextChildStrategy(child);
final Indent indent = calcCurrentIndent(child, state);
state = calcNewState(child, state);
@@ -132,7 +130,7 @@ public class CodeBlockBlock extends AbstractJavaBlock {
child = composeCodeBlock(result, child, indent, myChildrenIndent, childWrap);
}
else {
child = processChild(result, child, chooseAlignment(child, childAlignment), childWrap, indent);
child = processChild(result, child, alignmentStrategy, childWrap, indent);
}
}
if (child != null) {
@@ -141,26 +139,6 @@ public class CodeBlockBlock extends AbstractJavaBlock {
}
}
@Nullable
private Alignment chooseAlignment(@NotNull ASTNode child, @Nullable Alignment defaultAlignment) {
if (defaultAlignment != null) {
return defaultAlignment;
}
// Take special care about anonymous classes.
if (child.getElementType() != JavaTokenType.RBRACE) {
return defaultAlignment;
}
final ASTNode parent = child.getTreeParent();
if (parent == null || parent.getElementType() != JavaElementType.ANONYMOUS_CLASS) {
return defaultAlignment;
}
final ASTNode whiteSpaceCandidate = parent.getTreePrev();
if (whiteSpaceCandidate == null || whiteSpaceCandidate.getElementType() != TokenType.WHITE_SPACE) {
return defaultAlignment;
}
return StringUtil.countNewLines(whiteSpaceCandidate.getChars()) > 0 ? myAlignment : defaultAlignment;
}
@Nullable
private ASTNode processCaseAndStatementAfter(final ArrayList<Block> result,
ASTNode child,
@@ -657,4 +657,80 @@ public class JavaFormatterAlignmentTest extends AbstractJavaFormatterTest {
"}"
);
}
public void test_Align_ConsecutiveVars_InsideIfBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
"if (a > 2) {\n" +
"int a=2;\n" +
"String name=\"Yarik\";\n" +
"}\n",
"if (a > 2) {\n" +
" int a = 2;\n" +
" String name = \"Yarik\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideForBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" for (int i = 0; i < 10; i++) {\n" +
" int a=2;\n" +
" String name=\"Xa\";\n" +
" }\n",
"for (int i = 0; i < 10; i++) {\n" +
" int a = 2;\n" +
" String name = \"Xa\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideTryBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" try {\n" +
" int x = getX();\n" +
" String name = \"Ha\";\n" +
" }\n" +
" catch (IOException exception) {\n" +
" int y = 12;\n" +
" String test = \"Test\";\n" +
" }\n" +
" finally {\n" +
" int z = 12;\n" +
" String zzzz = \"pnmhd\";\n" +
" }\n",
"try {\n" +
" int x = getX();\n" +
" String name = \"Ha\";\n" +
"} catch (IOException exception) {\n" +
" int y = 12;\n" +
" String test = \"Test\";\n" +
"} finally {\n" +
" int z = 12;\n" +
" String zzzz = \"pnmhd\";\n" +
"}\n"
);
}
public void test_Align_ConsecutiveVars_InsideCodeBlock() {
getSettings().ALIGN_CONSECUTIVE_VARIABLE_DECLARATIONS = true;
doMethodTest(
" System.out.println(\"AAAA\");\n" +
" int a = 2;\n" +
" \n" +
" {\n" +
" int x=2;\n" +
" String name=3;\n" +
" }\n",
"System.out.println(\"AAAA\");\n" +
"int a = 2;\n" +
"\n" +
"{\n" +
" int x = 2;\n" +
" String name = 3;\n" +
"}\n"
);
}
}