KT-9815 Import insertion (on Alt-Enter) does not work in Evaluate Expression

#KT-9815 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-10-29 15:08:48 +03:00
parent 61fc3e7705
commit 752cdd5f83
2 changed files with 7 additions and 3 deletions
@@ -103,6 +103,10 @@ public abstract class KtCodeFragment(
override fun addImportsFromString(imports: String?) { override fun addImportsFromString(imports: String?) {
if (imports == null || imports.isEmpty()) return if (imports == null || imports.isEmpty()) return
myImports.addAll(imports.split(IMPORT_SEPARATOR)) myImports.addAll(imports.split(IMPORT_SEPARATOR))
// we need this code to force re-highlighting, otherwise it does not work by some reason
val tempElement = KtPsiFactory(project).createColon()
add(tempElement).delete()
} }
public fun importsAsImportList(): KtImportList? { public fun importsAsImportList(): KtImportList? {
@@ -30,7 +30,7 @@ import org.jetbrains.kotlin.idea.completion.test.ExpectedCompletionUtils
import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest import org.jetbrains.kotlin.idea.completion.test.handlers.AbstractCompletionHandlerTest
import org.jetbrains.kotlin.idea.test.JetWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.test.JetWithJdkAndRuntimeLightProjectDescriptor
import org.jetbrains.kotlin.idea.util.ImportInsertHelper import org.jetbrains.kotlin.idea.util.ImportInsertHelper
import org.jetbrains.kotlin.idea.util.application.runWriteAction import org.jetbrains.kotlin.idea.util.application.executeWriteCommand
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
@@ -49,12 +49,12 @@ public abstract class AbstractCodeFragmentHighlightingTest : AbstractJetPsiCheck
fun doTestWithImport(filePath: String) { fun doTestWithImport(filePath: String) {
myFixture.configureByCodeFragment(filePath) myFixture.configureByCodeFragment(filePath)
runWriteAction { project.executeWriteCommand("Imports insertion") {
val fileText = FileUtil.loadFile(File(filePath), true) val fileText = FileUtil.loadFile(File(filePath), true)
val file = myFixture.getFile() as KtFile val file = myFixture.getFile() as KtFile
InTextDirectivesUtils.findListWithPrefixes(fileText, "// IMPORT: ").forEach { InTextDirectivesUtils.findListWithPrefixes(fileText, "// IMPORT: ").forEach {
val descriptor = file.resolveImportReference(FqName(it)).singleOrNull() val descriptor = file.resolveImportReference(FqName(it)).singleOrNull()
?: error("Could not resolve descriptor to import: $it") ?: error("Could not resolve descriptor to import: $it")
ImportInsertHelper.getInstance(getProject()).importDescriptor(file, descriptor) ImportInsertHelper.getInstance(getProject()).importDescriptor(file, descriptor)
} }
} }