Changed semantics of ImportInsertHelper.importDescriptor() - it does not check if we allowed to import it on reference shortening
This commit is contained in:
@@ -16,19 +16,18 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.util
|
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.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.*
|
import java.util.*
|
||||||
|
|
||||||
public abstract class ImportInsertHelper {
|
public abstract class ImportInsertHelper {
|
||||||
/*TODO: implementation is not quite correct*/
|
/*TODO: implementation is not quite correct*/
|
||||||
public abstract fun isImportedWithDefault(importPath: ImportPath, contextFile: JetFile): Boolean
|
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>
|
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 {
|
private fun mayImport(descriptor: DeclarationDescriptor, file: JetFile): Boolean {
|
||||||
return descriptor.canBeReferencedViaImport()
|
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)
|
return importPath.isImported(defaultImports)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun mayImportByCodeStyle(descriptor: DeclarationDescriptor): Boolean {
|
override fun mayImportOnShortenReferences(descriptor: DeclarationDescriptor): Boolean {
|
||||||
val importable = descriptor.getImportableDescriptor()
|
val importable = descriptor.getImportableDescriptor()
|
||||||
return when (importable) {
|
return when (importable) {
|
||||||
is PackageViewDescriptor -> false // now package cannot be imported
|
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
|
val targetFqName = target.importableFqName ?: return ImportDescriptorResult.FAIL
|
||||||
if (isAlreadyImported(target, topLevelScope, targetFqName)) return ImportDescriptorResult.ALREADY_IMPORTED
|
if (isAlreadyImported(target, topLevelScope, targetFqName)) return ImportDescriptorResult.ALREADY_IMPORTED
|
||||||
|
|
||||||
if (!mayImportByCodeStyle(descriptor)) {
|
|
||||||
return ImportDescriptorResult.FAIL
|
|
||||||
}
|
|
||||||
|
|
||||||
val imports = if (file is JetCodeFragment)
|
val imports = if (file is JetCodeFragment)
|
||||||
file.importsAsImportList()?.getImports() ?: listOf()
|
file.importsAsImportList()?.getImports() ?: listOf()
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -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
@@ -0,0 +1,4 @@
|
|||||||
|
// IMPORT: dependency.MyObject.someFun
|
||||||
|
fun foo() {
|
||||||
|
someFun()
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import dependency.MyObject.someFun
|
||||||
|
|
||||||
|
// IMPORT: dependency.MyObject.someFun
|
||||||
|
fun foo() {
|
||||||
|
someFun()
|
||||||
|
}
|
||||||
@@ -1,2 +1 @@
|
|||||||
// IMPORT: kotlin.Map.Entry
|
// IMPORT: kotlin.Map.Entry
|
||||||
// IMPORT_NESTED_CLASSES: true
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import kotlin.Map.*
|
import kotlin.Map.*
|
||||||
|
|
||||||
// IMPORT: kotlin.Map.Entry
|
// IMPORT: kotlin.Map.Entry
|
||||||
// IMPORT_NESTED_CLASSES: true
|
|
||||||
|
|||||||
@@ -155,12 +155,6 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestMetadata("DoNotImportNestedClass.kt")
|
|
||||||
public void testDoNotImportNestedClass() throws Exception {
|
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/DoNotImportNestedClass.kt");
|
|
||||||
doTest(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
@TestMetadata("DropExplicitImports.kt")
|
@TestMetadata("DropExplicitImports.kt")
|
||||||
public void testDropExplicitImports() throws Exception {
|
public void testDropExplicitImports() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/DropExplicitImports.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/DropExplicitImports.kt");
|
||||||
@@ -197,6 +191,12 @@ public class AddImportTestGenerated extends AbstractAddImportTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("ImportFromObject.kt")
|
||||||
|
public void testImportFromObject() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFromObject.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("ImportFunctionBug.kt")
|
@TestMetadata("ImportFunctionBug.kt")
|
||||||
public void testImportFunctionBug() throws Exception {
|
public void testImportFunctionBug() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/addImport/ImportFunctionBug.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user