Reformat: BranchedFoldingUtils
This commit is contained in:
+28
-31
@@ -46,14 +46,14 @@ object BranchedFoldingUtils {
|
||||
}
|
||||
|
||||
fun getFoldableBranchedReturn(branch: KtExpression?): KtReturnExpression? =
|
||||
(branch?.lastBlockStatementOrThis() as? KtReturnExpression)?.takeIf {
|
||||
it.returnedExpression != null &&
|
||||
it.returnedExpression !is KtLambdaExpression &&
|
||||
it.getTargetLabel() == null
|
||||
}
|
||||
(branch?.lastBlockStatementOrThis() as? KtReturnExpression)?.takeIf {
|
||||
it.returnedExpression != null &&
|
||||
it.returnedExpression !is KtLambdaExpression &&
|
||||
it.getTargetLabel() == null
|
||||
}
|
||||
|
||||
private fun checkAssignmentsMatch(a1: KtBinaryExpression, a2: KtBinaryExpression): Boolean =
|
||||
a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken
|
||||
a1.left?.text == a2.left?.text && a1.operationToken == a2.operationToken
|
||||
|
||||
internal fun getFoldableAssignmentNumber(expression: KtExpression?): Int {
|
||||
expression ?: return -1
|
||||
@@ -61,18 +61,15 @@ object BranchedFoldingUtils {
|
||||
fun collectAssignmentsAndCheck(e: KtExpression?): Boolean = when (e) {
|
||||
is KtWhenExpression -> {
|
||||
val entries = e.entries
|
||||
!e.hasMissingCases() &&
|
||||
entries.isNotEmpty() &&
|
||||
entries.all { entry ->
|
||||
!e.hasMissingCases() && entries.isNotEmpty() && entries.all { entry ->
|
||||
val assignment = getFoldableBranchedAssignment(entry.expression)?.run { assignments.add(this) }
|
||||
assignment != null || collectAssignmentsAndCheck(entry.expression?.lastBlockStatementOrThis())
|
||||
}
|
||||
}
|
||||
is KtIfExpression -> {
|
||||
val branches = e.branches
|
||||
branches.size > 1 &&
|
||||
(branches.lastOrNull()?.getStrictParentOfType<KtIfExpression>()?.`else` != null) &&
|
||||
branches.all { branch ->
|
||||
val elseBranch = branches.lastOrNull()?.getStrictParentOfType<KtIfExpression>()?.`else`
|
||||
branches.size > 1 && elseBranch != null && branches.all { branch ->
|
||||
val assignment = getFoldableBranchedAssignment(branch)?.run { assignments.add(this) }
|
||||
assignment != null || collectAssignmentsAndCheck(branch?.lastBlockStatementOrThis())
|
||||
}
|
||||
@@ -105,25 +102,25 @@ object BranchedFoldingUtils {
|
||||
else
|
||||
false
|
||||
}
|
||||
)) {
|
||||
)
|
||||
) {
|
||||
return -1
|
||||
}
|
||||
return assignments.size
|
||||
}
|
||||
|
||||
private fun getFoldableReturns(branches: List<KtExpression?>): List<KtReturnExpression>? =
|
||||
branches.fold<KtExpression?, MutableList<KtReturnExpression>?>(mutableListOf()) { prevList, branch ->
|
||||
if (prevList == null) return@fold null
|
||||
val foldableBranchedReturn = getFoldableBranchedReturn(branch)
|
||||
if (foldableBranchedReturn != null) {
|
||||
prevList.add(foldableBranchedReturn)
|
||||
}
|
||||
else {
|
||||
val currReturns = getFoldableReturns(branch?.lastBlockStatementOrThis()) ?: return@fold null
|
||||
prevList += currReturns
|
||||
}
|
||||
prevList
|
||||
branches.fold<KtExpression?, MutableList<KtReturnExpression>?>(mutableListOf()) { prevList, branch ->
|
||||
if (prevList == null) return@fold null
|
||||
val foldableBranchedReturn = getFoldableBranchedReturn(branch)
|
||||
if (foldableBranchedReturn != null) {
|
||||
prevList.add(foldableBranchedReturn)
|
||||
} else {
|
||||
val currReturns = getFoldableReturns(branch?.lastBlockStatementOrThis()) ?: return@fold null
|
||||
prevList += currReturns
|
||||
}
|
||||
prevList
|
||||
}
|
||||
|
||||
internal fun getFoldableReturns(expression: KtExpression?): List<KtReturnExpression>? = when (expression) {
|
||||
is KtWhenExpression -> {
|
||||
@@ -175,6 +172,7 @@ object BranchedFoldingUtils {
|
||||
replace(rhs)
|
||||
}
|
||||
}
|
||||
|
||||
fun lift(e: KtExpression?) {
|
||||
when (e) {
|
||||
is KtWhenExpression -> e.entries.forEach { entry ->
|
||||
@@ -196,19 +194,18 @@ object BranchedFoldingUtils {
|
||||
fun KtReturnExpression.replaceWithReturned() {
|
||||
replace(returnedExpression!!)
|
||||
}
|
||||
|
||||
fun lift(e: KtExpression?) {
|
||||
when (e) {
|
||||
is KtWhenExpression -> e.entries.forEach { entry ->
|
||||
getFoldableBranchedReturn(entry.expression)?.replaceWithReturned()
|
||||
?: lift(entry.expression?.lastBlockStatementOrThis())
|
||||
val entryExpr = entry.expression
|
||||
getFoldableBranchedReturn(entryExpr)?.replaceWithReturned() ?: lift(entryExpr?.lastBlockStatementOrThis())
|
||||
}
|
||||
is KtIfExpression -> e.branches.forEach { branch ->
|
||||
getFoldableBranchedReturn(branch)?.replaceWithReturned() ?:
|
||||
lift(branch?.lastBlockStatementOrThis())
|
||||
getFoldableBranchedReturn(branch)?.replaceWithReturned() ?: lift(branch?.lastBlockStatementOrThis())
|
||||
}
|
||||
is KtTryExpression -> e.tryBlockAndCatchBodies().forEach {
|
||||
getFoldableBranchedReturn(it)?.replaceWithReturned() ?:
|
||||
lift(it?.lastBlockStatementOrThis())
|
||||
getFoldableBranchedReturn(it)?.replaceWithReturned() ?: lift(it?.lastBlockStatementOrThis())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -219,6 +216,6 @@ object BranchedFoldingUtils {
|
||||
private fun KtTryExpression.tryBlockAndCatchBodies(): List<KtExpression?> = listOf(tryBlock) + catchClauses.map { it.catchBody }
|
||||
|
||||
private fun KtWhenExpression.hasMissingCases(): Boolean =
|
||||
!KtPsiUtil.checkWhenExpressionHasSingleElse(this) && WhenChecker.getMissingCases(this, this.analyze()).isNotEmpty()
|
||||
!KtPsiUtil.checkWhenExpressionHasSingleElse(this) && WhenChecker.getMissingCases(this, this.analyze()).isNotEmpty()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user