Show documentation for a class if the constructor has no docs #KT-17926 Fixed
diff --git a/idea/src/org/jetbrains/kotlin/idea/KotlinDocumentationProvider.kt b/idea/src/org/jetbrains/kotlin/idea/KotlinDocumentationProvider.kt index 58547a7..d6fb301 100644 --- a/idea/src/org/jetbrains/kotlin/idea/KotlinDocumentationProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/KotlinDocumentationProvider.kt
@@ -6,7 +6,6 @@ package org.jetbrains.kotlin.idea import com.google.common.html.HtmlEscapers -import com.intellij.codeInsight.documentation.DocumentationManager import com.intellij.codeInsight.documentation.DocumentationManagerUtil import com.intellij.codeInsight.javadoc.JavaDocInfoGeneratorFactory import com.intellij.lang.documentation.AbstractDocumentationProvider @@ -33,7 +32,6 @@ import org.jetbrains.kotlin.idea.kdoc.KDocRenderer.appendKDocContent import org.jetbrains.kotlin.idea.kdoc.KDocRenderer.appendKDocSections import org.jetbrains.kotlin.idea.kdoc.KDocTemplate.DescriptionBodyTemplate -import org.jetbrains.kotlin.idea.references.KtDescriptorsBasedReference import org.jetbrains.kotlin.idea.references.resolveToDescriptors import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.resolve.ResolutionFacade @@ -182,7 +180,10 @@ defaultParameterValueRenderer = { (it.source.getPsi() as? KtParameter)?.defaultValue?.text ?: "..." } } - fun StringBuilder.renderKDoc(contentTag: KDocTag, sections: List<KDocSection>) { + private fun StringBuilder.renderKDoc( + contentTag: KDocTag, + sections: List<KDocSection> = if (contentTag is KDocSection) listOf(contentTag) else emptyList() + ) { insert(DescriptionBodyTemplate.Kotlin()) { content { appendKDocContent(contentTag) @@ -215,7 +216,7 @@ } if (!quickNavigation && kdoc != null) { description { - renderKDoc(kdoc.getDefaultSection(), listOf(kdoc.getDefaultSection())) + renderKDoc(kdoc.getDefaultSection()) } } } @@ -312,10 +313,9 @@ return mixKotlinToJava(declarationDescriptor, element, originalElement) } } - } else { - // This element was resolved to non-kotlin element, it will be rendered with own provider } + // This element was resolved to non-kotlin element, it will be rendered with own provider return null } @@ -384,11 +384,22 @@ if (!quickNavigation) { description { - val comment = declarationDescriptor.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(ktElement.project, it) } - if (comment != null) { - val sectionList = if (comment is KDocSection) listOf(comment) else emptyList() - renderKDoc(comment, sectionList) - } else if (declarationDescriptor is CallableDescriptor) { // If we couldn't find KDoc, try to find javadoc in one of super's + declarationDescriptor.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(ktElement.project, it) }?.let { + renderKDoc(it) + return@description + } + if (declarationDescriptor is ClassConstructorDescriptor && !declarationDescriptor.isPrimary) { + declarationDescriptor.constructedClass.findKDoc { + DescriptorToSourceUtilsIde.getAnyDeclaration( + ktElement.project, + it + ) + }?.let { + renderKDoc(it) + return@description + } + } + if (declarationDescriptor is CallableDescriptor) { // If we couldn't find KDoc, try to find javadoc in one of super's insert(DescriptionBodyTemplate.FromJava()) { body = extractJavaDescription(declarationDescriptor) }
diff --git a/idea/testData/editor/quickDoc/OnEmptySecondaryConstructor.kt b/idea/testData/editor/quickDoc/OnEmptySecondaryConstructor.kt new file mode 100644 index 0000000..f957bca --- /dev/null +++ b/idea/testData/editor/quickDoc/OnEmptySecondaryConstructor.kt
@@ -0,0 +1,17 @@ +/** +Documentation is here. + */ +class Foo { + private var x: Int = 0 + + constructor(x: Int) { + this.x = x + } //secondary constructor without documentation +} + +fun test() { + val f = Foo<caret>(10) +} +//INFO: <div class='definition'><pre><a href="psi_element://Foo"><code>Foo</code></a><br>public constructor <b>Foo</b>( +//INFO: x: Int +//INFO: )</pre></div><div class='content'><p>Documentation is here.</p></div><table class='sections'></table>
diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/quickDoc/QuickDocProviderTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/editor/quickDoc/QuickDocProviderTestGenerated.java index acc30f7..6447454 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/quickDoc/QuickDocProviderTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/quickDoc/QuickDocProviderTestGenerated.java
@@ -158,6 +158,11 @@ runTest("idea/testData/editor/quickDoc/OnClassDeclarationWithNoPackage.kt"); } + @TestMetadata("OnEmptySecondaryConstructor.kt") + public void testOnEmptySecondaryConstructor() throws Exception { + runTest("idea/testData/editor/quickDoc/OnEmptySecondaryConstructor.kt"); + } + @TestMetadata("OnEnumClassReference.kt") public void testOnEnumClassReference() throws Exception { runTest("idea/testData/editor/quickDoc/OnEnumClassReference.kt");