aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Kotlin/KotlinAsJavaDocumentationBuilder.kt
blob: be6dd2e174e1fc18106ceb7e59e74c2193d1ac27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package org.jetbrains.dokka

import com.google.inject.Inject
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiNamedElement
import org.jetbrains.dokka.Kotlin.DescriptorDocumentationParser
import org.jetbrains.kotlin.asJava.elements.KtLightElement
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPropertyAccessor

class KotlinAsJavaDocumentationBuilder
        @Inject constructor(val kotlinAsJavaDocumentationParser: KotlinAsJavaDocumentationParser) : PackageDocumentationBuilder
{
    override fun buildPackageDocumentation(documentationBuilder: DocumentationBuilder,
                                           packageName: FqName,
                                           packageNode: DocumentationNode,
                                           declarations: List<DeclarationDescriptor>,
                                           allFqNames: Collection<FqName>) {
        val project = documentationBuilder.resolutionFacade.project
        val psiPackage = JavaPsiFacade.getInstance(project).findPackage(packageName.asString())
        if (psiPackage == null) {
            documentationBuilder.logger.error("Cannot find Java package by qualified name: ${packageName.asString()}")
            return
        }

        val javaDocumentationBuilder = JavaPsiDocumentationBuilder(documentationBuilder.passConfiguration,
                documentationBuilder.refGraph,
                kotlinAsJavaDocumentationParser)

        psiPackage.classes.filter { it is KtLightElement<*, *> }.filter { it.isVisibleInDocumentation() }.forEach {
            javaDocumentationBuilder.appendClasses(packageNode, arrayOf(it))
        }
    }

    fun PsiClass.isVisibleInDocumentation(): Boolean {
        val origin: KtDeclaration = (this as KtLightElement<*, *>).kotlinOrigin as? KtDeclaration ?: return true

        return origin.hasModifier(KtTokens.INTERNAL_KEYWORD) != true &&
                origin.hasModifier(KtTokens.PRIVATE_KEYWORD) != true
    }
}

class KotlinAsJavaDocumentationParser
        @Inject constructor(val resolutionFacade: DokkaResolutionFacade,
                            val descriptorDocumentationParser: DescriptorDocumentationParser) : JavaDocumentationParser
{
    override fun parseDocumentation(element: PsiNamedElement): JavadocParseResult {
        val kotlinLightElement = element as? KtLightElement<*, *> ?: return JavadocParseResult.Empty
        val origin = kotlinLightElement.kotlinOrigin as? KtDeclaration ?: return JavadocParseResult.Empty
        if (origin is KtParameter) {
            // LazyDeclarationResolver does not support setter parameters
            val grandFather = origin.parent?.parent
            if (grandFather is KtPropertyAccessor) {
                return JavadocParseResult.Empty
            }
        }
        val descriptor = resolutionFacade.resolveToDescriptor(origin)
        val content = descriptorDocumentationParser.parseDocumentation(descriptor, origin is KtParameter)
        return JavadocParseResult(content, null)
    }
}