Do not threat modifications inside lambdas in KtScriptInitializer block as out of code block

This commit is contained in:
Natalia Selezneva
2019-02-25 13:12:24 +03:00
parent 49277bb5e4
commit 48433110a4
9 changed files with 39 additions and 10 deletions
@@ -527,7 +527,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractOutOfBlockModificationTest> {
model("codeInsight/outOfBlock")
model("codeInsight/outOfBlock", pattern = KT_OR_KTS)
}
testClass<AbstractDataFlowValueRenderingTest> {
@@ -518,7 +518,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractOutOfBlockModificationTest> {
model("codeInsight/outOfBlock")
model("codeInsight/outOfBlock", pattern = KT_OR_KTS)
}
testClass<AbstractDataFlowValueRenderingTest> {
@@ -515,7 +515,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractOutOfBlockModificationTest> {
model("codeInsight/outOfBlock")
model("codeInsight/outOfBlock", pattern = KT_OR_KTS)
}
testClass<AbstractDataFlowValueRenderingTest> {
@@ -507,7 +507,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractOutOfBlockModificationTest> {
model("codeInsight/outOfBlock")
model("codeInsight/outOfBlock", pattern = KT_OR_KTS)
}
testClass<AbstractDataFlowValueRenderingTest> {
@@ -507,7 +507,7 @@ fun main(args: Array<String>) {
}
testClass<AbstractOutOfBlockModificationTest> {
model("codeInsight/outOfBlock")
model("codeInsight/outOfBlock", pattern = KT_OR_KTS)
}
testClass<AbstractDataFlowValueRenderingTest> {
@@ -39,7 +39,6 @@ import org.jetbrains.kotlin.idea.caches.project.cached
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getTopmostParentOfType
import org.jetbrains.kotlin.psi.psiUtil.isAncestor
import org.jetbrains.kotlin.psi.psiUtil.parents
val KOTLIN_CONSOLE_KEY = Key.create<Boolean>("kotlin.console")
@@ -128,8 +127,8 @@ class KotlinCodeBlockModificationListener(
?.let { return it }
}
val blockDeclaration = KtPsiUtil.getTopmostParentOfTypes(element, *BLOCK_DECLARATION_TYPES) ?: return null
if (blockDeclaration.parents.any { it !is KtClassBody && it !is KtClassOrObject && it !is KtFile }) return null // should not be local declaration
val blockDeclaration = KtPsiUtil.getTopmostParentOfTypes(element, *BLOCK_DECLARATION_TYPES) as? KtDeclaration ?: return null
if (KtPsiUtil.isLocal(blockDeclaration)) return null // should not be local declaration
when (blockDeclaration) {
is KtNamedFunction -> {
@@ -151,6 +150,13 @@ class KotlinCodeBlockModificationListener(
}
}
is KtScriptInitializer -> {
return (blockDeclaration.body as? KtCallExpression)
?.lambdaArguments?.last()
?.getLambdaExpression()
?.takeIf { it.isAncestor(element) }
}
else -> throw IllegalStateException()
}
@@ -163,7 +169,8 @@ class KotlinCodeBlockModificationListener(
private val BLOCK_DECLARATION_TYPES = arrayOf<Class<out KtDeclaration>>(
KtProperty::class.java,
KtNamedFunction::class.java
KtNamedFunction::class.java,
KtScriptInitializer::class.java
)
fun getInstance(project: Project) = project.getComponent(KotlinCodeBlockModificationListener::class.java)
@@ -0,0 +1,8 @@
// FALSE
plugins {
<caret>
}
fun plugins(f: () -> Unit) {
}
@@ -0,0 +1,4 @@
// TRUE
1<caret>
// SKIP_ANALYZE_CHECK
@@ -26,7 +26,7 @@ public class OutOfBlockModificationTestGenerated extends AbstractOutOfBlockModif
}
public void testAllFilesPresentInOutOfBlock() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/outOfBlock"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/outOfBlock"), Pattern.compile("^(.+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("Class_Class_FunNoType_Block.kt")
@@ -219,6 +219,16 @@ public class OutOfBlockModificationTestGenerated extends AbstractOutOfBlockModif
runTest("idea/testData/codeInsight/outOfBlock/PropertyWithType_Initializer_String.kt");
}
@TestMetadata("scriptInLambdaExpression.kts")
public void testScriptInLambdaExpression() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/scriptInLambdaExpression.kts");
}
@TestMetadata("scriptTopLevelExpression.kts")
public void testScriptTopLevelExpression() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/scriptTopLevelExpression.kts");
}
@TestMetadata("StringInSuperConstroctorCall.kt")
public void testStringInSuperConstroctorCall() throws Exception {
runTest("idea/testData/codeInsight/outOfBlock/StringInSuperConstroctorCall.kt");