Changed semantics of ImportInsertHelper.importDescriptor() - it does not check if we allowed to import it on reference shortening

This commit is contained in:
Valentin Kipyatkov
2015-10-18 11:13:56 +03:00
parent 1730069d5e
commit b62260180f
11 changed files with 28 additions and 22 deletions
@@ -16,19 +16,18 @@
package org.jetbrains.kotlin.idea.util
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetImportDirective
import org.jetbrains.kotlin.resolve.ImportPath
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import com.intellij.openapi.project.Project
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.resolve.ImportPath
import java.util.*
public abstract class ImportInsertHelper {
/*TODO: implementation is not quite correct*/
public abstract fun isImportedWithDefault(importPath: ImportPath, contextFile: JetFile): Boolean
public abstract fun mayImportByCodeStyle(descriptor: DeclarationDescriptor): Boolean
public abstract fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean
public abstract val importSortComparator: Comparator<ImportPath>
@@ -65,7 +65,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
private fun mayImport(descriptor: DeclarationDescriptor, file: JetFile): Boolean {
return descriptor.canBeReferencedViaImport()
&& ImportInsertHelper.getInstance(file.getProject()).mayImportByCodeStyle(descriptor)
&& ImportInsertHelper.getInstance(file.getProject()).mayImportOnShortenReferences(descriptor)
}
}
@@ -81,7 +81,7 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
return importPath.isImported(defaultImports)
}
override fun mayImportByCodeStyle(descriptor: DeclarationDescriptor): Boolean {
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {
val importable = descriptor.getImportableDescriptor()
return when (importable) {
is PackageViewDescriptor -> false // now package cannot be imported
@@ -132,10 +132,6 @@ public class ImportInsertHelperImpl(private val project: Project) : ImportInsert
val targetFqName = target.importableFqName ?: return ImportDescriptorResult.FAIL
if (isAlreadyImported(target, topLevelScope, targetFqName)) return ImportDescriptorResult.ALREADY_IMPORTED
if (!mayImportByCodeStyle(descriptor)) {
return ImportDescriptorResult.FAIL
}
val imports = if (file is JetCodeFragment)
file.importsAsImportList()?.getImports() ?: listOf()
else
-1
View File
@@ -1 +0,0 @@
// IMPORT: kotlin.Map.Entry
@@ -1 +0,0 @@
Failed to add import
@@ -0,0 +1,5 @@
package dependency
object MyObject {
fun someFun(){}
}
+4
View File
@@ -0,0 +1,4 @@
// IMPORT: dependency.MyObject.someFun
fun foo() {
someFun()
}
+6
View File
@@ -0,0 +1,6 @@
import dependency.MyObject.someFun
// IMPORT: dependency.MyObject.someFun
fun foo() {
someFun()
}
-1
View File
@@ -1,2 +1 @@
// IMPORT: kotlin.Map.Entry
// IMPORT_NESTED_CLASSES: true
-1
View File
@@ -1,4 +1,3 @@
import kotlin.Map.*
// IMPORT: kotlin.Map.Entry
// IMPORT_NESTED_CLASSES: true
@@ -155,12 +155,6 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
doTest(fileName);
}
@TestMetadata("DoNotImportNestedClass.kt")
public void testDoNotImportNestedClass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/DoNotImportNestedClass.kt");
doTest(fileName);
}
@TestMetadata("DropExplicitImports.kt")
public void testDropExplicitImports() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/DropExplicitImports.kt");
@@ -197,6 +191,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
doTest(fileName);
}
@TestMetadata("ImportFromObject.kt")
public void testImportFromObject() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFromObject.kt");
doTest(fileName);
}
@TestMetadata("ImportFunctionBug.kt")
public void testImportFunctionBug() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt");