Smaller ranges for intentions

This commit is contained in:
Valentin Kipyatkov
2015-05-11 16:48:39 +03:00
parent 4c275cb4ea
commit ea168b0f6b
12 changed files with 95 additions and 73 deletions
@@ -17,15 +17,19 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
public class FoldIfToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with assignment") {
override fun isApplicableTo(element: JetIfExpression): Boolean {
val thenAssignment = BranchedFoldingUtils.getFoldableBranchedAssignment(element.getThen()) ?: return false
val elseAssignment = BranchedFoldingUtils.getFoldableBranchedAssignment(element.getElse()) ?: return false
return BranchedFoldingUtils.checkAssignmentsMatch(thenAssignment, elseAssignment)
public class FoldIfToAssignmentIntention : JetSelfTargetingRangeIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with assignment") {
override fun applicabilityRange(element: JetIfExpression): TextRange? {
val thenAssignment = BranchedFoldingUtils.getFoldableBranchedAssignment(element.getThen()) ?: return null
val elseAssignment = BranchedFoldingUtils.getFoldableBranchedAssignment(element.getElse()) ?: return null
if (!BranchedFoldingUtils.checkAssignmentsMatch(thenAssignment, elseAssignment)) return null
return element.getIfKeyword().getTextRange()
}
override fun applyTo(element: JetIfExpression, editor: Editor) {
@@ -17,18 +17,20 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.psi.*
public class FoldIfToReturnAsymmetricallyIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with return") {
override fun isApplicableTo(element: JetIfExpression): Boolean {
public class FoldIfToReturnAsymmetricallyIntention : JetSelfTargetingRangeIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with return") {
override fun applicabilityRange(element: JetIfExpression): TextRange? {
if (BranchedFoldingUtils.getFoldableBranchedReturn(element.getThen()) == null || element.getElse() != null) {
return false
return null
}
val nextElement = JetPsiUtil.skipTrailingWhitespacesAndComments(element)
return nextElement is JetReturnExpression && nextElement.getReturnedExpression() != null
val nextElement = JetPsiUtil.skipTrailingWhitespacesAndComments(element) as? JetReturnExpression
if (nextElement?.getReturnedExpression() == null) return null
return element.getIfKeyword().getTextRange()
}
override fun applyTo(element: JetIfExpression, editor: Editor) {
@@ -17,16 +17,18 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
import org.jetbrains.kotlin.psi.createExpressionByPattern
public class FoldIfToReturnIntention : JetSelfTargetingOffsetIndependentIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with return") {
override fun isApplicableTo(element: JetIfExpression): Boolean {
return BranchedFoldingUtils.getFoldableBranchedReturn(element.getThen()) != null
&& BranchedFoldingUtils.getFoldableBranchedReturn(element.getElse()) != null
public class FoldIfToReturnIntention : JetSelfTargetingRangeIntention<JetIfExpression>(javaClass(), "Replace 'if' expression with return") {
override fun applicabilityRange(element: JetIfExpression): TextRange? {
if (BranchedFoldingUtils.getFoldableBranchedReturn(element.getThen()) == null) return null
if (BranchedFoldingUtils.getFoldableBranchedReturn(element.getElse()) == null) return null
return element.getIfKeyword().getTextRange()
}
override fun applyTo(element: JetIfExpression, editor: Editor) {
@@ -17,22 +17,23 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.psi.*
import java.util.ArrayList
public class FoldWhenToAssignmentIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>(javaClass(), "Replace 'when' expression with assignment") {
override fun isApplicableTo(element: JetWhenExpression): Boolean {
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(element)) return false
public class FoldWhenToAssignmentIntention : JetSelfTargetingRangeIntention<JetWhenExpression>(javaClass(), "Replace 'when' expression with assignment") {
override fun applicabilityRange(element: JetWhenExpression): TextRange? {
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(element)) return null
val entries = element.getEntries()
if (entries.isEmpty()) return false
if (entries.isEmpty()) return null
val assignments = ArrayList<JetBinaryExpression>()
for (entry in entries) {
val assignment = BranchedFoldingUtils.getFoldableBranchedAssignment(entry.getExpression()) ?: return false
val assignment = BranchedFoldingUtils.getFoldableBranchedAssignment(entry.getExpression()) ?: return null
assignments.add(assignment)
}
@@ -40,10 +41,10 @@ public class FoldWhenToAssignmentIntention : JetSelfTargetingOffsetIndependentIn
val firstAssignment = assignments.get(0)
for (assignment in assignments) {
if (!BranchedFoldingUtils.checkAssignmentsMatch(assignment, firstAssignment)) return false
if (!BranchedFoldingUtils.checkAssignmentsMatch(assignment, firstAssignment)) return null
}
return true
return element.getWhenKeywordElement().getTextRange()
}
override fun applyTo(element: JetWhenExpression, editor: Editor) {
@@ -17,23 +17,24 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.psi.*
public class FoldWhenToReturnIntention : JetSelfTargetingOffsetIndependentIntention<JetWhenExpression>(javaClass(), "Replace 'when' expression with return") {
override fun isApplicableTo(element: JetWhenExpression): Boolean {
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(element)) return false
public class FoldWhenToReturnIntention : JetSelfTargetingRangeIntention<JetWhenExpression>(javaClass(), "Replace 'when' expression with return") {
override fun applicabilityRange(element: JetWhenExpression): TextRange? {
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(element)) return null
val entries = element.getEntries()
if (entries.isEmpty()) return false
if (entries.isEmpty()) return null
for (entry in entries) {
if (BranchedFoldingUtils.getFoldableBranchedReturn(entry.getExpression()) == null) return false
if (BranchedFoldingUtils.getFoldableBranchedReturn(entry.getExpression()) == null) return null
}
return true
return element.getWhenKeywordElement().getTextRange()
}
override fun applyTo(element: JetWhenExpression, editor: Editor) {
@@ -17,17 +17,19 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetIfExpression
public class UnfoldAssignmentToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'if' expression") {
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false
if (element.getLeft() == null) return false
return element.getRight() is JetIfExpression
public class UnfoldAssignmentToIfIntention : JetSelfTargetingRangeIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'if' expression") {
override fun applicabilityRange(element: JetBinaryExpression): TextRange? {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return null
if (element.getLeft() == null) return null
val right = element.getRight() as? JetIfExpression ?: return null
return TextRange(element.getTextRange().getStartOffset(), right.getIfKeyword().getTextRange().getEndOffset())
}
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
@@ -17,19 +17,21 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.JetBinaryExpression
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.psi.JetWhenExpression
public class UnfoldAssignmentToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'when' expression" ) {
override fun isApplicableTo(element: JetBinaryExpression): Boolean {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return false
if (element.getLeft() == null) return false
val right = element.getRight()
return right is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(right)
public class UnfoldAssignmentToWhenIntention : JetSelfTargetingRangeIntention<JetBinaryExpression>(javaClass(), "Replace assignment with 'when' expression" ) {
override fun applicabilityRange(element: JetBinaryExpression): TextRange? {
if (element.getOperationToken() !in JetTokens.ALL_ASSIGNMENTS) return null
if (element.getLeft() == null) return null
val right = element.getRight() as? JetWhenExpression ?: return null
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(right)) return null
return TextRange(element.getTextRange().getStartOffset(), right.getWhenKeywordElement().getTextRange().getEndOffset())
}
override fun applyTo(element: JetBinaryExpression, editor: Editor) {
@@ -17,16 +17,18 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
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
public class UnfoldPropertyToIfIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>(javaClass(), "Replace property initializer with 'if' expression") {
override fun isApplicableTo(element: JetProperty): Boolean {
if (!element.isLocal()) return false
return element.getInitializer() is JetIfExpression
public class UnfoldPropertyToIfIntention : JetSelfTargetingRangeIntention<JetProperty>(javaClass(), "Replace property initializer with 'if' expression") {
override fun applicabilityRange(element: JetProperty): TextRange? {
if (!element.isLocal()) return null
val initializer = element.getInitializer() as? JetIfExpression ?: return null
return TextRange(element.getTextRange().getStartOffset(), initializer.getIfKeyword().getTextRange().getEndOffset())
}
override fun applyTo(element: JetProperty, editor: Editor) {
@@ -17,18 +17,20 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
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
public class UnfoldPropertyToWhenIntention : JetSelfTargetingOffsetIndependentIntention<JetProperty>(javaClass(), "Replace property initializer with 'when' expression") {
override fun isApplicableTo(element: JetProperty): Boolean {
if (!element.isLocal()) return false
val initializer = element.getInitializer()
return initializer is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)
public class UnfoldPropertyToWhenIntention : JetSelfTargetingRangeIntention<JetProperty>(javaClass(), "Replace property initializer with 'when' expression") {
override fun applicabilityRange(element: JetProperty): TextRange? {
if (!element.isLocal()) return null
val initializer = element.getInitializer() as? JetWhenExpression ?: return null
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(initializer)) return null
return TextRange(element.getTextRange().getStartOffset(), initializer.getWhenKeywordElement().getTextRange().getEndOffset())
}
override fun applyTo(element: JetProperty, editor: Editor) {
@@ -17,7 +17,8 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
import org.jetbrains.kotlin.psi.JetIfExpression
import org.jetbrains.kotlin.psi.JetPsiFactory
@@ -25,9 +26,10 @@ 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 {
return element.getReturnedExpression() is JetIfExpression
public class UnfoldReturnToIfIntention : JetSelfTargetingRangeIntention<JetReturnExpression>(javaClass(), "Replace return with 'if' expression") {
override fun applicabilityRange(element: JetReturnExpression): TextRange? {
val ifExpression = element.getReturnedExpression() as? JetIfExpression ?: return null
return TextRange(element.getTextRange().getStartOffset(), ifExpression.getIfKeyword().getTextRange().getEndOffset())
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
@@ -17,15 +17,17 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingOffsetIndependentIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedUnfoldingUtils
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 {
val expr = element.getReturnedExpression()
return expr is JetWhenExpression && JetPsiUtil.checkWhenExpressionHasSingleElse(expr)
public class UnfoldReturnToWhenIntention : JetSelfTargetingRangeIntention<JetReturnExpression>(javaClass(), "Replace return with 'when' expression") {
override fun applicabilityRange(element: JetReturnExpression): TextRange? {
val whenExpr = element.getReturnedExpression() as? JetWhenExpression ?: return null
if (!JetPsiUtil.checkWhenExpressionHasSingleElse(whenExpr)) return null
return TextRange(element.getTextRange().getStartOffset(), whenExpr.getWhenKeywordElement().getTextRange().getEndOffset())
}
override fun applyTo(element: JetReturnExpression, editor: Editor) {
@@ -17,18 +17,18 @@
package org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingIntention
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.idea.intentions.JetSelfTargetingRangeIntention
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.toExpression
import org.jetbrains.kotlin.psi.*
public class WhenToIfIntention : JetSelfTargetingIntention<JetWhenExpression>(javaClass(), "Replace 'when' with 'if'") {
override fun isApplicableTo(element: JetWhenExpression, caretOffset: Int): Boolean {
public class WhenToIfIntention : JetSelfTargetingRangeIntention<JetWhenExpression>(javaClass(), "Replace 'when' with 'if'") {
override fun applicabilityRange(element: JetWhenExpression): TextRange? {
val entries = element.getEntries()
if (entries.isEmpty()) return false
if (entries.isEmpty()) return null
val lastEntry = entries.last()
if (entries.any { it != lastEntry && it.isElse() }) return false
return element.getWhenKeywordElement().getTextRange().containsOffset(caretOffset)
if (entries.any { it != lastEntry && it.isElse() }) return null
return element.getWhenKeywordElement().getTextRange()
}
override fun applyTo(element: JetWhenExpression, editor: Editor) {