InfixCallToOrdinaryIntention - smaller range + minor code improvements

This commit is contained in:
Valentin Kipyatkov
2015-04-29 17:37:01 +03:00
parent 1b7ac6bb02
commit 9dd67cfc4d
7 changed files with 21 additions and 19 deletions
@@ -23,26 +23,28 @@ import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression
import org.jetbrains.kotlin.psi.JetParenthesizedExpression import org.jetbrains.kotlin.psi.JetParenthesizedExpression
import org.jetbrains.kotlin.lexer.JetTokens import org.jetbrains.kotlin.lexer.JetTokens
public class InfixCallToOrdinaryIntention : JetSelfTargetingOffsetIndependentIntention<JetBinaryExpression>(javaClass(), "Replace infix call with ordinary call") { public class InfixCallToOrdinaryIntention : JetSelfTargetingIntention<JetBinaryExpression>(javaClass(), "Replace infix call with ordinary call") {
override fun isApplicableTo(element: JetBinaryExpression): Boolean { override fun isApplicableTo(element: JetBinaryExpression, caretOffset: Int): Boolean {
return element.getLeft() != null && element.getRight() != null && element.getOperationToken() == JetTokens.IDENTIFIER if (element.getOperationToken() != JetTokens.IDENTIFIER || element.getLeft() == null || element.getRight() == null) return false
return element.getOperationReference().getTextRange().containsOffset(caretOffset)
} }
override fun applyTo(element: JetBinaryExpression, editor: Editor) { override fun applyTo(element: JetBinaryExpression, editor: Editor) {
val receiverText = element.getLeft()!!.getText() val receiverText = element.getLeft()!!.getText()
val argumentText = element.getRight()!!.getText() val argumentText = element.getRight()!!.getText()
val functionName = element.getOperationReference().getText() val functionName = element.getOperationReference().getText()
val replacementExpressionStringBuilder = StringBuilder("$receiverText.$functionName")
replacementExpressionStringBuilder.append( val text = StringBuilder {
when (element.getRight()) { append(receiverText)
is JetFunctionLiteralExpression -> " $argumentText" append(".")
is JetParenthesizedExpression -> argumentText append(functionName)
else -> "($argumentText)" append(when (element.getRight()) {
} is JetFunctionLiteralExpression -> " $argumentText"
) is JetParenthesizedExpression -> argumentText
else -> "($argumentText)"
})
}.toString()
val replacement = JetPsiFactory(element).createExpression(replacementExpressionStringBuilder.toString()) element.replace(JetPsiFactory(element).createExpression(text))
element.replace(replacement)
} }
} }
@@ -1,3 +1,3 @@
fun foo(x: Int) { fun foo(x: Int) {
(<caret>x shl 1).minus() (x <caret>shl 1).minus()
} }
@@ -2,5 +2,5 @@ trait Foo {
fun foo(f: (Int) -> Unit) fun foo(f: (Int) -> Unit)
} }
fun foo(x: Foo) { fun foo(x: Foo) {
<caret>x foo { it * 2 } x <caret>foo { it * 2 }
} }
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false // IS_APPLICABLE: false
fun foo(x: String) { fun foo(x: String) {
<caret>x == x x <caret>== x
} }
@@ -1,3 +1,3 @@
fun foo(x: String) { fun foo(x: String) {
<caret>x!! compareTo "1" x!! <caret>compareTo "1"
} }
@@ -1,3 +1,3 @@
fun foo(x: String) { fun foo(x: String) {
<caret>x plus ("1" + "2") x plus<caret> ("1" + "2")
} }
@@ -1,3 +1,3 @@
fun foo(x: Int) { fun foo(x: Int) {
<caret>x compareTo 1 x <caret>compareTo 1
} }