Smart completion does not need SAM-constructor in declarations from scope + it's present even when not imported
This commit is contained in:
@@ -34,6 +34,7 @@ import org.jetbrains.jet.plugin.caches.resolve.getLazyResolveSession
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.getReceiverExpression
|
||||
import org.jetbrains.jet.plugin.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.jet.lang.resolve.java.descriptor.SamConstructorDescriptor
|
||||
|
||||
class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
val resolveSession: ResolveSessionForBodies,
|
||||
@@ -106,7 +107,8 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
|
||||
fun filterDeclaration(descriptor: DeclarationDescriptor): Collection<LookupElement> {
|
||||
val result = ArrayList<LookupElement>()
|
||||
if (!itemsToSkip.contains(descriptor)) {
|
||||
if (!itemsToSkip.contains(descriptor)
|
||||
&& descriptor !is SamConstructorDescriptor /* SAM-constructor is added explicitly and is not needed here */) {
|
||||
val types = typesWithSmartCasts(descriptor)
|
||||
val nonNullTypes = types.map { it.makeNotNullable() }
|
||||
val classifier = { (expectedInfo: ExpectedInfo) ->
|
||||
|
||||
@@ -37,6 +37,8 @@ import org.jetbrains.jet.lang.descriptors.Visibilities
|
||||
import org.jetbrains.jet.plugin.util.makeNotNullable
|
||||
import org.jetbrains.jet.plugin.util.IdeDescriptorRenderers
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import org.jetbrains.jet.lang.descriptors.PackageFragmentDescriptor
|
||||
import org.jetbrains.jet.lang.resolve.java.descriptor.SamConstructorDescriptor
|
||||
|
||||
class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val bindingContext: BindingContext, val visibilityFilter: (DeclarationDescriptor) -> Boolean) {
|
||||
public fun addToCollection(collection: MutableCollection<LookupElement>, expectedInfos: Collection<ExpectedInfo>) {
|
||||
@@ -53,6 +55,8 @@ class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val bi
|
||||
val classifier = jetType.getConstructor().getDeclarationDescriptor()
|
||||
if (classifier !is ClassDescriptor) return
|
||||
|
||||
addSamConstructorItem(collection, classifier, tail)
|
||||
|
||||
val isAbstract = classifier.getModality() == Modality.ABSTRACT
|
||||
val allConstructors = classifier.getConstructors()
|
||||
val visibleConstructors = allConstructors.filter {
|
||||
@@ -135,4 +139,22 @@ class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val bi
|
||||
|
||||
collection.add(lookupElement.addTail(tail))
|
||||
}
|
||||
|
||||
private fun addSamConstructorItem(collection: MutableCollection<LookupElement>, `class`: ClassDescriptor, tail: Tail?) {
|
||||
if (`class`.getKind() == ClassKind.TRAIT) {
|
||||
val container = `class`.getContainingDeclaration()
|
||||
val scope = when (container) {
|
||||
is PackageFragmentDescriptor -> container.getMemberScope()
|
||||
is ClassDescriptor -> container.getStaticScope()
|
||||
else -> return
|
||||
}
|
||||
val samConstructor = scope.getFunctions(`class`.getName())
|
||||
.filterIsInstance(javaClass<SamConstructorDescriptor>())
|
||||
.singleOrNull() ?: return
|
||||
val lookupElement = createLookupElement(samConstructor, resolveSession, bindingContext)
|
||||
.assignSmartCompletionPriority(SmartCompletionItemPriority.INSTANTIATION)
|
||||
.addTail(tail)
|
||||
collection.add(lookupElement)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
var a : java.io.Closeable = <caret>
|
||||
|
||||
// ELEMENT: Closeable
|
||||
@@ -0,0 +1,5 @@
|
||||
import java.io.Closeable
|
||||
|
||||
var a : java.io.Closeable = Closeable { <caret> }
|
||||
|
||||
// ELEMENT: Closeable
|
||||
@@ -234,12 +234,6 @@ public class JvmSmartCompletionTestGenerated extends AbstractJvmSmartCompletionT
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("FunctionReference6.kt")
|
||||
public void testFunctionReference6() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/smart/FunctionReference6.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("FunctionReference7.kt")
|
||||
public void testFunctionReference7() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/smart/FunctionReference7.kt");
|
||||
|
||||
+6
@@ -522,6 +522,12 @@ public class SmartCompletionHandlerTestGenerated extends AbstractSmartCompletion
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("SAMExpected2.kt")
|
||||
public void testSAMExpected2() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/handlers/smart/SAMExpected2.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("TabReplaceComma1.kt")
|
||||
public void testTabReplaceComma1() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/completion/handlers/smart/TabReplaceComma1.kt");
|
||||
|
||||
Reference in New Issue
Block a user