Make assignment indent conforms with indent for expression bodies (KT-28484)

Do not make plain list of binary expression for assignment expressions.

 #KT-28484 Fixed
This commit is contained in:
Nikolay Krasko
2018-11-27 15:23:33 +03:00
parent 699e3397d9
commit 05b2443988
9 changed files with 167 additions and 12 deletions
@@ -434,9 +434,14 @@ abstract class KotlinCommonBlock(
val childNodes = when {
overrideChildren != null -> overrideChildren.asSequence()
node.elementType == KtNodeTypes.BINARY_EXPRESSION -> {
val binaryExpressionChildren = mutableListOf<ASTNode>()
collectBinaryExpressionChildren(node, binaryExpressionChildren)
binaryExpressionChildren.asSequence()
val binaryExpression = node.psi as? KtBinaryExpression
if (binaryExpression != null && ALL_ASSIGNMENTS.contains(binaryExpression.operationToken)) {
node.children()
} else {
val binaryExpressionChildren = mutableListOf<ASTNode>()
collectBinaryExpressionChildren(node, binaryExpressionChildren)
binaryExpressionChildren.asSequence()
}
}
else -> node.children()
}
@@ -725,6 +730,21 @@ private val INDENT_RULES = arrayOf(
}
.continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES),
strategy("Assignment expressions")
.within(KtNodeTypes.BINARY_EXPRESSION)
.within {
val binaryExpression = it.psi as? KtBinaryExpression
?: return@within false
return@within KtTokens.ALL_ASSIGNMENTS.contains(binaryExpression.operationToken)
}
.forElement {
val psi = it.psi
val binaryExpression = psi?.parent as? KtBinaryExpression
binaryExpression?.right == psi
}
.continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES),
strategy("Indent for parts")
.within(KtNodeTypes.PROPERTY, KtNodeTypes.FUN, KtNodeTypes.DESTRUCTURING_DECLARATION, KtNodeTypes.SECONDARY_CONSTRUCTOR)
.notForType(
@@ -755,7 +775,9 @@ private val INDENT_RULES = arrayOf(
strategy("Binary expressions")
.within(BINARY_EXPRESSIONS)
.forElement { node -> !node.suppressBinaryExpressionIndent() }
.forElement { node ->
!node.suppressBinaryExpressionIndent()
}
.set(Indent.getContinuationWithoutFirstIndent(false)),
strategy("Parenthesized expression")
@@ -38,7 +38,10 @@ abstract class NodeIndentStrategy {
private var indentCallback: (CodeStyleSettings) -> Indent = { Indent.getNoneIndent() }
private val within = ArrayList<IElementType>()
private var withinCallback: ((ASTNode) -> Boolean)? = null
private val notIn = ArrayList<IElementType>()
private val forElement = ArrayList<IElementType>()
private val notForElement = ArrayList<IElementType>()
private var forElementCallback: ((ASTNode) -> Boolean)? = null
@@ -72,6 +75,11 @@ abstract class NodeIndentStrategy {
return this
}
fun within(callback: (ASTNode) -> Boolean): PositionStrategy {
withinCallback = callback
return this
}
fun notWithin(parentType: IElementType, vararg orParentTypes: IElementType): PositionStrategy {
fillTypes(notIn, parentType, orParentTypes)
return this
@@ -129,6 +137,10 @@ abstract class NodeIndentStrategy {
if (notIn.contains(parent.elementType)) {
return null
}
if (withinCallback?.invoke(parent) == false) {
return null
}
}
else {
if (!within.isEmpty()) {
@@ -35,4 +35,5 @@ fun test() {
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -2,19 +2,19 @@ fun test() {
var a = 12
a =
12
12
a +=
12
12
a -=
12
12
a *=
12
12
a /=
12
12
a is
String
@@ -35,4 +35,5 @@ fun test() {
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -35,4 +35,5 @@ fun test() {
1..2
}
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_TRUE: ALIGN_MULTILINE_BINARY_OPERATION
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -0,0 +1,36 @@
var a: Any? = null
var b: Any? = null
var i = 0
var j = 0
fun test() {
a1 =
1
a =
b
a =
"Some"
i +=
j * j
i =
j ?: 0
i -=
j
i *=
0
i %=
5
i /=
2
}
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -0,0 +1,36 @@
var a: Any? = null
var b: Any? = null
var i = 0
var j = 0
fun test() {
a1 =
1
a =
b
a =
"Some"
i +=
j * j
i =
j ?: 0
i -=
j
i *=
0
i %=
5
i /=
2
}
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -0,0 +1,36 @@
var a: Any? = null
var b: Any? = null
var i = 0
var j = 0
fun test() {
a1 =
1
a =
b
a =
"Some"
i +=
j * j
i =
j ?: 0
i -=
j
i *=
0
i %=
5
i /=
2
}
// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES
@@ -171,6 +171,11 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
runTest("idea/testData/formatter/CommentInFunctionLiteral.after.kt");
}
@TestMetadata("ContinuationIndentInAssigments.after.kt")
public void testContinuationIndentInAssigments() throws Exception {
runTest("idea/testData/formatter/ContinuationIndentInAssigments.after.kt");
}
@TestMetadata("ContinuationIndentInParameterLists.after.kt")
public void testContinuationIndentInParameterLists() throws Exception {
runTest("idea/testData/formatter/ContinuationIndentInParameterLists.after.kt");
@@ -1136,6 +1141,11 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
runTest("idea/testData/formatter/ContinuationIndentForExpressionBodies.after.inv.kt");
}
@TestMetadata("ContinuationIndentInAssigments.after.inv.kt")
public void testContinuationIndentInAssigments() throws Exception {
runTest("idea/testData/formatter/ContinuationIndentInAssigments.after.inv.kt");
}
@TestMetadata("DelegationList.after.inv.kt")
public void testDelegationList() throws Exception {
runTest("idea/testData/formatter/DelegationList.after.inv.kt");