Skip to content

Commit c8e295c

Browse files
Debugger: do not insert ambiguous imports inside codeFragments
1 parent 4b6255c commit c8e295c

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

compiler/frontend/src/org/jetbrains/kotlin/psi/KtCodeFragment.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,25 @@ abstract class KtCodeFragment(
9999

100100
override fun addImportsFromString(imports: String?) {
101101
if (imports == null || imports.isEmpty()) return
102-
this.imports.addAll(imports.split(IMPORT_SEPARATOR))
102+
103+
imports.split(IMPORT_SEPARATOR).forEach {
104+
addImport(it)
105+
}
103106

104107
// we need this code to force re-highlighting, otherwise it does not work by some reason
105108
val tempElement = KtPsiFactory(project).createColon()
106109
add(tempElement).delete()
107110
}
108111

112+
fun addImport(import: String) {
113+
val contextFile = getContextContainingFile()
114+
if (contextFile != null) {
115+
if (contextFile.importDirectives.find { it.text == import } == null) {
116+
imports.add(import)
117+
}
118+
}
119+
}
120+
109121
fun importsAsImportList(): KtImportList? {
110122
if (!imports.isEmpty() && context != null) {
111123
return KtPsiFactory(this).createAnalyzableFile("imports_for_codeFragment.kt", imports.joinToString("\n"), context).importList
@@ -146,7 +158,11 @@ abstract class KtCodeFragment(
146158

147159
private fun initImports(imports: String?) {
148160
if (imports != null && !imports.isEmpty()) {
149-
this.imports.addAll(imports.split(IMPORT_SEPARATOR).map { it.check { it.startsWith("import ") } ?: "import $it" })
161+
162+
val importsWithPrefix = imports.split(IMPORT_SEPARATOR).map { it.check { it.startsWith("import ") } ?: "import ${it.trim()}" }
163+
importsWithPrefix.forEach {
164+
addImport(it)
165+
}
150166
}
151167
}
152168

idea/testData/debugger/tinyApp/outs/createExpressionWithArray.out

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
LineBreakpoint created at createExpressionWithArray.kt:9
1+
LineBreakpoint created at createExpressionWithArray.kt:11
22
!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
33
Connected to the target VM, address: '!HOST_NAME!:PORT_NAME!', transport: 'socket'
4-
createExpressionWithArray.kt:9
4+
createExpressionWithArray.kt:11
55
package createExpressionWithArray
66

77
import forTests.MyJavaClass
8+
// do not remove this import, it checks that we do not insert ambiguous imports during EE
9+
import forTests.MyJavaClass.InnerClass
810

911
fun main(args: Array<String>) {
1012
val baseArray = arrayOf(MyJavaClass().getBaseClassValue())

idea/testData/debugger/tinyApp/src/evaluate/singleBreakpoint/createExpression/createExpressionWithArray.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package createExpressionWithArray
22

33
import forTests.MyJavaClass
4+
// do not remove this import, it checks that we do not insert ambiguous imports during EE
5+
import forTests.MyJavaClass.InnerClass
46

57
fun main(args: Array<String>) {
68
val baseArray = arrayOf(MyJavaClass().getBaseClassValue())

0 commit comments

Comments
 (0)