Code refactoring

This commit is contained in:
Valentin Kipyatkov
2015-05-11 16:21:38 +03:00
parent a3360df424
commit 4c275cb4ea
5 changed files with 44 additions and 92 deletions
@@ -17,118 +17,47 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.copied
public object BranchedUnfoldingUtils {
private fun getOutermostLastBlockElement(expression: JetExpression?): JetExpression {
public fun getOutermostLastBlockElement(expression: JetExpression?): JetExpression {
return JetPsiUtil.getOutermostLastBlockElement(expression, JetPsiUtil.ANY_JET_ELEMENT) as JetExpression
}
public val UNFOLD_WITHOUT_CHECK: String = "Expression must be checked before unfolding"
private fun assertNotNull(value: Any?) {
assert(value != null) { UNFOLD_WITHOUT_CHECK }
}
public fun unfoldAssignmentToIf(assignment: JetBinaryExpression, editor: Editor) {
val op = assignment.getOperationReference().getText()
val lhs = assignment.getLeft()
val left = assignment.getLeft()!!
val ifExpression = assignment.getRight() as JetIfExpression
assertNotNull(ifExpression)
//noinspection ConstantConditions
val newIfExpression = ifExpression.copy() as JetIfExpression
val newIfExpression = ifExpression.copied()
val thenExpr = getOutermostLastBlockElement(newIfExpression.getThen())
val elseExpr = getOutermostLastBlockElement(newIfExpression.getElse())
assertNotNull(thenExpr)
assertNotNull(elseExpr)
//noinspection ConstantConditions
val psiFactory = JetPsiFactory(assignment)
thenExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", lhs, op, thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", lhs, op, elseExpr))
thenExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", left, op, thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("$0 $1 $2", left, op, elseExpr))
val resultElement = assignment.replace(newIfExpression)
val resultIf = assignment.replace(newIfExpression)
editor.getCaretModel().moveToOffset(resultElement.getTextOffset())
editor.getCaretModel().moveToOffset(resultIf.getTextOffset())
}
public fun unfoldAssignmentToWhen(assignment: JetBinaryExpression, editor: Editor) {
val op = assignment.getOperationReference().getText()
val lhs = assignment.getLeft()
val left = assignment.getLeft()!!
val whenExpression = assignment.getRight() as JetWhenExpression
assertNotNull(whenExpression)
//noinspection ConstantConditions
val newWhenExpression = whenExpression.copy() as JetWhenExpression
val newWhenExpression = whenExpression.copied()
for (entry in newWhenExpression.getEntries()) {
val currExpr = getOutermostLastBlockElement(entry.getExpression())
assertNotNull(currExpr)
//noinspection ConstantConditions
currExpr.replace(JetPsiFactory(assignment).createExpressionByPattern("$0 $1 $2", lhs, op, currExpr))
val expr = getOutermostLastBlockElement(entry.getExpression())
expr.replace(JetPsiFactory(assignment).createExpressionByPattern("$0 $1 $2", left, op, expr))
}
val resultElement = assignment.replace(newWhenExpression)
val resultWhen = assignment.replace(newWhenExpression)
editor.getCaretModel().moveToOffset(resultElement.getTextOffset())
}
public fun unfoldPropertyToIf(property: JetProperty, editor: Editor) {
val assignment = DeclarationUtils.splitPropertyDeclaration(property)
unfoldAssignmentToIf(assignment, editor)
}
public fun unfoldPropertyToWhen(property: JetProperty, editor: Editor) {
val assignment = DeclarationUtils.splitPropertyDeclaration(property)
unfoldAssignmentToWhen(assignment, editor)
}
public fun unfoldReturnToIf(returnExpression: JetReturnExpression) {
val ifExpression = returnExpression.getReturnedExpression() as JetIfExpression
assertNotNull(ifExpression)
//noinspection ConstantConditions
val newIfExpression = ifExpression.copy() as JetIfExpression
val thenExpr = getOutermostLastBlockElement(newIfExpression.getThen())
val elseExpr = getOutermostLastBlockElement(newIfExpression.getElse())
assertNotNull(thenExpr)
assertNotNull(elseExpr)
val psiFactory = JetPsiFactory(returnExpression)
thenExpr.replace(psiFactory.createExpressionByPattern("return $0", thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("return $0", elseExpr))
returnExpression.replace(newIfExpression)
}
public fun unfoldReturnToWhen(returnExpression: JetReturnExpression) {
val whenExpression = returnExpression.getReturnedExpression() as JetWhenExpression
assertNotNull(whenExpression)
//noinspection ConstantConditions
val newWhenExpression = whenExpression.copy() as JetWhenExpression
for (entry in newWhenExpression.getEntries()) {
val currExpr = getOutermostLastBlockElement(entry.getExpression())
assertNotNull(currExpr)
currExpr.replace(JetPsiFactory(returnExpression).createExpressionByPattern("return $0", currExpr))
}
returnExpression.replace(newWhenExpression)
editor.getCaretModel().moveToOffset(resultWhen.getTextOffset())
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetProperty
@@ -29,6 +30,7 @@ public class UnfoldPropertyToIfIntention : JetSelfTargetingOffsetIndependentInte
}
override fun applyTo(element: JetProperty, editor: Editor) {
BranchedUnfoldingUtils.unfoldPropertyToIf(element, editor)
val assignment = DeclarationUtils.splitPropertyDeclaration(element)
BranchedUnfoldingUtils.unfoldAssignmentToIf(assignment, editor)
}
}
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.idea.intentions.declarations.DeclarationUtils
import org.jetbrains.kotlin.psi.JetProperty
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetWhenExpression
@@ -31,6 +32,7 @@ public class UnfoldPropertyToWhenIntention : JetSelfTargetingOffsetIndependentIn
}
override fun applyTo(element: JetProperty, editor: Editor) {
BranchedUnfoldingUtils.unfoldPropertyToWhen(element, editor)
val assignment = DeclarationUtils.splitPropertyDeclaration(element)
BranchedUnfoldingUtils.unfoldAssignmentToWhen(assignment, editor)
}
}
@@ -20,7 +20,10 @@ import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.JetReturnExpression
import org.jetbrains.kotlin.psi.createExpressionByPattern
import org.jetbrains.kotlin.psi.psiUtil.copied
public class UnfoldReturnToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetReturnExpression>(javaClass(), "Replace return with 'if' expression") {
override fun isApplicableTo(element: JetReturnExpression): Boolean {
@@ -28,6 +31,15 @@ public class UnfoldReturnToIfIntention : JetSelfTargetingOffsetIndependentIntent
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldReturnToIf(element)
val ifExpression = element.getReturnedExpression() as JetIfExpression
val newIfExpression = ifExpression.copied()
val thenExpr = BranchedUnfoldingUtils.getOutermostLastBlockElement(newIfExpression.getThen())
val elseExpr = BranchedUnfoldingUtils.getOutermostLastBlockElement(newIfExpression.getElse())
val psiFactory = JetPsiFactory(element)
thenExpr.replace(psiFactory.createExpressionByPattern("return $0", thenExpr))
elseExpr.replace(psiFactory.createExpressionByPattern("return $0", elseExpr))
element.replace(newIfExpression)
}
}
@@ -19,9 +19,8 @@ package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetReturnExpression
import org.jetbrains.kotlin.psi.JetWhenExpression
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.copied
public class UnfoldReturnToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetReturnExpression>(javaClass(), "Replace return with 'when' expression") {
override fun isApplicableTo(element: JetReturnExpression): Boolean {
@@ -30,6 +29,14 @@ public class UnfoldReturnToWhenIntention : JetSelfTargetingOffsetIndependentInte
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
BranchedUnfoldingUtils.unfoldReturnToWhen(element)
val whenExpression = element.getReturnedExpression() as JetWhenExpression
val newWhenExpression = whenExpression.copied()
for (entry in newWhenExpression.getEntries()) {
val currExpr = BranchedUnfoldingUtils.getOutermostLastBlockElement(entry.getExpression())
currExpr.replace(JetPsiFactory(element).createExpressionByPattern("return $0", currExpr))
}
element.replace(newWhenExpression)
}
}