NodeIndentStrategy: remove code duplication

This commit is contained in:
Dmitry Gridin
2019-10-31 18:50:05 +07:00
parent 196dd25637
commit 0098588a6b
@@ -21,7 +21,7 @@ import com.intellij.lang.ASTNode
import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.codeStyle.CodeStyleSettings
import com.intellij.psi.tree.IElementType import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet import com.intellij.psi.tree.TokenSet
import java.util.* import kotlin.collections.ArrayList
abstract class NodeIndentStrategy { abstract class NodeIndentStrategy {
@@ -113,40 +113,12 @@ abstract class NodeIndentStrategy {
} }
override fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent? { override fun getIndent(node: ASTNode, settings: CodeStyleSettings): Indent? {
if (!forElement.isEmpty()) { if (!isValidIndent(forElement, notForElement, node, forElementCallback)) return null
if (!forElement.contains(node.elementType)) {
return null
}
}
if (notForElement.contains(node.elementType)) {
return null
}
if (forElementCallback?.invoke(node) == false) {
return null
}
val parent = node.treeParent val parent = node.treeParent
if (parent != null) { if (parent != null) {
if (!within.isEmpty()) { if (!isValidIndent(within, notIn, parent, withinCallback)) return null
if (!within.contains(parent.elementType)) { } else if (within.isNotEmpty()) return null
return null
}
}
if (notIn.contains(parent.elementType)) {
return null
}
if (withinCallback?.invoke(parent) == false) {
return null
}
}
else {
if (!within.isEmpty()) {
return null
}
}
return indentCallback(settings) return indentCallback(settings)
} }
@@ -154,7 +126,7 @@ abstract class NodeIndentStrategy {
private fun fillTypes(resultCollection: MutableList<IElementType>, singleType: IElementType, otherTypes: Array<out IElementType>) { private fun fillTypes(resultCollection: MutableList<IElementType>, singleType: IElementType, otherTypes: Array<out IElementType>) {
resultCollection.clear() resultCollection.clear()
resultCollection.add(singleType) resultCollection.add(singleType)
Collections.addAll(resultCollection, *otherTypes) resultCollection.addAll(otherTypes)
} }
} }
@@ -168,3 +140,15 @@ abstract class NodeIndentStrategy {
} }
} }
} }
private fun isValidIndent(
elements: ArrayList<IElementType>,
excludeElements: ArrayList<IElementType>,
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
}