From 070ee108729970d13536288f7ddececc669e7439 Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Tue, 5 Apr 2016 18:57:17 +0300 Subject: [PATCH] Debugger: do not insert ambiguous imports inside codeFragments #KT-11927 Fixed --- .../jetbrains/kotlin/psi/KtCodeFragment.kt | 20 +++++++++++++++++-- .../outs/createExpressionWithArray.out | 6 ++++-- .../createExpressionWithArray.kt | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt index aedf721c14d..a152431acda 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt @@ -99,13 +99,25 @@ abstract class KtCodeFragment( override fun addImportsFromString(imports: String?) { if (imports == null || imports.isEmpty()) return - this.imports.addAll(imports.split(IMPORT_SEPARATOR)) + + imports.split(IMPORT_SEPARATOR).forEach { + addImport(it) + } // we need this code to force re-highlighting, otherwise it does not work by some reason val tempElement = KtPsiFactory(project).createColon() add(tempElement).delete() } + fun addImport(import: String) { + val contextFile = getContextContainingFile() + if (contextFile != null) { + if (contextFile.importDirectives.find { it.text == import } == null) { + imports.add(import) + } + } + } + fun importsAsImportList(): KtImportList? { if (!imports.isEmpty() && context != null) { return KtPsiFactory(this).createAnalyzableFile("imports_for_codeFragment.kt", imports.joinToString("\n"), context).importList @@ -146,7 +158,11 @@ abstract class KtCodeFragment( 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" }) + + val importsWithPrefix = imports.split(IMPORT_SEPARATOR).map { it.check { it.startsWith("import ") } ?: "import ${it.trim()}" } + importsWithPrefix.forEach { + addImport(it) + } } } diff --git a/idea/testData/debugger/tinyApp/outs/createExpressionWithArray.out b/idea/testData/debugger/tinyApp/outs/createExpressionWithArray.out index 127abe89d99..5fd1999f290 100644 --- a/idea/testData/debugger/tinyApp/outs/createExpressionWithArray.out +++ b/idea/testData/debugger/tinyApp/outs/createExpressionWithArray.out @@ -1,10 +1,12 @@ -LineBreakpoint created at createExpressionWithArray.kt:9 +LineBreakpoint created at createExpressionWithArray.kt:11 !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! createExpressionWithArray.CreateExpressionWithArrayKt Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket' -createExpressionWithArray.kt:9 +createExpressionWithArray.kt:11 package createExpressionWithArray import forTests.MyJavaClass +// do not remove this import, it checks that we do not insert ambiguous imports during EE +import forTests.MyJavaClass.InnerClass fun main(args: Array) { val baseArray = arrayOf(MyJavaClass().getBaseClassValue()) diff --git a/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/createExpression/createExpressionWithArray.kt b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/createExpression/createExpressionWithArray.kt index 07384a5513b..dcb7e747f6b 100644 --- a/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/createExpression/createExpressionWithArray.kt +++ b/idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/createExpression/createExpressionWithArray.kt @@ -1,6 +1,8 @@ package createExpressionWithArray import forTests.MyJavaClass +// do not remove this import, it checks that we do not insert ambiguous imports during EE +import forTests.MyJavaClass.InnerClass fun main(args: Array) { val baseArray = arrayOf(MyJavaClass().getBaseClassValue())