EliminateWhenSubjectIntention - smaller range and code refactoring
This commit is contained in:
@@ -258,8 +258,6 @@ merge.when=Merge 'when' expressions
|
|||||||
merge.when.family=Merge 'when' Expression
|
merge.when.family=Merge 'when' Expression
|
||||||
introduce.when.subject=Introduce argument to 'when'
|
introduce.when.subject=Introduce argument to 'when'
|
||||||
introduce.when.subject.family=Introduce Argument to 'when'
|
introduce.when.subject.family=Introduce Argument to 'when'
|
||||||
eliminate.when.subject=Eliminate argument of 'when'
|
|
||||||
eliminate.when.subject.family=Eliminate Argument of 'when'
|
|
||||||
transform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
|
transform.if.statement.with.assignments.to.expression=Transform 'if' statement with assignments to expression
|
||||||
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
transform.assignment.with.if.expression.to.statement=Transform assignment with 'if' expression to statement
|
||||||
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
transform.if.statement.with.assignments.to.expression.family=Transform 'if' Statement with Assignments to Expression
|
||||||
|
|||||||
-25
@@ -125,10 +125,6 @@ public fun JetWhenExpression.canIntroduceSubject(): Boolean {
|
|||||||
return getSubjectCandidate() != null
|
return getSubjectCandidate() != null
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun JetWhenExpression.canEliminateSubject(): Boolean {
|
|
||||||
return getSubjectExpression() is JetSimpleNameExpression
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun JetWhenExpression.flatten(): JetWhenExpression {
|
public fun JetWhenExpression.flatten(): JetWhenExpression {
|
||||||
val subjectExpression = getSubjectExpression()
|
val subjectExpression = getSubjectExpression()
|
||||||
val elseBranch = getElseExpression()
|
val elseBranch = getElseExpression()
|
||||||
@@ -193,27 +189,6 @@ public fun JetWhenExpression.introduceSubject(): JetWhenExpression {
|
|||||||
return replaced(builder.toExpression())
|
return replaced(builder.toExpression())
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun JetWhenExpression.eliminateSubject(): JetWhenExpression {
|
|
||||||
val subject = getSubjectExpression()!!
|
|
||||||
|
|
||||||
val builder = JetPsiFactory(this).WhenBuilder()
|
|
||||||
for (entry in getEntries()) {
|
|
||||||
val branchExpression = entry.getExpression()
|
|
||||||
|
|
||||||
if (entry.isElse()) {
|
|
||||||
builder.elseEntry(branchExpression)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for (condition in entry.getConditions()) {
|
|
||||||
builder.condition(condition.toExpressionText(subject))
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.branchExpression(branchExpression)
|
|
||||||
}
|
|
||||||
|
|
||||||
return replaced(builder.toExpression())
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun JetIfExpression.canTransformToWhen(): Boolean = getThen() != null
|
public fun JetIfExpression.canTransformToWhen(): Boolean = getThen() != null
|
||||||
|
|
||||||
public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty()
|
public fun JetWhenExpression.canTransformToIf(): Boolean = !getEntries().isEmpty()
|
||||||
|
|||||||
+28
-6
@@ -17,15 +17,37 @@
|
|||||||
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
|
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
|
||||||
|
|
||||||
import com.intellij.openapi.editor.Editor
|
import com.intellij.openapi.editor.Editor
|
||||||
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
|
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.canEliminateSubject
|
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.toExpressionText
|
||||||
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.eliminateSubject
|
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||||
|
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
|
||||||
import org.jetbrains.kotlin.psi.JetWhenExpression
|
import org.jetbrains.kotlin.psi.JetWhenExpression
|
||||||
|
|
||||||
public class EliminateWhenSubjectIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>("eliminate.when.subject", javaClass()) {
|
public class EliminateWhenSubjectIntention : JetSelfTargetingIntention<JetWhenExpression>(javaClass(), "Eliminate argument of 'when'") {
|
||||||
override fun isApplicableTo(element: JetWhenExpression): Boolean = element.canEliminateSubject()
|
override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean {
|
||||||
|
if (element.getSubjectExpression() !is JetSimpleNameExpression) return false
|
||||||
|
val lBrace = element.getOpenBrace() ?: return false
|
||||||
|
return caretOffset <= lBrace.getTextRange().getStartOffset()
|
||||||
|
}
|
||||||
|
|
||||||
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
||||||
element.eliminateSubject()
|
val subject = element.getSubjectExpression()!!
|
||||||
|
|
||||||
|
val builder = JetPsiFactory(element).WhenBuilder()
|
||||||
|
for (entry in element.getEntries()) {
|
||||||
|
val branchExpression = entry.getExpression()
|
||||||
|
|
||||||
|
if (entry.isElse()) {
|
||||||
|
builder.elseEntry(branchExpression)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for (condition in entry.getConditions()) {
|
||||||
|
builder.condition(condition.toExpressionText(subject))
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.branchExpression(branchExpression)
|
||||||
|
}
|
||||||
|
|
||||||
|
element.replace(builder.toExpression())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user