Allow to move suspend lambda out of parentheses

#KT-26674 Fixed
#KT-26676 Fixed
This commit is contained in:
Mikhail Glukhikh
2018-09-13 12:21:14 +03:00
parent 3a5de13dd4
commit 76d1d6ff12
7 changed files with 49 additions and 3 deletions
@@ -21,7 +21,7 @@ import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.source.codeStyle.CodeEditUtil
import com.intellij.psi.tree.IElementType
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.builtins.isFunctionType
import org.jetbrains.kotlin.builtins.isFunctionOrSuspendFunctionType
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.extensions.DeclarationAttributeAltererExtension
import org.jetbrains.kotlin.idea.caches.resolve.analyze
@@ -135,8 +135,8 @@ fun KtCallExpression.canMoveLambdaOutsideParentheses(): Boolean {
// if there are functions among candidates but none of them have last function parameter then not show the intention
if (candidates.isNotEmpty() && candidates.none { candidate ->
val params = candidate.valueParameters
params.lastOrNull()?.type?.isFunctionType == true &&
params.count { it.type.isFunctionType } == lambdaArgumentCount + referenceArgumentCount
params.lastOrNull()?.type?.isFunctionOrSuspendFunctionType == true &&
params.count { it.type.isFunctionOrSuspendFunctionType } == lambdaArgumentCount + referenceArgumentCount
}
) return false
}
@@ -0,0 +1,7 @@
fun runSuspend(block: suspend () -> Unit) {}
fun println() {}
fun usage() {
runSuspend(<caret>{ println() })
}
@@ -0,0 +1,7 @@
fun runSuspend(block: suspend () -> Unit) {}
fun println() {}
fun usage() {
runSuspend { println() }
}
@@ -0,0 +1,11 @@
// "Replace with 'runSuspend(block)'" "true"
fun runSuspend(block: suspend () -> Unit) {}
@Deprecated("Use new function", ReplaceWith("runSuspend(block)"))
fun runSuspendOld(block: suspend () -> Unit) = runSuspend(block)
fun println() {}
fun usage() {
<caret>runSuspendOld { println() }
}
@@ -0,0 +1,11 @@
// "Replace with 'runSuspend(block)'" "true"
fun runSuspend(block: suspend () -> Unit) {}
@Deprecated("Use new function", ReplaceWith("runSuspend(block)"))
fun runSuspendOld(block: suspend () -> Unit) = runSuspend(block)
fun println() {}
fun usage() {
runSuspend { println() }
}
@@ -3387,6 +3387,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
public void testNoTwoConsequentLambdas() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/noTwoConsequentLambdas.kt");
}
@TestMetadata("suspendLambda.kt")
public void testSuspendLambda() throws Exception {
runTest("idea/testData/inspectionsLocal/moveLambdaOutsideParentheses/suspendLambda.kt");
}
}
@TestMetadata("idea/testData/inspectionsLocal/moveSuspiciousCallableReferenceIntoParentheses")
@@ -5323,6 +5323,11 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/stringTemplate.kt");
}
@TestMetadata("suspendLambda.kt")
public void testSuspendLambda() throws Exception {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/suspendLambda.kt");
}
@TestMetadata("toOuterClassMethod.kt")
public void testToOuterClassMethod() throws Exception {
runTest("idea/testData/quickfix/deprecatedSymbolUsage/toOuterClassMethod.kt");