Minor code refactorings
This commit is contained in:
+6
-9
@@ -17,22 +17,19 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
||||||
|
|
||||||
import com.google.common.base.Predicate
|
import com.google.common.base.Predicate
|
||||||
import com.intellij.psi.PsiElement
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
import java.util.ArrayList
|
|
||||||
|
|
||||||
object BranchedFoldingUtils {
|
object BranchedFoldingUtils {
|
||||||
private val CHECK_ASSIGNMENT = object : Predicate<JetElement> {
|
private val CHECK_ASSIGNMENT = object : Predicate<JetElement> {
|
||||||
override fun apply(input: JetElement): Boolean {
|
override fun apply(input: JetElement): Boolean {
|
||||||
if (!JetPsiUtil.isAssignment(input)) return false
|
if (input !is JetBinaryExpression) return false
|
||||||
|
if (input.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false
|
||||||
|
|
||||||
val assignment = input as JetBinaryExpression
|
val left = input.getLeft() as? JetSimpleNameExpression ?: return false
|
||||||
|
if (input.getRight() == null) return false
|
||||||
|
|
||||||
val left = assignment.getLeft() as? JetSimpleNameExpression ?: return false
|
val parent = input.getParent()
|
||||||
if (assignment.getRight() == null) return false
|
|
||||||
|
|
||||||
val parent = assignment.getParent()
|
|
||||||
if (parent is JetBlockExpression) {
|
if (parent is JetBlockExpression) {
|
||||||
return !JetPsiUtil.checkVariableDeclarationInBlock(parent, left.getText())
|
return !JetPsiUtil.checkVariableDeclarationInBlock(parent, left.getText())
|
||||||
}
|
}
|
||||||
|
|||||||
+24
-22
@@ -17,8 +17,8 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import com.intellij.psi.PsiElement
|
|
||||||
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
|
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
|
||||||
|
import org.jetbrains.kotlin.lexer.JetTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
|
||||||
public object BranchedUnfoldingUtils {
|
public object BranchedUnfoldingUtils {
|
||||||
@@ -30,33 +30,35 @@ public object BranchedUnfoldingUtils {
|
|||||||
public fun getUnfoldableExpressionKind(root: JetExpression?): UnfoldableKind? {
|
public fun getUnfoldableExpressionKind(root: JetExpression?): UnfoldableKind? {
|
||||||
if (root == null) return null
|
if (root == null) return null
|
||||||
|
|
||||||
if (JetPsiUtil.isAssignment(root)) {
|
when (root) {
|
||||||
val assignment = root as JetBinaryExpression
|
is JetBinaryExpression -> {
|
||||||
|
if (root.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return null
|
||||||
|
if (root.getLeft() == null) return null
|
||||||
|
|
||||||
if (assignment.getLeft() == null) return null
|
val rhs = root.getRight()
|
||||||
|
if (rhs is JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF
|
||||||
val rhs = assignment.getRight()
|
if (rhs is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(rhs)) {
|
||||||
if (rhs is JetIfExpression) return UnfoldableKind.ASSIGNMENT_TO_IF
|
return UnfoldableKind.ASSIGNMENT_TO_WHEN
|
||||||
if (rhs is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(rhs)) {
|
}
|
||||||
return UnfoldableKind.ASSIGNMENT_TO_WHEN
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (root is JetReturnExpression) {
|
|
||||||
val resultExpr = root.getReturnedExpression()
|
|
||||||
|
|
||||||
if (resultExpr is JetIfExpression) return UnfoldableKind.RETURN_TO_IF
|
is JetReturnExpression -> {
|
||||||
if (resultExpr is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(resultExpr)) {
|
val resultExpr = root.getReturnedExpression()
|
||||||
return UnfoldableKind.RETURN_TO_WHEN
|
if (resultExpr is JetIfExpression) return UnfoldableKind.RETURN_TO_IF
|
||||||
|
if (resultExpr is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(resultExpr)) {
|
||||||
|
return UnfoldableKind.RETURN_TO_WHEN
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (root is JetProperty) {
|
|
||||||
if (!root.isLocal()) return null
|
|
||||||
|
|
||||||
val initializer = root.getInitializer()
|
is JetProperty -> {
|
||||||
|
if (!root.isLocal()) return null
|
||||||
|
|
||||||
if (initializer is JetIfExpression) return UnfoldableKind.PROPERTY_TO_IF
|
val initializer = root.getInitializer()
|
||||||
if (initializer is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)) {
|
|
||||||
return UnfoldableKind.PROPERTY_TO_WHEN
|
if (initializer is JetIfExpression) return UnfoldableKind.PROPERTY_TO_IF
|
||||||
|
if (initializer is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)) {
|
||||||
|
return UnfoldableKind.PROPERTY_TO_WHEN
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user