Fix Missing documentation inspection for primary ctor properties

#KT-20954 fixed
#KT-21005 fixed
This commit is contained in:
Simon Ogorodnik
2018-02-08 17:49:06 +03:00
parent 0f130a0a5a
commit 470a4bb615
9 changed files with 113 additions and 64 deletions
@@ -17,17 +17,19 @@
package org.jetbrains.kotlin.idea.inspections
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.isPropertyParameter
/**
* @returns string description of declaration, like Function ''name''
* @return string description of declaration, like `Function "describe"`
*/
fun KtNamedDeclaration.describe(): String? = when (this) {
is KtClass -> "Class ''$name''"
is KtObjectDeclaration -> "Object ''$name''"
is KtNamedFunction -> "Function ''$name''"
is KtClass -> "Class \"$name\""
is KtObjectDeclaration -> "Object \"$name\""
is KtNamedFunction -> "Function \"$name\""
is KtSecondaryConstructor -> "Constructor"
is KtProperty, is KtParameter -> "Property ''$name''"
is KtTypeParameter -> "Type parameter ''$name''"
is KtTypeAlias -> "Type alias ''$name''"
is KtProperty -> "Property \"$name\""
is KtParameter -> if (this.isPropertyParameter()) "Property \"$name\"" else "Parameter \"$name\""
is KtTypeParameter -> "Type parameter \"$name\""
is KtTypeAlias -> "Type alias \"$name\""
else -> null
}
@@ -14,7 +14,6 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import com.siyeh.ig.psiutils.TestUtils
import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility
import org.jetbrains.kotlin.descriptors.MemberDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.unblockDocument
@@ -30,27 +29,29 @@ import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
import org.jetbrains.kotlin.resolve.descriptorUtil.isEffectivelyPublicApi
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
class KDocMissingDocumentationInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor =
namedDeclarationVisitor { element ->
if (TestUtils.isInTestSourceContent(element)) {
return@namedDeclarationVisitor
}
val nameIdentifier = element.nameIdentifier
val descriptor = element.resolveToDescriptorIfAny(BodyResolveMode.FULL)
as? DeclarationDescriptorWithVisibility
as? MemberDescriptor ?: return@namedDeclarationVisitor
if (nameIdentifier != null && descriptor.isEffectivelyPublicApi) {
if (descriptor.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(element.project, it) } == null) {
namedDeclarationVisitor { element ->
if (TestUtils.isInTestSourceContent(element)) {
return@namedDeclarationVisitor
}
val nameIdentifier = element.nameIdentifier
if (nameIdentifier != null) {
if (element.findKDoc { DescriptorToSourceUtilsIde.getAnyDeclaration(element.project, it) } == null) {
val descriptor =
element.resolveToDescriptorIfAny() as? DeclarationDescriptorWithVisibility ?: return@namedDeclarationVisitor
if (descriptor.isEffectivelyPublicApi) {
val message = element.describe()?.let { "$it is missing documentation" } ?: "Missing documentation"
holder.registerProblem(nameIdentifier, message, AddDocumentationFix())
}
}
}
}
override fun runForWholeFile(): Boolean = true
class AddDocumentationFix : LocalQuickFix {
override fun getName(): String = "Add documentation"