Static members completion includes SAM-adapters

This commit is contained in:
Valentin Kipyatkov
2015-11-02 22:23:58 +03:00
parent ea804ed76e
commit 3bd508ca84
6 changed files with 19 additions and 6 deletions
@@ -214,7 +214,8 @@ class BasicCompletionSession(
collector.addDescriptorElements(notImported, lookupElementFactory, notImported = true)
}
val staticMembersCompletion = StaticMembersCompletion(collector, prefixMatcher, resolutionFacade, lookupElementFactory, referenceVariants!!.imported)
val staticMembersCompletion = StaticMembersCompletion(
collector, prefixMatcher, resolutionFacade, lookupElementFactory, referenceVariants!!.imported, isJvmModule)
if (callTypeAndReceiver is CallTypeAndReceiver.DEFAULT) {
staticMembersCompletion.completeFromImports(position.containingFile as KtFile)
}
@@ -37,7 +37,8 @@ class StaticMembersCompletion(
private val prefixMatcher: PrefixMatcher,
private val resolutionFacade: ResolutionFacade,
private val lookupElementFactory: LookupElementFactory,
alreadyAdded: Collection<DeclarationDescriptor>
alreadyAdded: Collection<DeclarationDescriptor>,
private val isJvmModule: Boolean
) {
private val alreadyAdded = alreadyAdded.mapTo(HashSet()) {
if (it is ImportedFromObjectCallableDescriptor<*>) it.callableFromObject else it
@@ -64,10 +65,8 @@ class StaticMembersCompletion(
}
}
//TODO: SAM-adapters
//TODO: filter out those that are accessible from SmartCompletion.additionalItems
//TODO: what about enum members?
//TODO: filter out Kotlin functions from file facades
//TODO: better presentation for lookup elements from imports too
//TODO: better sorting
//TODO: from the same file
@@ -75,13 +74,15 @@ class StaticMembersCompletion(
val descriptorKindFilter = DescriptorKindFilter.CALLABLES exclude DescriptorKindExclude.Extensions
val nameFilter: (String) -> Boolean = { prefixMatcher.prefixMatches(it) }
indicesHelper.getJavaStaticMembers(descriptorKindFilter, nameFilter).forEach { addFromDescriptor(it, ItemPriority.STATIC_MEMBER) }
if (isJvmModule) {
indicesHelper.getJavaStaticMembers(descriptorKindFilter, nameFilter).forEach { addFromDescriptor(it, ItemPriority.STATIC_MEMBER) }
}
indicesHelper.getObjectMembers(descriptorKindFilter, nameFilter).forEach { addFromDescriptor(it, ItemPriority.STATIC_MEMBER) }
}
private fun addFromDescriptor(member: CallableDescriptor, itemPriority: ItemPriority) {
if (member !in alreadyAdded) {
if (member !in alreadyAdded) { //TODO: substitution
collector.addElement(createLookupElement(member, itemPriority) ?: return)
}
}
@@ -5,4 +5,5 @@ fun foo() {
// INVOCATION_COUNT: 2
// EXIST_JAVA_ONLY: { allLookupStrings: "invokeLater", itemText: "SwingUtilities.invokeLater", tailText: "(Runnable!) (javax.swing)", typeText: "Unit", attributes: "" }
// EXIST_JAVA_ONLY: { allLookupStrings: "invokeAndWait", itemText: "SwingUtilities.invokeAndWait", tailText: "(Runnable!) (javax.swing)", typeText: "Unit", attributes: "" }
// EXIST_JAVA_ONLY: { allLookupStrings: "invokeLater", itemText: "SwingUtilities.invokeLater", tailText: " {...} ((() -> Unit)!) (javax.swing)", typeText: "Unit", attributes: "" }
// ABSENT: { itemText: "SwingUtilities.convertScreenLocationToParent" }
@@ -4,3 +4,4 @@ fun foo() {
// INVOCATION_COUNT: 2
// ELEMENT_TEXT: "SwingUtilities.invokeLater"
// TAIL_TEXT: "(Runnable!) (javax.swing)"
@@ -6,3 +6,4 @@ fun foo() {
// INVOCATION_COUNT: 2
// ELEMENT_TEXT: "SwingUtilities.invokeLater"
// TAIL_TEXT: "(Runnable!) (javax.swing)"
@@ -35,7 +35,9 @@ import org.jetbrains.kotlin.idea.util.CallType
import org.jetbrains.kotlin.idea.util.CallTypeAndReceiver
import org.jetbrains.kotlin.idea.util.receiverTypes
import org.jetbrains.kotlin.idea.util.substituteExtensionIfCallable
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.load.java.descriptors.SamAdapterDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtExpression
@@ -229,8 +231,14 @@ public class KotlinIndicesHelper(
if (!method.hasModifierProperty(PsiModifier.STATIC)) continue
if (!visibilityFilterMayIncludeAccessible && method.hasModifierProperty(PsiModifier.PRIVATE)) continue
val descriptor = method.getJavaMethodDescriptor() ?: continue
val container = descriptor.containingDeclaration as? ClassDescriptor ?: continue
if (descriptorKindFilter.accepts(descriptor) && descriptorFilter(descriptor)) {
result.add(descriptor)
val samAdapter = container.staticScope.getContributedFunctions(descriptor.name, NoLookupLocation.FROM_IDE)
.filterIsInstance<SamAdapterDescriptor<*>>()
.firstOrNull { it.originForSam.original == descriptor.original }
result.addIfNotNull(samAdapter)
}
}
true