Reformat MergeWhenIntention

This commit is contained in:
Toshiaki Kameyama
2018-04-23 06:09:22 +03:00
committed by Mikhail Glukhikh
parent 4ee9db7a6c
commit cdbd7a7c5a
@@ -29,7 +29,11 @@ import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.* import org.jetbrains.kotlin.psi.psiUtil.*
class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenExpression::class.java, "Merge with next 'when'", "Merge 'when' expressions") { class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(
KtWhenExpression::class.java,
"Merge with next 'when'",
"Merge 'when' expressions"
) {
override fun applicabilityRange(element: KtWhenExpression): TextRange? { override fun applicabilityRange(element: KtWhenExpression): TextRange? {
val next = PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace::class.java) as? KtWhenExpression ?: return null val next = PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace::class.java) as? KtWhenExpression ?: return null
@@ -42,14 +46,15 @@ class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenE
val entries2 = next.entries val entries2 = next.entries
if (entries1.size != entries2.size) return null if (entries1.size != entries2.size) return null
if (!entries1.zip(entries2).all { pair -> if (!entries1.zip(entries2).all { pair ->
conditionsMatch(pair.first, pair.second) && checkBodies(pair.first, pair.second) conditionsMatch(pair.first, pair.second) && checkBodies(pair.first, pair.second)
}) return null }
) return null
return element.whenKeyword.textRange return element.whenKeyword.textRange
} }
private fun conditionsMatch(e1: KtWhenEntry, e2: KtWhenEntry): Boolean = private fun conditionsMatch(e1: KtWhenEntry, e2: KtWhenEntry): Boolean =
e1.conditions.toList().toRange().matches(e2.conditions.toList().toRange()) e1.conditions.toList().toRange().matches(e2.conditions.toList().toRange())
private fun checkBodies(e1: KtWhenEntry, e2: KtWhenEntry): Boolean { private fun checkBodies(e1: KtWhenEntry, e2: KtWhenEntry): Boolean {
val names1 = e1.declarationNames() val names1 = e1.declarationNames()
@@ -63,10 +68,10 @@ class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenE
} }
private fun KtWhenEntry.declarationNames(): Set<String> = private fun KtWhenEntry.declarationNames(): Set<String> =
expression?.blockExpressionsOrSingle() expression?.blockExpressionsOrSingle()
?.filter { it is KtNamedDeclaration } ?.filter { it is KtNamedDeclaration }
?.mapNotNull { it.name } ?.mapNotNull { it.name }
?.toSet() ?: emptySet() ?.toSet() ?: emptySet()
override fun applyTo(element: KtWhenExpression, editor: Editor?) { override fun applyTo(element: KtWhenExpression, editor: Editor?) {
val nextWhen = PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace::class.java) as KtWhenExpression val nextWhen = PsiTreeUtil.skipSiblingsForward(element, PsiWhiteSpace::class.java) as KtWhenExpression
@@ -84,11 +89,7 @@ class MergeWhenIntention : SelfTargetingRangeIntention<KtWhenExpression>(KtWhenE
else -> { else -> {
val psiFactory = KtPsiFactory(this) val psiFactory = KtPsiFactory(this)
val block = if (this is KtBlockExpression) val block = this as? KtBlockExpression ?: replaced(psiFactory.createSingleStatementBlock(this))
this
else
replaced(psiFactory.createSingleStatementBlock(this))
for (element in that.blockExpressionsOrSingle()) { for (element in that.blockExpressionsOrSingle()) {
val expression = block.appendElement(element) val expression = block.appendElement(element)
block.addBefore(psiFactory.createNewLine(), expression) block.addBefore(psiFactory.createNewLine(), expression)