Cleanup: make single KtExpression.isElseIf

This commit is contained in:
Mikhail Glukhikh
2017-07-07 17:23:39 +03:00
parent a6f0b7c7a4
commit 41893736df
4 changed files with 6 additions and 13 deletions
@@ -25,6 +25,7 @@ import com.intellij.usageView.UsageViewShortNameLocation
import com.intellij.xml.breadcrumbs.BreadcrumbsInfoProvider import com.intellij.xml.breadcrumbs.BreadcrumbsInfoProvider
import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.idea.KotlinLanguage import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isElseIf
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.unwrapIfLabeled import org.jetbrains.kotlin.idea.intentions.loopToCallChain.unwrapIfLabeled
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression import org.jetbrains.kotlin.psi.psiUtil.getCallNameExpression
@@ -469,8 +470,6 @@ class KotlinBreadcrumbsInfoProvider : BreadcrumbsInfoProvider() {
val ellipsis = "${Typography.ellipsis}" val ellipsis = "${Typography.ellipsis}"
fun KtIfExpression.isElseIf() = parent.node.elementType == KtNodeTypes.ELSE
fun KtContainerNode.bodyOwner(): KtExpression? { fun KtContainerNode.bodyOwner(): KtExpression? {
return if (node.elementType == KtNodeTypes.BODY) parent as KtExpression else null return if (node.elementType == KtNodeTypes.BODY) parent as KtExpression else null
} }
@@ -22,7 +22,7 @@ import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder import com.intellij.codeInspection.ProblemsHolder
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfToWhenIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfToWhenIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isIfBranch import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isElseIf
import org.jetbrains.kotlin.idea.intentions.branches import org.jetbrains.kotlin.idea.intentions.branches
import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
@@ -44,7 +44,7 @@ class CascadeIfInspection : AbstractKotlinInspection() {
it.lastBlockStatementOrThis() is KtIfExpression it.lastBlockStatementOrThis() is KtIfExpression
}) return }) return
if (expression.isIfBranch()) return if (expression.isElseIf()) return
if (expression.anyDescendantOfType<KtExpressionWithLabel> { if (expression.anyDescendantOfType<KtExpressionWithLabel> {
it is KtBreakExpression || it is KtContinueExpression it is KtBreakExpression || it is KtContinueExpression
@@ -20,7 +20,7 @@ import com.intellij.codeInspection.*
import com.intellij.openapi.project.Project import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isIfBranch import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isElseIf
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
@@ -32,7 +32,7 @@ class LiftReturnOrAssignmentInspection : AbstractKotlinInspection() {
object : KtVisitorVoid() { object : KtVisitorVoid() {
private fun visitIfOrWhen(expression: KtExpression, keyword: PsiElement) { private fun visitIfOrWhen(expression: KtExpression, keyword: PsiElement) {
if (expression.lineCount() > LINES_LIMIT) return if (expression.lineCount() > LINES_LIMIT) return
if (expression.isIfBranch()) return if (expression.isElseIf()) return
val foldableReturns = BranchedFoldingUtils.getFoldableReturns(expression) val foldableReturns = BranchedFoldingUtils.getFoldableReturns(expression)
if (foldableReturns?.isNotEmpty() == true) { if (foldableReturns?.isNotEmpty() == true) {
@@ -262,10 +262,4 @@ internal fun KtExpression.lineCount(): Int {
internal fun KtExpression.isOneLiner(): Boolean = lineCount() == 1 internal fun KtExpression.isOneLiner(): Boolean = lineCount() == 1
internal fun KtExpression.isIfBranch(): Boolean { internal fun KtExpression.isElseIf() = parent.node.elementType == KtNodeTypes.ELSE
if (parent is KtContainerNodeForControlStructureBody) {
val grandParent = parent.parent
if (grandParent is KtIfExpression && grandParent.`else` == this) return true
}
return false
}