Ordering items in smart completion for 'it'

This commit is contained in:
Valentin Kipyatkov
2014-10-20 18:27:04 +04:00
parent f5f5b514d4
commit af883d9165
5 changed files with 36 additions and 11 deletions
@@ -116,7 +116,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
else -> ExpectedInfoClassification.NOT_MATCHES
}
}
result.addLookupElements(expectedInfos, classifier, { createLookupElement(descriptor, resolveSession) })
result.addLookupElements(expectedInfos, classifier, { createLookupElement(descriptor, resolveSession, bindingContext) })
if (receiver == null) {
toFunctionReferenceLookupElement(descriptor, functionExpectedInfos)?.let { result.add(it) }
@@ -127,7 +127,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
val additionalItems = ArrayList<LookupElement>()
if (receiver == null) {
TypeInstantiationItems(resolveSession, visibilityFilter).addToCollection(additionalItems, expectedInfos)
TypeInstantiationItems(resolveSession, bindingContext, visibilityFilter).addToCollection(additionalItems, expectedInfos)
StaticMembers(bindingContext, resolveSession).addToCollection(additionalItems, expectedInfos, expression, itemsToSkip)
@@ -236,7 +236,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
val matchedExpectedInfos = functionExpectedInfos.filter { functionType.isSubtypeOf(it.type) }
if (matchedExpectedInfos.isEmpty()) return null
var lookupElement = createLookupElement(descriptor, resolveSession)
var lookupElement = createLookupElement(descriptor, resolveSession, bindingContext)
val text = "::" + (if (descriptor is ConstructorDescriptor) descriptor.getContainingDeclaration().getName() else descriptor.getName())
lookupElement = object: LookupElementDecorator<LookupElement>(lookupElement) {
override fun getLookupString() = text
@@ -293,7 +293,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
if (jetType.isError()) return null
val classifier = jetType.getConstructor().getDeclarationDescriptor() ?: return null
val lookupElement = createLookupElement(classifier, resolveSession)
val lookupElement = createLookupElement(classifier, resolveSession, bindingContext)
val lookupString = lookupElement.getLookupString()
val typeArgs = jetType.getArguments()
@@ -107,7 +107,7 @@ class StaticMembers(val bindingContext: BindingContext, val resolveSession: Reso
}
private fun createLookupElement(memberDescriptor: DeclarationDescriptor, classDescriptor: ClassDescriptor): LookupElement {
val lookupElement = createLookupElement(memberDescriptor, resolveSession)
val lookupElement = createLookupElement(memberDescriptor, resolveSession, bindingContext)
val qualifierPresentation = classDescriptor.getName().asString()
val lookupString = qualifierPresentation + "." + lookupElement.getLookupString()
val qualifierText = qualifiedNameForSourceCode(classDescriptor)
@@ -36,8 +36,9 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor
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
class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val visibilityFilter: (DeclarationDescriptor) -> Boolean) {
class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val bindingContext: BindingContext, val visibilityFilter: (DeclarationDescriptor) -> Boolean) {
public fun addToCollection(collection: MutableCollection<LookupElement>, expectedInfos: Collection<ExpectedInfo>) {
val expectedInfosGrouped: Map<JetType, List<ExpectedInfo>> = expectedInfos.groupBy { it.type.makeNotNullable() }
for ((jetType, infos) in expectedInfosGrouped) {
@@ -62,7 +63,7 @@ class TypeInstantiationItems(val resolveSession: ResolveSessionForBodies, val vi
}
if (allConstructors.isNotEmpty() && visibleConstructors.isEmpty()) return
var lookupElement = createLookupElement(classifier, resolveSession)
var lookupElement = createLookupElement(classifier, resolveSession, bindingContext)
var lookupString = lookupElement.getLookupString()
@@ -37,6 +37,8 @@ import org.jetbrains.jet.plugin.completion.handlers.WithTailInsertHandler
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor
import org.jetbrains.jet.lang.descriptors.ClassifierDescriptor
import com.intellij.openapi.util.Key
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor
import org.jetbrains.jet.lang.resolve.BindingContext
class ArtificialElementInsertHandler(
val textBeforeCaret: String, val textAfterCaret: String, val shortenRefs: Boolean) : InsertHandler<LookupElement>{
@@ -184,9 +186,18 @@ fun functionType(function: FunctionDescriptor): JetType? {
function.getReturnType() ?: return null)
}
fun createLookupElement(descriptor: DeclarationDescriptor, resolveSession: ResolveSessionForBodies): LookupElement {
val element = KotlinLookupElementFactory.createLookupElement(resolveSession, descriptor)
return if (descriptor is FunctionDescriptor && descriptor.getValueParameters().isNotEmpty()) element.keepOldArgumentListOnTab() else element
fun createLookupElement(descriptor: DeclarationDescriptor, resolveSession: ResolveSessionForBodies, bindingContext: BindingContext): LookupElement {
var element = KotlinLookupElementFactory.createLookupElement(resolveSession, descriptor)
if (descriptor is FunctionDescriptor && descriptor.getValueParameters().isNotEmpty()) {
element = element.keepOldArgumentListOnTab()
}
if (descriptor is ValueParameterDescriptor && bindingContext[BindingContext.AUTO_CREATED_IT, descriptor]) {
element = element.assignSmartCompletionPriority(SmartCompletionItemPriority.IT)
}
return element
}
fun JetType.isSubtypeOf(expectedType: JetType) = !isError() && JetTypeChecker.DEFAULT.isSubtypeOf(this, expectedType)
@@ -197,7 +208,7 @@ fun <T : Any> T?.toSet(): Set<T> = if (this != null) setOf(this) else setOf()
fun String?.isNullOrEmpty() = this == null || this.isEmpty()
enum class SmartCompletionItemPriority {
/*IT*/ //TODO
IT
TRUE
FALSE
THIS
@@ -0,0 +1,13 @@
fun foo(xx: String){}
fun bar(handler: (String) -> Unit){}
fun f() {
val v = ""
val xx = ""
bar { foo(<caret>) }
}
// ORDER: xx
// ORDER: it
// ORDER: v