From 0098588a6bbd04207aa7e849d9819dd3bc8b89fd Mon Sep 17 00:00:00 2001 From: Dmitry Gridin Date: Thu, 31 Oct 2019 18:50:05 +0700 Subject: [PATCH] NodeIndentStrategy: remove code duplication --- .../idea/formatter/NodeIndentStrategy.kt | 50 +++++++------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/NodeIndentStrategy.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/NodeIndentStrategy.kt index 02edee60448..c9b8ef344c2 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/NodeIndentStrategy.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/NodeIndentStrategy.kt @@ -21,7 +21,7 @@ import com.intellij.lang.ASTNode import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.TokenSet -import java.util.* +import kotlin.collections.ArrayList abstract class NodeIndentStrategy { @@ -113,40 +113,12 @@ abstract class NodeIndentStrategy { } override fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent? { - if (!forElement.isEmpty()) { - if (!forElement.contains(node.elementType)) { - return null - } - } - if (notForElement.contains(node.elementType)) { - return null - } - - if (forElementCallback?.invoke(node) == false) { - return null - } + if (!isValidIndent(forElement, notForElement, node, forElementCallback)) return null val parent = node.treeParent if (parent != null) { - if (!within.isEmpty()) { - if (!within.contains(parent.elementType)) { - return null - } - } - - if (notIn.contains(parent.elementType)) { - return null - } - - if (withinCallback?.invoke(parent) == false) { - return null - } - } - else { - if (!within.isEmpty()) { - return null - } - } + if (!isValidIndent(within, notIn, parent, withinCallback)) return null + } else if (within.isNotEmpty()) return null return indentCallback(settings) } @@ -154,7 +126,7 @@ abstract class NodeIndentStrategy { private fun fillTypes(resultCollection: MutableList, singleType: IElementType, otherTypes: Array) { resultCollection.clear() resultCollection.add(singleType) - Collections.addAll(resultCollection, *otherTypes) + resultCollection.addAll(otherTypes) } } @@ -168,3 +140,15 @@ abstract class NodeIndentStrategy { } } } + +private fun isValidIndent( + elements: ArrayList, + excludeElements: ArrayList, + node: ASTNode, + callback: ((ASTNode) -> Boolean)? +): Boolean { + if (elements.isNotEmpty() && !elements.contains(node.elementType)) return false + if (excludeElements.contains(node.elementType)) return false + if (callback?.invoke(node) == false) return false + return true +} \ No newline at end of file