"Move lambda argument out of parentheses" intention - smaller range
This commit is contained in:
@@ -302,8 +302,6 @@ replace.with.infix.function.call.intention.error.resolution.failed=The element c
|
||||
replace.with.infix.function.call.intention.error.package.call=Cannot be applied with a package as the receiver
|
||||
replace.explicit.function.literal.param.with.it=Replace explicit parameter ''{0}'' with ''it''
|
||||
replace.explicit.function.literal.param.with.it.family=Replace Explicit Parameter with 'it'
|
||||
move.lambda.outside.parentheses=Move lambda expression out of parentheses
|
||||
move.lambda.outside.parentheses.family=Move Lambda Expression out of Parentheses
|
||||
replace.it.with.explicit.function.literal.param=Replace 'it' with explicit parameter
|
||||
replace.it.with.explicit.function.literal.param.family=Replace 'it' with Explicit Parameter
|
||||
split.if=Split into 2 if's
|
||||
|
||||
+16
-14
@@ -18,23 +18,25 @@ package org.jetbrains.kotlin.idea.intentions
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import org.jetbrains.kotlin.idea.util.psiModificationUtil.moveLambdaOutsideParentheses
|
||||
import org.jetbrains.kotlin.psi.JetCallExpression
|
||||
import org.jetbrains.kotlin.psi.JetFunctionLiteralExpression
|
||||
import org.jetbrains.kotlin.psi.JetPsiFactory
|
||||
import org.jetbrains.kotlin.psi.JetExpression
|
||||
import org.jetbrains.kotlin.psi.JetLabeledExpression
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParentheses
|
||||
|
||||
public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingOffsetIndependentIntention<JetCallExpression>(
|
||||
"move.lambda.outside.parentheses", javaClass()) {
|
||||
public class MoveLambdaOutsideParenthesesIntention : JetSelfTargetingIntention<JetCallExpression>(javaClass(), "Move lambda argument out of parentheses") {
|
||||
override fun isApplicableTo(element: JetCallExpression, caretOffset: Int): Boolean {
|
||||
val argument = element.getValueArgumentsInParentheses().lastOrNull() ?: return false
|
||||
val expression = argument.getArgumentExpression() ?: return false
|
||||
val functionLiteral = getFunctionLiteral(expression) ?: return false
|
||||
if (caretOffset < argument.asElement().getTextRange().getStartOffset()) return false
|
||||
val bodyRange = functionLiteral.getBodyExpression()?.getTextRange() ?: return true
|
||||
return caretOffset <= bodyRange.getStartOffset() || caretOffset >= bodyRange.getEndOffset()
|
||||
}
|
||||
|
||||
private fun isLambdaOrLabeledLambda(expression: JetExpression?): Boolean =
|
||||
expression is JetFunctionLiteralExpression ||
|
||||
(expression is JetLabeledExpression && isLambdaOrLabeledLambda(expression.getBaseExpression()))
|
||||
|
||||
override fun isApplicableTo(element: JetCallExpression): Boolean {
|
||||
val args = element.getValueArgumentsInParentheses()
|
||||
return args.size > 0 && isLambdaOrLabeledLambda(args.last?.getArgumentExpression())
|
||||
private fun getFunctionLiteral(expression: JetExpression?): JetFunctionLiteral? {
|
||||
return when (expression) {
|
||||
is JetFunctionLiteralExpression -> expression.getFunctionLiteral()
|
||||
is JetLabeledExpression -> getFunctionLiteral(expression.getBaseExpression())
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
override fun applyTo(element: JetCallExpression, editor: Editor) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: false
|
||||
fun foo() {
|
||||
bar(<caret>) { it }
|
||||
bar() <caret>{ it }
|
||||
}
|
||||
|
||||
fun bar(b: (Int) -> Int) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(2, l@{ it })
|
||||
bar(2, <caret>l@{ it })
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(2, { it })
|
||||
bar(2, <caret>{ it })
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(2) { it }
|
||||
bar(2) { it }
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(a = 2, b = { it })
|
||||
bar(a = 2, b = {<caret> it })
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(a = 2) { it }
|
||||
bar(a = 2) { it }
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// ERROR: No value passed for parameter b
|
||||
// ERROR: Unresolved reference: it
|
||||
fun foo() {
|
||||
bar<caret>({ it })
|
||||
bar({ it <caret>})
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(2, {
|
||||
bar(2, {
|
||||
val x = 3
|
||||
it * x
|
||||
})
|
||||
})<caret>
|
||||
}
|
||||
|
||||
fun bar(a: Int, b: (Int) -> Int) {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(name1 = 3, name2 = 2, name3 = 1, name4 = { it })
|
||||
bar(name1 = 3, name2 = 2, name3 = 1, <caret>name4 = { it })
|
||||
}
|
||||
|
||||
fun bar(name1: Int, name2: Int, name3: Int, name4: (Int) -> Int) : Int {
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// IS_APPLICABLE: true
|
||||
fun foo() {
|
||||
bar<caret>(3, 2, 1, { it })
|
||||
bar(3, 2, 1, { it }<caret>)
|
||||
}
|
||||
|
||||
fun bar(name1: Int, name2: Int, name3: Int, name4: (Int) -> Int) : Int {
|
||||
@@ -4591,15 +4591,15 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("moveLambda7.kt")
|
||||
public void testMoveLambda7() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda7.kt");
|
||||
@TestMetadata("moveLambda5.kt")
|
||||
public void testMoveLambda5() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda5.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("moveLambda8.kt")
|
||||
public void testMoveLambda8() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda8.kt");
|
||||
@TestMetadata("moveLambda6.kt")
|
||||
public void testMoveLambda6() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/moveLambdaOutsideParentheses/moveLambda6.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user