Option to apply normal indent to children of 'if' expressions
This commit is contained in:
+1
@@ -45,6 +45,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
|
||||
public boolean CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true;
|
||||
public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true;
|
||||
public boolean CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = true;
|
||||
public boolean CONTINUATION_INDENT_IN_IF_CONDITIONS = true;
|
||||
public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0;
|
||||
public int WRAP_EXPRESSION_BODY_FUNCTIONS = 0;
|
||||
public int WRAP_ELVIS_EXPRESSIONS = 1;
|
||||
|
||||
@@ -358,12 +358,32 @@ abstract class KotlinCommonBlock(
|
||||
val childrenAlignmentStrategy = getChildrenAlignmentStrategy()
|
||||
val wrappingStrategy = getWrappingStrategy()
|
||||
|
||||
return node.children()
|
||||
val childNodes = if (node.elementType == KtNodeTypes.BINARY_EXPRESSION) {
|
||||
val binaryExpressionChildren = mutableListOf<ASTNode>()
|
||||
collectBinaryExpressionChildren(node, binaryExpressionChildren)
|
||||
binaryExpressionChildren.asSequence()
|
||||
}
|
||||
else {
|
||||
node.children()
|
||||
}
|
||||
|
||||
return childNodes
|
||||
.filter { it.textRange.length > 0 && it.elementType != TokenType.WHITE_SPACE }
|
||||
.map { buildSubBlock(it, childrenAlignmentStrategy, wrappingStrategy ) }
|
||||
.toList()
|
||||
}
|
||||
|
||||
private fun collectBinaryExpressionChildren(node: ASTNode, result: MutableList<ASTNode>) {
|
||||
for (child in node.children()) {
|
||||
if (child.elementType == KtNodeTypes.BINARY_EXPRESSION) {
|
||||
collectBinaryExpressionChildren(child, result)
|
||||
}
|
||||
else {
|
||||
result.add(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getWrappingStrategy(): WrappingStrategy {
|
||||
val commonSettings = settings.kotlinCommonSettings
|
||||
val elementType = node.elementType
|
||||
@@ -574,6 +594,16 @@ private val INDENT_RULES = arrayOf<NodeIndentStrategy>(
|
||||
}
|
||||
.continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true),
|
||||
|
||||
strategy("If condition")
|
||||
.within(KtNodeTypes.CONDITION)
|
||||
.set { settings ->
|
||||
val indentType = if (settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_IF_CONDITIONS)
|
||||
Indent.Type.CONTINUATION
|
||||
else
|
||||
Indent.Type.NORMAL
|
||||
Indent.getIndent(indentType, false, true)
|
||||
},
|
||||
|
||||
strategy("Property accessor expression body")
|
||||
.within(KtNodeTypes.PROPERTY_ACCESSOR)
|
||||
.forElement {
|
||||
@@ -614,6 +644,7 @@ private val INDENT_RULES = arrayOf<NodeIndentStrategy>(
|
||||
|
||||
strategy("Binary expressions")
|
||||
.within(BINARY_EXPRESSIONS)
|
||||
.forElement { node -> !node.suppressBinaryExpressionIndent() }
|
||||
.set(Indent.getContinuationWithoutFirstIndent(false)),
|
||||
|
||||
strategy("Parenthesized expression")
|
||||
@@ -678,6 +709,18 @@ private fun hasErrorElementBefore(node: ASTNode): Boolean {
|
||||
return lastChild?.elementType == TokenType.ERROR_ELEMENT
|
||||
}
|
||||
|
||||
/**
|
||||
* Suppress indent for binary expressions when there is a block higher in the tree that forces
|
||||
* its indent to children ('if' condition or elvis).
|
||||
*/
|
||||
private fun ASTNode.suppressBinaryExpressionIndent(): Boolean {
|
||||
var psi = psi.parent as? KtBinaryExpression ?: return false
|
||||
while (psi.parent is KtBinaryExpression) {
|
||||
psi = psi.parent as KtBinaryExpression
|
||||
}
|
||||
return psi.parent?.node?.elementType == KtNodeTypes.CONDITION || psi.operationToken == KtTokens.ELVIS
|
||||
}
|
||||
|
||||
private fun getAlignmentForChildInParenthesis(
|
||||
shouldAlignChild: Boolean, parameter: IElementType, delimiter: IElementType,
|
||||
shouldAlignParenthesis: Boolean, openBracket: IElementType, closeBracket: IElementType): CommonAlignmentStrategy {
|
||||
|
||||
Reference in New Issue
Block a user