From 0671fd9aaa2c9fc9edd05c5639de0a117b6d3aad Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 17 Dec 2020 19:25:41 +0100 Subject: [PATCH] Support destructuring declarations in scratch files #KT-25038 fixed --- .../compile/KtScratchSourceFileProcessor.kt | 19 +++++++++++++++++++ .../ScratchRunActionTestGenerated.java | 10 ++++++++++ .../scratch/destructuringDecls.comp.after | 7 +++++++ .../testData/scratch/destructuringDecls.kts | 7 +++++++ .../scratch/destructuringDecls.repl.after | 7 +++++++ 5 files changed, 50 insertions(+) create mode 100644 idea/scripting-support/testData/scratch/destructuringDecls.comp.after create mode 100644 idea/scripting-support/testData/scratch/destructuringDecls.kts create mode 100644 idea/scripting-support/testData/scratch/destructuringDecls.repl.after diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtScratchSourceFileProcessor.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtScratchSourceFileProcessor.kt index df110183eef..8064f40fd3a 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtScratchSourceFileProcessor.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/compile/KtScratchSourceFileProcessor.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.diagnostics.rendering.RenderingContext import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny import org.jetbrains.kotlin.idea.scratch.ScratchExpression import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.calls.util.isSingleUnderscore class KtScratchSourceFileProcessor { companion object { @@ -71,6 +72,7 @@ class KtScratchSourceFileProcessor { fun process(expression: ScratchExpression) { val psiElement = expression.element when (psiElement) { + is KtDestructuringDeclaration -> processDestructuringDeclaration(expression, psiElement) is KtVariableDeclaration -> processDeclaration(expression, psiElement) is KtFunction -> processDeclaration(expression, psiElement) is KtClassOrObject -> processDeclaration(expression, psiElement) @@ -89,6 +91,23 @@ class KtScratchSourceFileProcessor { objectBuilder.appendLineInfo(e) } + private fun processDestructuringDeclaration(e: ScratchExpression, c: KtDestructuringDeclaration) { + val entries = c.entries.mapNotNull { if (it.isSingleUnderscore) null else it.resolveToDescriptorIfAny() } + entries.forEach { + val context = RenderingContext.of(it) + val rendered = Renderers.COMPACT.render(it, context) + classBuilder.append(rendered).newLine() + objectBuilder.println(rendered) + } + objectBuilder.appendLineInfo(e) + classBuilder.append("init {").newLine() + classBuilder.append(c.text).newLine() + entries.forEach { + classBuilder.append("this.${it.name} = ${it.name}").newLine() + } + classBuilder.append("}").newLine() + } + private fun processExpression(e: ScratchExpression, expr: KtExpression) { val resName = "$GET_RES_FUN_NAME_PREFIX$resCount" diff --git a/idea/scripting-support/test/org/jetbrains/kotlin/idea/scratch/ScratchRunActionTestGenerated.java b/idea/scripting-support/test/org/jetbrains/kotlin/idea/scratch/ScratchRunActionTestGenerated.java index 58a941e2496..383712fa1f7 100644 --- a/idea/scripting-support/test/org/jetbrains/kotlin/idea/scratch/ScratchRunActionTestGenerated.java +++ b/idea/scripting-support/test/org/jetbrains/kotlin/idea/scratch/ScratchRunActionTestGenerated.java @@ -31,6 +31,11 @@ public class ScratchRunActionTestGenerated extends AbstractScratchRunActionTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/scripting-support/testData/scratch"), Pattern.compile("^(.+)\\.kts$"), null, false); } + @TestMetadata("destructuringDecls.kts") + public void testDestructuringDecls() throws Exception { + runTest("idea/scripting-support/testData/scratch/destructuringDecls.kts"); + } + @TestMetadata("for.kts") public void testFor() throws Exception { runTest("idea/scripting-support/testData/scratch/for.kts"); @@ -119,6 +124,11 @@ public class ScratchRunActionTestGenerated extends AbstractScratchRunActionTest KtTestUtil.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("idea/scripting-support/testData/scratch"), Pattern.compile("^(.+)\\.kts$"), null, false); } + @TestMetadata("destructuringDecls.kts") + public void testDestructuringDecls() throws Exception { + runTest("idea/scripting-support/testData/scratch/destructuringDecls.kts"); + } + @TestMetadata("for.kts") public void testFor() throws Exception { runTest("idea/scripting-support/testData/scratch/for.kts"); diff --git a/idea/scripting-support/testData/scratch/destructuringDecls.comp.after b/idea/scripting-support/testData/scratch/destructuringDecls.comp.after new file mode 100644 index 00000000000..e950d3bd456 --- /dev/null +++ b/idea/scripting-support/testData/scratch/destructuringDecls.comp.after @@ -0,0 +1,7 @@ +// REPL_MODE: false + +val (foo, bar) = 1 to "2" // RESULT: val foo: Int; val bar: String +foo // RESULT: 1 +bar // RESULT: 2 +val (_, baz) = 3 to "4" // RESULT: val baz: String +baz // RESULT: 4 \ No newline at end of file diff --git a/idea/scripting-support/testData/scratch/destructuringDecls.kts b/idea/scripting-support/testData/scratch/destructuringDecls.kts new file mode 100644 index 00000000000..e24c166f89f --- /dev/null +++ b/idea/scripting-support/testData/scratch/destructuringDecls.kts @@ -0,0 +1,7 @@ +// REPL_MODE: ~REPL_MODE~ + +val (foo, bar) = 1 to "2" +foo +bar +val (_, baz) = 3 to "4" +baz diff --git a/idea/scripting-support/testData/scratch/destructuringDecls.repl.after b/idea/scripting-support/testData/scratch/destructuringDecls.repl.after new file mode 100644 index 00000000000..531cdbbc5f5 --- /dev/null +++ b/idea/scripting-support/testData/scratch/destructuringDecls.repl.after @@ -0,0 +1,7 @@ +// REPL_MODE: true + +val (foo, bar) = 1 to "2" +foo // RESULT: res1: kotlin.Int = 1 +bar // RESULT: res2: kotlin.String = 2 +val (_, baz) = 3 to "4" +baz // RESULT: res4: kotlin.String = 4