Code refactoring

This commit is contained in:
Valentin Kipyatkov
2014-12-03 21:15:13 +03:00
parent 7ba11b0f2f
commit 796a2b6e7c
2 changed files with 8 additions and 19 deletions
@@ -128,21 +128,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
val result = ArrayList<LookupElement>() val result = ArrayList<LookupElement>()
val types = descriptor.fuzzyTypes(smartCastTypes) val types = descriptor.fuzzyTypes(smartCastTypes)
val classifier = { (expectedInfo: ExpectedInfo) -> val classifier = { (expectedInfo: ExpectedInfo) -> types.classifyExpectedInfo(expectedInfo) }
val substitutor = types.stream().map { it.checkIsSubtypeOf(expectedInfo.type) }.firstOrNull()
if (substitutor != null) {
ExpectedInfoClassification.matches(substitutor)
}
else {
val substitutor2 = types.stream().map { it.makeNotNullable().checkIsSubtypeOf(expectedInfo.type) }.firstOrNull()
if (substitutor2 != null) {
ExpectedInfoClassification.matchesIfNotNullable(substitutor2)
}
else {
ExpectedInfoClassification.notMatches
}
}
}
result.addLookupElements(descriptor, expectedInfos, classifier) { descriptor -> result.addLookupElements(descriptor, expectedInfos, classifier) { descriptor ->
lookupElementFactory.createLookupElement(descriptor, resolutionFacade, bindingContext, true) lookupElementFactory.createLookupElement(descriptor, resolutionFacade, bindingContext, true)
} }
@@ -110,14 +110,15 @@ class ExpectedInfoClassification private(val substitutor: TypeSubstitutor?, val
} }
} }
fun FuzzyType.classifyExpectedInfo(expectedInfo: ExpectedInfo): ExpectedInfoClassification { fun Collection<FuzzyType>.classifyExpectedInfo(expectedInfo: ExpectedInfo): ExpectedInfoClassification {
val substitutor = checkIsSubtypeOf(expectedInfo.type) val stream = stream()
val substitutor = stream.map { it.checkIsSubtypeOf(expectedInfo.type) }.firstOrNull()
if (substitutor != null) { if (substitutor != null) {
return ExpectedInfoClassification.matches(substitutor) return ExpectedInfoClassification.matches(substitutor)
} }
if (isNullable()) { if (stream.any { it.isNullable() }) {
val substitutor2 = makeNotNullable().checkIsSubtypeOf(expectedInfo.type) val substitutor2 = stream.map { it.makeNotNullable().checkIsSubtypeOf(expectedInfo.type) }.firstOrNull()
if (substitutor2 != null) { if (substitutor2 != null) {
return ExpectedInfoClassification.matchesIfNotNullable(substitutor2) return ExpectedInfoClassification.matchesIfNotNullable(substitutor2)
} }
@@ -126,6 +127,8 @@ fun FuzzyType.classifyExpectedInfo(expectedInfo: ExpectedInfo): ExpectedInfoClas
return ExpectedInfoClassification.notMatches return ExpectedInfoClassification.notMatches
} }
fun FuzzyType.classifyExpectedInfo(expectedInfo: ExpectedInfo) = listOf(this).classifyExpectedInfo(expectedInfo)
fun<TDescriptor: DeclarationDescriptor?> MutableCollection<LookupElement>.addLookupElements( fun<TDescriptor: DeclarationDescriptor?> MutableCollection<LookupElement>.addLookupElements(
descriptor: TDescriptor, descriptor: TDescriptor,
expectedInfos: Collection<ExpectedInfo>, expectedInfos: Collection<ExpectedInfo>,