Fix problem with alignment in assignments and other binary expressions

This commit is contained in:
Nikolay Krasko
2014-05-14 18:51:04 +04:00
parent 356cb80e7f
commit d4ddf06ebd
19 changed files with 285 additions and 7 deletions
@@ -53,7 +53,11 @@ public class JetBlock extends AbstractBlock {
private List<Block> mySubBlocks;
private static final TokenSet BINARY_EXPRESSIONS = TokenSet.create(BINARY_EXPRESSION, BINARY_WITH_TYPE, IS_EXPRESSION);
private static final TokenSet QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS);
private static final TokenSet ALIGN_FOR_BINARY_OPERATIONS =
TokenSet.create(MUL, DIV, PERC, PLUS, MINUS, ELVIS, LT, GT, LTEQ, GTEQ, ANDAND, OROR);
private static final TokenSet CODE_BLOCKS = TokenSet.create(
BLOCK,
CLASS_BODY,
@@ -229,7 +233,7 @@ public class JetBlock extends AbstractBlock {
else if (parentType == WHEN) {
return getAlignmentForCaseBranch(jetSettings.ALIGN_IN_COLUMNS_CASE_BRANCH);
}
else if (parentType == BINARY_EXPRESSION) {
else if (BINARY_EXPRESSIONS.contains(parentType) && ALIGN_FOR_BINARY_OPERATIONS.contains(getOperationType(getNode()))) {
return NodeAlignmentStrategy.fromTypes(AlignmentStrategy.wrap(
createAlignment(jetCommonSettings.ALIGN_MULTILINE_BINARY_OPERATION, getAlignment())));
}
@@ -352,7 +356,7 @@ public class JetBlock extends AbstractBlock {
.set(Indent.getContinuationIndent(false)),
strategy("Binary expressions")
.in(BINARY_EXPRESSION)
.in(BINARY_EXPRESSIONS)
.set(Indent.getContinuationWithoutFirstIndent(false)),
strategy("Parenthesized expression")
@@ -434,4 +438,10 @@ public class JetBlock extends AbstractBlock {
}
});
}
@Nullable
private static IElementType getOperationType(ASTNode node) {
ASTNode operationNode = node.findChildByType(OPERATION_REFERENCE);
return operationNode != null ? operationNode.getFirstChildNode().getElementType() : null;
}
}
@@ -19,10 +19,12 @@ package org.jetbrains.jet.plugin.formatter;
import com.intellij.formatting.Indent;
import com.intellij.lang.ASTNode;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -77,6 +79,16 @@ public abstract class NodeIndentStrategy {
return this;
}
public PositionStrategy in(@NotNull TokenSet parents) {
IElementType[] types = parents.getTypes();
if (types.length == 0) {
throw new IllegalArgumentException("Empty token set is unexpected");
}
fillTypes(in, types[0], Arrays.copyOfRange(types, 1, types.length));
return this;
}
public PositionStrategy in(@NotNull IElementType parentType, @NotNull IElementType... orParentTypes) {
fillTypes(in, parentType, orParentTypes);
return this;
@@ -0,0 +1,41 @@
fun test() {
var a = 12
a =
12
a +=
12
a -=
12
a *=
12
a /=
12
a is
String
a !is
String
a as
String
a as?
String
a :
String
a in
1..2
a !in
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,41 @@
fun test() {
var a = 12
a =
12
a +=
12
a -=
12
a *=
12
a /=
12
a is
String
a !is
String
a as
String
a as?
String
a :
String
a in
1..2
a !in
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,41 @@
fun test() {
var a = 12
a =
12
a +=
12
a -=
12
a *=
12
a /=
12
a is
String
a !is
String
a as
String
a as?
String
a :
String
a in
1..2
a !in
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,17 @@
fun test(a: Int?) {
a ?: 42
a ?: 42
a ?:
42
a
?: 42
val some = a ?:
b ?:
12
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
+9 -3
View File
@@ -4,8 +4,14 @@ fun test(a: Int?) {
a ?: 42
a ?:
42
42
a
?: 42
}
?: 42
val some = a ?:
b ?:
12
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
+7 -1
View File
@@ -8,4 +8,10 @@ fun test(a: Int?) {
a
?: 42
}
val some = a ?:
b ?:
12
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,7 @@
fun test() {
var a = 1
a =
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,7 @@
fun test() {
var a = 1
a =
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,6 @@
fun test() {
var a = 1
a = <caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,7 @@
fun test() {
var a = 1
a as
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,7 @@
fun test() {
var a = 1
a as
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,6 @@
fun test() {
var a = 1
a as <caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,6 @@
fun test() {
1 is
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,6 @@
fun test() {
1 is
<caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -0,0 +1,5 @@
fun test() {
1 is <caret>
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
@@ -54,6 +54,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTest("idea/testData/formatter/BinaryExpressionsBoolean.after.kt");
}
@TestMetadata("BinaryExpressionsWithoutAlignment.after.kt")
public void testBinaryExpressionsWithoutAlignment() throws Exception {
doTest("idea/testData/formatter/BinaryExpressionsWithoutAlignment.after.kt");
}
@TestMetadata("BlockFor.after.kt")
public void testBlockFor() throws Exception {
doTest("idea/testData/formatter/BlockFor.after.kt");
@@ -479,6 +484,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted("idea/testData/formatter/BinaryExpressionsBoolean.after.inv.kt");
}
@TestMetadata("BinaryExpressionsWithoutAlignment.after.inv.kt")
public void testBinaryExpressionsWithoutAlignment() throws Exception {
doTestInverted("idea/testData/formatter/BinaryExpressionsWithoutAlignment.after.inv.kt");
}
@TestMetadata("CatchFinallyOnNewLine.after.inv.kt")
public void testCatchFinallyOnNewLine() throws Exception {
doTestInverted("idea/testData/formatter/CatchFinallyOnNewLine.after.inv.kt");
@@ -509,6 +519,11 @@ public class JetFormatterTestGenerated extends AbstractJetFormatterTest {
doTestInverted("idea/testData/formatter/ElseOnNewLine.after.inv.kt");
}
@TestMetadata("Elvis.after.inv.kt")
public void testElvis() throws Exception {
doTestInverted("idea/testData/formatter/Elvis.after.inv.kt");
}
@TestMetadata("EmptyBlocks.after.inv.kt")
public void testEmptyBlocks() throws Exception {
doTestInverted("idea/testData/formatter/EmptyBlocks.after.inv.kt");
@@ -61,7 +61,19 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde
}
public void testAllFilesPresentInDirectSettings() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/indentationOnNewline"), Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true);
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage",
new File("idea/testData/indentationOnNewline"),
Pattern.compile("^([^\\.]+)\\.after\\.kt.*$"), true);
}
@TestMetadata("AssignmentAfterEq.after.kt")
public void testAssignmentAfterEq() throws Exception {
doNewlineTest("idea/testData/indentationOnNewline/AssignmentAfterEq.after.kt");
}
@TestMetadata("BinaryWithTypeExpressions.after.kt")
public void testBinaryWithTypeExpressions() throws Exception {
doNewlineTest("idea/testData/indentationOnNewline/BinaryWithTypeExpressions.after.kt");
}
@TestMetadata("ConsecutiveCallsAfterDot.after.kt")
@@ -204,6 +216,11 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde
doNewlineTest("idea/testData/indentationOnNewline/InMultilineLambdaAfterArrow.after.kt");
}
@TestMetadata("IsExpressionAfterIs.after.kt")
public void testIsExpressionAfterIs() throws Exception {
doNewlineTest("idea/testData/indentationOnNewline/IsExpressionAfterIs.after.kt");
}
@TestMetadata("MultideclarationAfterEq.after.kt")
public void testMultideclarationAfterEq() throws Exception {
doNewlineTest("idea/testData/indentationOnNewline/MultideclarationAfterEq.after.kt");
@@ -282,6 +299,16 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("idea/testData/indentationOnNewline"), Pattern.compile("^([^\\.]+)\\.after\\.inv\\.kt.*$"), true);
}
@TestMetadata("AssignmentAfterEq.after.inv.kt")
public void testAssignmentAfterEq() throws Exception {
doNewlineTestWithInvert("idea/testData/indentationOnNewline/AssignmentAfterEq.after.inv.kt");
}
@TestMetadata("BinaryWithTypeExpressions.after.inv.kt")
public void testBinaryWithTypeExpressions() throws Exception {
doNewlineTestWithInvert("idea/testData/indentationOnNewline/BinaryWithTypeExpressions.after.inv.kt");
}
@TestMetadata("InBinaryExpressionInMiddle.after.inv.kt")
public void testInBinaryExpressionInMiddle() throws Exception {
doNewlineTestWithInvert("idea/testData/indentationOnNewline/InBinaryExpressionInMiddle.after.inv.kt");
@@ -337,6 +364,11 @@ public class JetTypingIndentationTestBaseGenerated extends AbstractJetTypingInde
doNewlineTestWithInvert("idea/testData/indentationOnNewline/InExpressionsParenthesesBeforeOperand.after.inv.kt");
}
@TestMetadata("IsExpressionAfterIs.after.inv.kt")
public void testIsExpressionAfterIs() throws Exception {
doNewlineTestWithInvert("idea/testData/indentationOnNewline/IsExpressionAfterIs.after.inv.kt");
}
@TestMetadata("SettingAlignMultilineParametersInCalls.after.inv.kt")
public void testSettingAlignMultilineParametersInCalls() throws Exception {
doNewlineTestWithInvert("idea/testData/indentationOnNewline/SettingAlignMultilineParametersInCalls.after.inv.kt");