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()) {