[groovy] handle wrapping and indents in array initializers (IDEA-188432)

This commit is contained in:
Daniil Ovchinnikov
2018-07-05 18:29:38 +03:00
parent 05076eb02d
commit df06a88f8e
11 changed files with 117 additions and 22 deletions
@@ -11,6 +11,7 @@ public class ThisIsASampleClass extends C1 implements I1, I2, I3, I4, I5 {
int
i = 0
int var1 = 1; int var2 = 2
new int[]{-1000,-100,-10,-1,0,1,10,100,1000,10000,100000}
foo1(0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057)
foo2 "a", "ab", "abc", "abcd", "abcde", "abcdef", "abcdefg", "abcdefgh"
int x = (3 + 4 + 5 + 6) * (7 + 8 + 9 + 10) * (11 + 12 + 13 + 14 + 0xFFFFFFFF)
@@ -125,10 +125,10 @@ public class GroovyLanguageCodeStyleSettingsProvider extends LanguageCodeStyleSe
"ALIGN_MULTILINE_TERNARY_OPERATION",
//"TERNARY_OPERATION_SIGNS_ON_NEXT_LINE",
//"ARRAY_INITIALIZER_WRAP",
//"ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION",
//"ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE",
//"ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE",
"ARRAY_INITIALIZER_WRAP",
"ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION",
"ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE",
"ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE",
"MODIFIER_LIST_WRAP",
@@ -1,5 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.formatter.blocks;
import com.intellij.formatting.*;
@@ -33,6 +32,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFile;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
import org.jetbrains.plugins.groovy.lang.psi.api.GrArrayInitializer;
import org.jetbrains.plugins.groovy.lang.psi.api.GrTryResourceList;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrThrowsClause;
@@ -620,7 +620,8 @@ public class GroovyBlockGenerator {
blockPsi instanceof GrExtendsClause && myContext.getSettings().ALIGN_MULTILINE_EXTENDS_LIST ||
blockPsi instanceof GrThrowsClause && myContext.getSettings().ALIGN_MULTILINE_THROWS_LIST ||
blockPsi instanceof GrListOrMap && myContext.getGroovySettings().ALIGN_MULTILINE_LIST_OR_MAP ||
blockPsi instanceof GrTryResourceList && myContext.getSettings().ALIGN_MULTILINE_RESOURCES;
blockPsi instanceof GrTryResourceList && myContext.getSettings().ALIGN_MULTILINE_RESOURCES ||
blockPsi instanceof GrArrayInitializer && myContext.getSettings().ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION;
}
private static boolean isListLikeClause(PsiElement blockPsi) {
@@ -630,7 +631,8 @@ public class GroovyBlockGenerator {
blockPsi instanceof GrExtendsClause ||
blockPsi instanceof GrThrowsClause ||
blockPsi instanceof GrListOrMap ||
blockPsi instanceof GrTryResourceList;
blockPsi instanceof GrTryResourceList ||
blockPsi instanceof GrArrayInitializer;
}
private static boolean isKeyword(ASTNode node) {
@@ -1,5 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.plugins.groovy.formatter.processors;
import com.intellij.formatting.ChildAttributes;
@@ -23,6 +22,7 @@ import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor;
import org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.api.GrArrayInitializer;
import org.jetbrains.plugins.groovy.lang.psi.api.GrTryResourceList;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrThrowsClause;
@@ -53,8 +53,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.types.GrWildcardTypeArgument;
import static com.intellij.formatting.Indent.*;
import static com.intellij.psi.codeStyle.CommonCodeStyleSettings.NEXT_LINE_SHIFTED;
import static com.intellij.psi.codeStyle.CommonCodeStyleSettings.NEXT_LINE_SHIFTED2;
import static org.jetbrains.plugins.groovy.lang.psi.GroovyElementTypes.T_LPAREN;
import static org.jetbrains.plugins.groovy.lang.psi.GroovyElementTypes.T_RPAREN;
import static org.jetbrains.plugins.groovy.lang.psi.GroovyElementTypes.*;
/**
* @author ilyas
@@ -483,6 +482,13 @@ public class GroovyIndentProcessor extends GroovyElementVisitor {
myResult = getContinuationWithoutFirstIndent();
}
@Override
public void visitArrayInitializer(@NotNull GrArrayInitializer arrayInitializer) {
if (myChildType != T_LBRACE && myChildType != T_RBRACE) {
myResult = getContinuationWithoutFirstIndent();
}
}
@NotNull
public static Indent getIndentInBlock(int braceStyle) {
return braceStyle == NEXT_LINE_SHIFTED ? getNoneIndent() : getNormalIndent();
@@ -627,12 +627,28 @@ public class GroovySpacingProcessor extends GroovyElementVisitor {
@Nullable Boolean spaceWithinEmpty,
@Nullable Boolean leftLF,
@Nullable Boolean rightLF) {
if (myType1 == T_LPAREN && myType2 == T_RPAREN && spaceWithinEmpty != null) {
return processParentheses(spaceWithin, spaceWithinEmpty, leftLF, rightLF, T_LPAREN, T_RPAREN);
}
private boolean processBraces(@NotNull Boolean spaceWithin,
@Nullable Boolean spaceWithinEmpty,
@Nullable Boolean leftLF,
@Nullable Boolean rightLF) {
return processParentheses(spaceWithin, spaceWithinEmpty, leftLF, rightLF, T_LBRACE, T_RBRACE);
}
private boolean processParentheses(@NotNull Boolean spaceWithin,
@Nullable Boolean spaceWithinEmpty,
@Nullable Boolean leftLF,
@Nullable Boolean rightLF,
@NotNull IElementType leftType,
@NotNull IElementType rightType) {
if (myType1 == leftType && myType2 == rightType && spaceWithinEmpty != null) {
createSpaceInCode(spaceWithinEmpty);
return true;
}
else if (myType1 == T_LPAREN) {
final ASTNode rparenth = findFrom(myChild1, T_RPAREN, true);
else if (myType1 == leftType) {
final ASTNode rparenth = findFrom(myChild1, rightType, true);
if (rparenth == null || leftLF == null) {
createSpaceInCode(spaceWithin);
}
@@ -642,8 +658,8 @@ public class GroovySpacingProcessor extends GroovyElementVisitor {
}
return true;
}
else if (myType2 == T_RPAREN) {
final ASTNode lparenth = findFrom(myChild1, T_LPAREN, false);
else if (myType2 == rightType) {
final ASTNode lparenth = findFrom(myChild1, leftType, false);
if (lparenth == null || rightLF == null) {
createSpaceInCode(spaceWithin);
}
@@ -1029,12 +1045,12 @@ public class GroovySpacingProcessor extends GroovyElementVisitor {
@Override
public void visitArrayInitializer(@NotNull GrArrayInitializer arrayInitializer) {
if (isWithinBraces()) {
createSpaceInCode(
arrayInitializer.isEmpty() ? mySettings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES
: mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES
);
}
processBraces(
mySettings.SPACE_WITHIN_ARRAY_INITIALIZER_BRACES,
mySettings.SPACE_WITHIN_EMPTY_ARRAY_INITIALIZER_BRACES,
mySettings.ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE,
mySettings.ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE
);
}
private static boolean isOpenBlock(IElementType type) {
@@ -18,7 +18,7 @@ import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation;
import static org.jetbrains.plugins.groovy.lang.psi.GroovyElementTypes.TRY_RESOURCE_LIST;
import static org.jetbrains.plugins.groovy.lang.psi.GroovyElementTypes.*;
/**
* @author Max Medvedev
@@ -99,6 +99,12 @@ public class GroovyWrappingProcessor {
}
}
if (myParentType == ARRAY_INITIALIZER) {
if (childType == T_LBRACE || childType == T_RBRACE) {
return createNoneWrap();
}
}
if (myParentType == GroovyElementTypes.THROW_CLAUSE && childType == GroovyTokenTypes.kTHROWS) {
return Wrap.createWrap(mySettings.THROWS_KEYWORD_WRAP, true);
}
@@ -220,6 +226,11 @@ public class GroovyWrappingProcessor {
return Wrap.createWrap(mySettings.ASSERT_STATEMENT_WRAP, false);
}
if (myParentType == ARRAY_INITIALIZER) {
myUsedDefaultWrap = true;
return Wrap.createWrap(mySettings.ARRAY_INITIALIZER_WRAP, false);
}
if (TokenSets.BLOCK_SET.contains(myParentType)) {
return createNormalWrap();
}
@@ -171,4 +171,12 @@ class GroovyCodeStyleFormatterTest extends GroovyFormatterTestCase {
void testBracesEndLine() { doTest() }
void testArrayInitializerSpaces() { doTest() }
void testArrayInitializerDontWrap() { doTest() }
void testArrayInitializerWrapAlways() { doTest() }
void testArrayInitializerWrapAlwaysAlign() { doTest() }
void testArrayInitializerWrapAlwaysNl() { doTest() }
}
@@ -0,0 +1,4 @@
<option>RIGHT_MARGIN=10</option>
new int[]{-1000,-100,-10,-1,0,1,10,100,1000,10000,100000}
-----
new int[]{-1000, -100, -10, -1, 0, 1, 10, 100, 1000, 10000, 100000}
@@ -0,0 +1,14 @@
<option>ARRAY_INITIALIZER_WRAP=WRAP_ALWAYS</option>
new int[]{-1000,-100,-10,-1,0,1,10,100,1000,10000,100000}
-----
new int[]{-1000,
-100,
-10,
-1,
0,
1,
10,
100,
1000,
10000,
100000}
@@ -0,0 +1,15 @@
<option>ARRAY_INITIALIZER_WRAP=WRAP_ALWAYS</option>
<option>ALIGN_MULTILINE_ARRAY_INITIALIZER_EXPRESSION=true</option>
new int[]{-1000,-100,-10,-1,0,1,10,100,1000,10000,100000}
-----
new int[]{-1000,
-100,
-10,
-1,
0,
1,
10,
100,
1000,
10000,
100000}
@@ -0,0 +1,18 @@
<option>ARRAY_INITIALIZER_WRAP=WRAP_ALWAYS</option>
<option>ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE=true</option>
<option>ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE=true</option>
new int[]{-1000,-100,-10,-1,0,1,10,100,1000,10000,100000}
-----
new int[]{
-1000,
-100,
-10,
-1,
0,
1,
10,
100,
1000,
10000,
100000
}