From 48433110a4bc39df906d55212fe146ddc7b42f2d Mon Sep 17 00:00:00 2001 From: Natalia Selezneva Date: Mon, 25 Feb 2019 13:12:24 +0300 Subject: [PATCH] Do not threat modifications inside lambdas in KtScriptInitializer block as out of code block --- .../kotlin/generators/tests/GenerateTests.kt | 2 +- .../kotlin/generators/tests/GenerateTests.kt.191 | 2 +- .../kotlin/generators/tests/GenerateTests.kt.as32 | 2 +- .../kotlin/generators/tests/GenerateTests.kt.as33 | 2 +- .../kotlin/generators/tests/GenerateTests.kt.as34 | 2 +- .../KotlinCodeBlockModificationListener.kt | 15 +++++++++++---- .../outOfBlock/scriptInLambdaExpression.kts | 8 ++++++++ .../outOfBlock/scriptTopLevelExpression.kts | 4 ++++ .../OutOfBlockModificationTestGenerated.java | 12 +++++++++++- 9 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 idea/testData/codeInsight/outOfBlock/scriptInLambdaExpression.kts create mode 100644 idea/testData/codeInsight/outOfBlock/scriptTopLevelExpression.kts diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index b802d3f6b15..ed8b5ffe8db 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -527,7 +527,7 @@ fun main(args: Array) { } testClass { - model("codeInsight/outOfBlock") + model("codeInsight/outOfBlock", pattern = KT_OR_KTS) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.191 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.191 index bf20940e324..5ee58a570e6 100755 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.191 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.191 @@ -518,7 +518,7 @@ fun main(args: Array) { } testClass { - model("codeInsight/outOfBlock") + model("codeInsight/outOfBlock", pattern = KT_OR_KTS) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 index 25e9b4eca07..255d600e4f8 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as32 @@ -515,7 +515,7 @@ fun main(args: Array) { } testClass { - model("codeInsight/outOfBlock") + model("codeInsight/outOfBlock", pattern = KT_OR_KTS) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 index ceadfe00ba2..a80ca5c3f79 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as33 @@ -507,7 +507,7 @@ fun main(args: Array) { } testClass { - model("codeInsight/outOfBlock") + model("codeInsight/outOfBlock", pattern = KT_OR_KTS) } testClass { diff --git a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 index f6975177d93..03b90785eaf 100644 --- a/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 +++ b/generators/tests/org/jetbrains/kotlin/generators/tests/GenerateTests.kt.as34 @@ -507,7 +507,7 @@ fun main(args: Array) { } testClass { - model("codeInsight/outOfBlock") + model("codeInsight/outOfBlock", pattern = KT_OR_KTS) } testClass { diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinCodeBlockModificationListener.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinCodeBlockModificationListener.kt index a6636466dff..b03d0207b13 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinCodeBlockModificationListener.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/project/KotlinCodeBlockModificationListener.kt @@ -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("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>( KtProperty::class.java, - KtNamedFunction::class.java + KtNamedFunction::class.java, + KtScriptInitializer::class.java ) fun getInstance(project: Project) = project.getComponent(KotlinCodeBlockModificationListener::class.java) diff --git a/idea/testData/codeInsight/outOfBlock/scriptInLambdaExpression.kts b/idea/testData/codeInsight/outOfBlock/scriptInLambdaExpression.kts new file mode 100644 index 00000000000..1fe8a82535f --- /dev/null +++ b/idea/testData/codeInsight/outOfBlock/scriptInLambdaExpression.kts @@ -0,0 +1,8 @@ +// FALSE +plugins { + +} + +fun plugins(f: () -> Unit) { + +} \ No newline at end of file diff --git a/idea/testData/codeInsight/outOfBlock/scriptTopLevelExpression.kts b/idea/testData/codeInsight/outOfBlock/scriptTopLevelExpression.kts new file mode 100644 index 00000000000..238f826bc3e --- /dev/null +++ b/idea/testData/codeInsight/outOfBlock/scriptTopLevelExpression.kts @@ -0,0 +1,4 @@ +// TRUE +1 + +// SKIP_ANALYZE_CHECK diff --git a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java index ea235d2b458..666918aa007 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/codeInsight/OutOfBlockModificationTestGenerated.java @@ -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");