Revert extension function in IndexUtils and better testing

This commit is contained in:
Nikolay Krasko
2019-01-31 17:22:43 +03:00
parent c89bee624a
commit a75da632a6
9 changed files with 37 additions and 29 deletions
@@ -30,7 +30,7 @@ fun <TDeclaration : KtCallableDeclaration> indexTopLevelExtension(stub: KotlinCa
if (stub.isExtension()) {
val declaration = stub.psi
val containingTypeReference = declaration.receiverTypeReference!!
index(containingTypeReference.typeElement, declaration, containingTypeReference) { typeName ->
containingTypeReference.typeElement?.index(declaration, containingTypeReference) { typeName ->
val name = declaration.name ?: return@index
sink.occurrence(
KotlinTopLevelExtensionsByReceiverTypeIndex.INSTANCE.key,
@@ -44,38 +44,35 @@ fun indexTypeAliasExpansion(stub: KotlinTypeAliasStub, sink: IndexSink) {
val declaration = stub.psi
val typeReference = declaration.getTypeReference() ?: return
val typeElement = typeReference.typeElement ?: return
index(typeElement, declaration, typeReference) { typeName ->
typeElement.index(declaration, typeReference) { typeName ->
sink.occurrence(KotlinTypeAliasByExpansionShortNameIndex.KEY, typeName)
}
}
private fun index(
typeElement: KtTypeElement?,
private fun KtTypeElement.index(
declaration: KtTypeParameterListOwner,
containingTypeReference: KtTypeReference,
occurrence: (String) -> Unit
) {
fun internalIndex(
typeElement: KtTypeElement?,
fun KtTypeElement.indexWithVisited(
declaration: KtTypeParameterListOwner,
containingTypeReference: KtTypeReference,
visited: MutableSet<KtTypeElement>,
occurrence: (String) -> Unit
) {
if (typeElement == null) return
if (typeElement in visited) return
if (this in visited) return
visited.add(typeElement)
visited.add(this)
when (typeElement) {
when (this) {
is KtUserType -> {
val referenceName = typeElement.referencedName ?: return
val referenceName = referencedName ?: return
val typeParameter = declaration.typeParameters.firstOrNull { it.name == referenceName }
if (typeParameter != null) {
val bound = typeParameter.extendsBound
if (bound != null) {
internalIndex(bound.typeElement, declaration, containingTypeReference, visited, occurrence)
bound.typeElement?.indexWithVisited(declaration, containingTypeReference, visited, occurrence)
} else {
occurrence("Any")
}
@@ -84,13 +81,13 @@ private fun index(
occurrence(referenceName)
typeElement.aliasImportMap()[referenceName].forEach { occurrence(it) }
aliasImportMap()[referenceName].forEach { occurrence(it) }
}
is KtNullableType -> internalIndex(typeElement.innerType, declaration, containingTypeReference, visited, occurrence)
is KtNullableType -> innerType?.indexWithVisited(declaration, containingTypeReference, visited, occurrence)
is KtFunctionType -> {
val arity = typeElement.parameters.size + (if (typeElement.receiverTypeReference != null) 1 else 0)
val arity = parameters.size + (if (receiverTypeReference != null) 1 else 0)
val suspendPrefix =
if (containingTypeReference.modifierList?.hasModifier(KtTokens.SUSPEND_KEYWORD) == true)
"Suspend"
@@ -101,11 +98,11 @@ private fun index(
is KtDynamicType -> occurrence("Any")
else -> error("Unsupported type: $typeElement")
else -> error("Unsupported type: $this")
}
}
internalIndex(typeElement, declaration, containingTypeReference, mutableSetOf(), occurrence)
indexWithVisited(declaration, containingTypeReference, mutableSetOf(), occurrence)
}
fun indexInternals(stub: KotlinCallableStubBase<*>, sink: IndexSink) {