KT-22815: Cleanup findKDoc logic

This commit is contained in:
Simon Ogorodnik
2018-07-16 20:04:11 +03:00
parent 1bd02472a2
commit ad6eac6ec6
@@ -39,13 +39,20 @@ fun DeclarationDescriptor.findKDoc(
return null return null
} }
private typealias DescriptorToPsi = (DeclarationDescriptorWithSource) -> PsiElement?
fun KtElement.findKDoc(descriptorToPsi: (DeclarationDescriptorWithSource) -> PsiElement?): KDocTag? { fun KtElement.findKDoc(descriptorToPsi: DescriptorToPsi): KDocTag? {
var psiDeclaration = this return this.lookupOwnedKDoc()
?: this.lookupKDocInContainer()
?: this.lookupInheritedKDoc(descriptorToPsi)
}
private fun KtElement.lookupOwnedKDoc(): KDocTag? {
// KDoc for primary constructor is located inside of its class KDoc // KDoc for primary constructor is located inside of its class KDoc
if (psiDeclaration is KtPrimaryConstructor) { val psiDeclaration = when (this) {
psiDeclaration = psiDeclaration.getContainingClassOrObject() is KtPrimaryConstructor -> getContainingClassOrObject()
else -> this
} }
if (psiDeclaration is KtDeclaration) { if (psiDeclaration is KtDeclaration) {
@@ -61,43 +68,34 @@ fun KtElement.findKDoc(descriptorToPsi: (DeclarationDescriptorWithSource) -> Psi
return kdoc.getDefaultSection() return kdoc.getDefaultSection()
} }
} }
return null
}
private fun KtElement.lookupKDocInContainer(): KDocTag? {
val subjectName = name
if (this is KtParameter || this is KtTypeParameter) { val containingDeclaration =
val containingDeclaration = PsiTreeUtil.findFirstParent(this, true) {
PsiTreeUtil.findFirstParent(this, true) { it is KtDeclarationWithBody && it !is KtPrimaryConstructor
it is KtDeclarationWithBody && it !is KtPrimaryConstructor || it is KtClassOrObject
|| it is KtClassOrObject
}
val containerKDoc = containingDeclaration?.getChildOfType<KDoc>()
val subjectName = name
if (containerKDoc != null && subjectName != null) {
val propertyDoc =
containerKDoc.findSectionByTag(KDocKnownTag.PROPERTY, subjectName)
?.takeIf { this is KtParameter && this.isPropertyParameter() }
if (propertyDoc != null) return propertyDoc
val paramDoc =
containerKDoc.findDescendantOfType<KDocTag> { it.knownTag == KDocKnownTag.PARAM && it.getSubjectName() == subjectName }
if (paramDoc != null) return paramDoc
} }
}
if (this is KtProperty) { val containerKDoc = containingDeclaration?.getChildOfType<KDoc>()
val classKDoc = containingClass()?.getChildOfType<KDoc>() if (containerKDoc == null || subjectName == null) return null
val subjectName = name val propertySection = containerKDoc.findSectionByTag(KDocKnownTag.PROPERTY, subjectName)
if (classKDoc != null && subjectName != null) { val paramTag = containerKDoc.findDescendantOfType<KDocTag> { it.knownTag == KDocKnownTag.PARAM && it.getSubjectName() == subjectName }
val propertySection = classKDoc.findSectionByTag(KDocKnownTag.PROPERTY, subjectName)
if (propertySection != null) {
return propertySection
}
}
}
return when {
this is KtParameter && this.isPropertyParameter() -> propertySection ?: paramTag
this is KtParameter || this is KtTypeParameter -> paramTag
this is KtProperty && containingDeclaration is KtClassOrObject -> propertySection
else -> null
}
}
private fun KtElement.lookupInheritedKDoc(descriptorToPsi: DescriptorToPsi): KDocTag? {
if (this is KtCallableDeclaration) { if (this is KtCallableDeclaration) {
val descriptor = this.resolveToDescriptorIfAny() as? CallableDescriptor ?: return null val descriptor = this.resolveToDescriptorIfAny() as? CallableDescriptor ?: return null
@@ -108,6 +106,5 @@ fun KtElement.findKDoc(descriptorToPsi: (DeclarationDescriptorWithSource) -> Psi
} }
} }
} }
return null return null
} }