aboutsummaryrefslogtreecommitdiff
path: root/src/Kotlin/KotlinAsJavaDocumentationBuilder.kt
blob: 8c6e5701ef6ba5512c3b2f5b68129d3a60e08cf8 (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
package org.jetbrains.dokka

import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiNamedElement
import org.jetbrains.kotlin.asJava.KotlinLightElement
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtParameter
import org.jetbrains.kotlin.psi.KtPropertyAccessor

class KotlinAsJavaDocumentationBuilder() : PackageDocumentationBuilder {
    override fun buildPackageDocumentation(documentationBuilder: DocumentationBuilder,
                                           packageName: FqName,
                                           packageNode: DocumentationNode,
                                           declarations: List<DeclarationDescriptor>) {
        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.options,
                documentationBuilder.refGraph,
                KotlinAsJavaDocumentationParser(documentationBuilder))

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

class KotlinAsJavaDocumentationParser(val documentationBuilder: DocumentationBuilder) : JavaDocumentationParser {
    override fun parseDocumentation(element: PsiNamedElement): JavadocParseResult {
        val kotlinLightElement = element as? KotlinLightElement<*, *> ?: return JavadocParseResult.Empty
        val origin = kotlinLightElement.getOrigin() ?: 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 = documentationBuilder.resolutionFacade.resolveToDescriptor(origin)
        val content = documentationBuilder.parseDocumentation(descriptor)
        return JavadocParseResult(content, null)
    }
}