diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt index 70816755788..e77e1354de7 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt @@ -131,6 +131,14 @@ public abstract class KtCodeFragment( return true } + public fun getContextContainingFile(): KtFile? { + val contextFile = (getContext() as? KtElement)?.getContainingKtFile() + if (contextFile is KtCodeFragment) { + return contextFile.getContextContainingFile() + } + return contextFile + } + private fun initImports(imports: String?) { if (imports != null && !imports.isEmpty()) { this.imports.addAll(imports.split(IMPORT_SEPARATOR).map { it.check { it.startsWith("import ") } ?: "import $it" }) diff --git a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt index 35317d87f07..c6f83caeabe 100644 --- a/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt +++ b/idea/src/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluationBuilder.kt @@ -479,7 +479,7 @@ package packageForDebugger private fun createFileForDebugger(codeFragment: KtCodeFragment, extractedFunction: KtNamedFunction ): KtFile { - val containingContextFile = (codeFragment.context as? KtElement)?.getContainingKtFile() + val containingContextFile = codeFragment.getContextContainingFile() val importsFromContextFile = containingContextFile?.importList?.let { it.text + "\n" } ?: "" val packageFromContextFile = containingContextFile?.packageName?.let { if (it.isNotBlank()) "import $it.*\n" else null diff --git a/idea/testData/debugger/tinyApp/outs/importsLambdaContext.out b/idea/testData/debugger/tinyApp/outs/importsLambdaContext.out new file mode 100644 index 00000000000..ba76b60824c --- /dev/null +++ b/idea/testData/debugger/tinyApp/outs/importsLambdaContext.out @@ -0,0 +1,11 @@ +LineBreakpoint created at importsLambdaContext.kt:10 lambdaOrdinal = 0 +!JDK_HOME!\bin\java -agentlib:jdwp=transport=dt_socket,address=!HOST_NAME!:!HOST_PORT!,suspend=y,server=n -Dfile.encoding=!FILE_ENCODING! -classpath !OUTPUT_PATH!;!KOTLIN_RUNTIME!;!CUSTOM_LIBRARY!;!RT_JAR! importsLambdaContext.ImportsLambdaContextKt +Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket' +importsLambdaContext.kt:10 +Compile bytecode for Collections.emptyList() +Compile bytecode for ArrayList() +Compile bytecode for HashSet() +Compile bytecode for JHashMap() +Disconnected from the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket' + +Process finished with exit code 0 diff --git a/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/importsLambdaContext.kt b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/importsLambdaContext.kt new file mode 100644 index 00000000000..a4215942eeb --- /dev/null +++ b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/importsLambdaContext.kt @@ -0,0 +1,23 @@ +package importsLambdaContext + +import java.util.Collections +import java.util.ArrayList; +import java.util.HashSet +import java.util.HashMap as JHashMap + +fun main(args: Array) { + //Breakpoint! (lambdaOrdinal = 0) + arrayOf(1).map { args.size() } +} + +// EXPRESSION: Collections.emptyList() +// RESULT: instance of java.util.Collections$EmptyList(id=ID): Ljava/util/Collections$EmptyList; + +// EXPRESSION: ArrayList() +// RESULT: instance of java.util.ArrayList(id=ID): Ljava/util/ArrayList; + +// EXPRESSION: HashSet() +// RESULT: instance of java.util.HashSet(id=ID): Ljava/util/HashSet; + +// EXPRESSION: JHashMap() +// RESULT: instance of java.util.HashMap(id=ID): Ljava/util/HashMap; diff --git a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java index 1a69f749b76..9606aa5abb3 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/debugger/evaluate/KotlinEvaluateExpressionTestGenerated.java @@ -151,6 +151,12 @@ public class KotlinEvaluateExpressionTestGenerated extends AbstractKotlinEvaluat doSingleBreakpointTest(fileName); } + @TestMetadata("importsLambdaContext.kt") + public void testImportsLambdaContext() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/importsLambdaContext.kt"); + doSingleBreakpointTest(fileName); + } + @TestMetadata("inlineFunction.kt") public void testInlineFunction() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/inlineFunction.kt");