Fixed bug with importing some of SAM-constructors and factory functions

#KT-3449 Fixed
This commit is contained in:
Valentin Kipyatkov
2015-02-09 21:24:49 +03:00
parent 2c5953dbe6
commit 546836d5dd
11 changed files with 94 additions and 3 deletions
@@ -58,9 +58,23 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
= DescriptorRenderer.FQ_NAMES_IN_TYPES.render(this)
private fun JetReferenceExpression.targets(context: BindingContext): Collection<DeclarationDescriptor> {
return context[BindingContext.REFERENCE_TARGET, this]?.let { listOf(it.getImportableDescriptor()) }
?: context[BindingContext.AMBIGUOUS_REFERENCE_TARGET, this]?.map { it.getImportableDescriptor() }?.toSet()
?: listOf()
val targets = context[BindingContext.REFERENCE_TARGET, this]?.let { listOf(it) }
?: context[BindingContext.AMBIGUOUS_REFERENCE_TARGET, this]
?: listOf()
return targets.map { descriptorToImport(it) }.toSet()
}
private fun descriptorToImport(target: DeclarationDescriptor): DeclarationDescriptor {
val descriptor = target.getImportableDescriptor()
// if there is a class with the same fq-name then prefer to consider it as target (otherwise we won't insert import)
if (descriptor is CallableDescriptor) {
val container = descriptor.getContainingDeclaration()
if (container is PackageFragmentDescriptor) {
val classifier = container.getMemberScope().getClassifier(descriptor.getName())
if (classifier != null) return classifier
}
}
return descriptor
}
private fun mayImport(descriptor: DeclarationDescriptor, file: JetFile): Boolean {
@@ -0,0 +1,11 @@
// "Import" "true"
import java.util.Collections
import java.util.ArrayList
import java.util.Comparator
fun foo() {
Collections.sort(
ArrayList<Int>(),
<caret>Comparator {(x: Int, y: Int) -> x - y }
)
}
@@ -0,0 +1,10 @@
// "Import" "true"
import java.util.Collections
import java.util.ArrayList
fun foo() {
Collections.sort(
ArrayList<Int>(),
<caret>Comparator {(x: Int, y: Int) -> x - y }
)
}
@@ -0,0 +1,8 @@
// "Import" "true"
// ERROR: Unresolved reference: XXX
import ppp.XXX
fun f() {
XXX { 1 }
}
@@ -0,0 +1,6 @@
// "Import" "true"
// ERROR: Unresolved reference: XXX
fun f() {
<caret>XXX { 1 }
}
@@ -0,0 +1,5 @@
package ppp
class XXX
fun XXX(p: () -> Int){}
@@ -0,0 +1,9 @@
import java.util.Collections
import java.util.ArrayList
fun foo() {
Collections.sort(
ArrayList<Int>(),
<selection>java.util.Comparator</selection> {(x: Int, y: Int) -> x - y }
)
}
@@ -0,0 +1,10 @@
import java.util.Collections
import java.util.ArrayList
import java.util.Comparator
fun foo() {
Collections.sort(
ArrayList<Int>(),
Comparator {(x: Int, y: Int) -> x - y }
)
}
@@ -92,6 +92,12 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes
doTestWithExtraFile(fileName);
}
@TestMetadata("factoryFunctionFromLambda.before.Main.kt")
public void testFactoryFunctionFromLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/factoryFunctionFromLambda.before.Main.kt");
doTestWithExtraFile(fileName);
}
@TestMetadata("falsePostfixOperator.before.Main.kt")
public void testFalsePostfixOperator() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/falsePostfixOperator.before.Main.kt");
@@ -412,6 +412,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("beforeSAMConstructorFromLambda.kt")
public void testSAMConstructorFromLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/beforeSAMConstructorFromLambda.kt");
doTest(fileName);
}
@TestMetadata("beforeUnresolvedReferenceInCall.kt")
public void testUnresolvedReferenceInCall() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/autoImports/beforeUnresolvedReferenceInCall.kt");
@@ -147,6 +147,12 @@ public class ShortenRefsTestGenerated extends AbstractShortenRefsTest {
doTest(fileName);
}
@TestMetadata("SAMConstructorFromLambda.kt")
public void testSAMConstructorFromLambda() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/shortenRefs/constructor/SAMConstructorFromLambda.kt");
doTest(fileName);
}
@TestMetadata("WorksForClassNameRange.kt")
public void testWorksForClassNameRange() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/shortenRefs/constructor/WorksForClassNameRange.kt");