Lambda to anonymous: report only from left brace to arrow

This commit is contained in:
Mikhail Glukhikh
2018-05-15 15:26:02 +03:00
parent f4acaae364
commit 6a1ddd6c70
10 changed files with 25 additions and 11 deletions
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.psi.*
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.resolve.bindingContextUtil.getTargetFunctionDescriptor
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
@@ -32,7 +33,9 @@ class LambdaToAnonymousFunctionIntention : SelfTargetingIntention<KtLambdaExpres
override fun isApplicableTo(element: KtLambdaExpression, caretOffset: Int): Boolean {
if (element.getStrictParentOfType<KtValueArgument>() == null) 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?) {
@@ -2,5 +2,5 @@ class Foo
fun bar(f: Foo.() -> Unit) {}
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 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 test() {
foo {
foo {<caret>
if (it == 1) {
return@foo "1"
}
"$it"
<caret>}
}
}
@@ -1,7 +1,7 @@
fun foo(f: (Int) -> String) {}
fun test() {
foo {
foo {<caret>
if (it == 1) {
return@foo "1"
} else if (it == 2) {
@@ -9,5 +9,5 @@ fun test() {
} else {
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 test() {
foo <caret>{ "" }
foo { <caret>-> "" }
}
@@ -3,8 +3,8 @@ fun unit(f: (Int) -> Unit) {}
fun foo(i: Int) {}
fun test() {
unit {
unit {<caret>
foo(it)
foo(it)
}<caret>
}
}
@@ -6,7 +6,6 @@
// ACTION: Replace with safe (this?.) call
// ACTION: Specify explicit lambda signature
// 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?
fun String?.foo(a: String?) {
@@ -9704,6 +9704,11 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
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")
public void testNamedArgument() throws Exception {
runTest("idea/testData/intentions/lambdaToAnonymousFunction/namedArgument.kt");