Lambda to anonymous: report only from left brace to arrow
This commit is contained in:
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
|
|||||||
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.endOffset
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescriptor
|
import org.jetbrains.kotlin.resolve.bindingContextUtil.getTargetFunctionDescriptor
|
||||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||||
@@ -32,7 +33,9 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
|
|||||||
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
|
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
|
||||||
if (element.getStrictParentOfType<KtValueArgument>() == null) return false
|
if (element.getStrictParentOfType<KtValueArgument>() == null) return false
|
||||||
val descriptor = element.functionLiteral.descriptor as? AnonymousFunctionDescriptor ?: return false
|
val descriptor = element.functionLiteral.descriptor as? AnonymousFunctionDescriptor ?: return false
|
||||||
return descriptor.valueParameters.none { it.name.isSpecial }
|
if (descriptor.valueParameters.any { it.name.isSpecial }) return false
|
||||||
|
val lastElement = element.functionLiteral.arrow ?: element.functionLiteral.lBrace
|
||||||
|
return caretOffset <= lastElement.endOffset
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun applyTo(element: KtLambdaExpression, editor: Editor?) {
|
override fun applyTo(element: KtLambdaExpression, editor: Editor?) {
|
||||||
|
|||||||
@@ -2,5 +2,5 @@ class Foo
|
|||||||
fun bar(f: Foo.() -> Unit) {}
|
fun bar(f: Foo.() -> Unit) {}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
bar {}<caret>
|
bar {<caret>}
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,5 @@ class Foo
|
|||||||
fun baz(f: Foo.(i: Int, j: Int) -> Int) {}
|
fun baz(f: Foo.(i: Int, j: Int) -> Int) {}
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
baz { i, j -> i + j }<caret>
|
baz { i, <caret>j -> i + j }
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
fun foo(f: (Int) -> String) {}
|
fun foo(f: (Int) -> String) {}
|
||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
foo {
|
foo {<caret>
|
||||||
if (it == 1) {
|
if (it == 1) {
|
||||||
return@foo "1"
|
return@foo "1"
|
||||||
}
|
}
|
||||||
"$it"
|
"$it"
|
||||||
<caret>}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
fun foo(f: (Int) -> String) {}
|
fun foo(f: (Int) -> String) {}
|
||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
foo {
|
foo {<caret>
|
||||||
if (it == 1) {
|
if (it == 1) {
|
||||||
return@foo "1"
|
return@foo "1"
|
||||||
} else if (it == 2) {
|
} else if (it == 2) {
|
||||||
@@ -9,5 +9,5 @@ fun test() {
|
|||||||
} else {
|
} else {
|
||||||
return@foo "$it"
|
return@foo "$it"
|
||||||
}
|
}
|
||||||
}<caret>
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// IS_APPLICABLE: false
|
||||||
|
|
||||||
|
fun foo(f: () -> String) {}
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
foo { -> "" <caret>}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
fun foo(f: () -> String) {}
|
fun foo(f: () -> String) {}
|
||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
foo <caret>{ "" }
|
foo { <caret>-> "" }
|
||||||
}
|
}
|
||||||
@@ -3,8 +3,8 @@ fun unit(f: (Int) -> Unit) {}
|
|||||||
fun foo(i: Int) {}
|
fun foo(i: Int) {}
|
||||||
|
|
||||||
fun test() {
|
fun test() {
|
||||||
unit {
|
unit {<caret>
|
||||||
foo(it)
|
foo(it)
|
||||||
foo(it)
|
foo(it)
|
||||||
}<caret>
|
}
|
||||||
}
|
}
|
||||||
-1
@@ -6,7 +6,6 @@
|
|||||||
// ACTION: Replace with safe (this?.) call
|
// ACTION: Replace with safe (this?.) call
|
||||||
// ACTION: Specify explicit lambda signature
|
// ACTION: Specify explicit lambda signature
|
||||||
// ACTION: Add return@let
|
// ACTION: Add return@let
|
||||||
// ACTION: Convert to anonymous function
|
|
||||||
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
|
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
|
||||||
|
|
||||||
fun String?.foo(a: String?) {
|
fun String?.foo(a: String?) {
|
||||||
|
|||||||
@@ -9704,6 +9704,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
runTest("idea/testData/intentions/lambdaToAnonymousFunction/implicitParameterName.kt");
|
runTest("idea/testData/intentions/lambdaToAnonymousFunction/implicitParameterName.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("incorrectOffset.kt")
|
||||||
|
public void testIncorrectOffset() throws Exception {
|
||||||
|
runTest("idea/testData/intentions/lambdaToAnonymousFunction/incorrectOffset.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("namedArgument.kt")
|
@TestMetadata("namedArgument.kt")
|
||||||
public void testNamedArgument() throws Exception {
|
public void testNamedArgument() throws Exception {
|
||||||
runTest("idea/testData/intentions/lambdaToAnonymousFunction/namedArgument.kt");
|
runTest("idea/testData/intentions/lambdaToAnonymousFunction/namedArgument.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user