• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.jetbrains.dokka
2 
3 import com.google.inject.Inject
4 import com.intellij.psi.JavaPsiFacade
5 import com.intellij.psi.PsiClass
6 import com.intellij.psi.PsiNamedElement
7 import org.jetbrains.dokka.Kotlin.DescriptorDocumentationParser
8 import org.jetbrains.kotlin.asJava.elements.KtLightElement
9 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
10 import org.jetbrains.kotlin.lexer.KtTokens
11 import org.jetbrains.kotlin.name.FqName
12 import org.jetbrains.kotlin.psi.KtDeclaration
13 import org.jetbrains.kotlin.psi.KtParameter
14 import org.jetbrains.kotlin.psi.KtPropertyAccessor
15 
16 class KotlinAsJavaDocumentationBuilder
17         @Inject constructor(val kotlinAsJavaDocumentationParser: KotlinAsJavaDocumentationParser) : PackageDocumentationBuilder
18 {
buildPackageDocumentationnull19     override fun buildPackageDocumentation(documentationBuilder: DocumentationBuilder,
20                                            packageName: FqName,
21                                            packageNode: DocumentationNode,
22                                            declarations: List<DeclarationDescriptor>,
23                                            allFqNames: Collection<FqName>) {
24         val project = documentationBuilder.resolutionFacade.project
25         val psiPackage = JavaPsiFacade.getInstance(project).findPackage(packageName.asString())
26         if (psiPackage == null) {
27             documentationBuilder.logger.error("Cannot find Java package by qualified name: ${packageName.asString()}")
28             return
29         }
30 
31         val javaDocumentationBuilder = JavaPsiDocumentationBuilder(documentationBuilder.options,
32                 documentationBuilder.refGraph,
33                 kotlinAsJavaDocumentationParser,
34                 documentationBuilder.linkResolver.externalDocumentationLinkResolver
35         )
36 
37         psiPackage.classes.filter { it is KtLightElement<*, *> }.filter { it.isVisibleInDocumentation() }.forEach {
38             javaDocumentationBuilder.appendClasses(packageNode, arrayOf(it))
39         }
40     }
41 
PsiClassnull42     fun PsiClass.isVisibleInDocumentation(): Boolean {
43         val origin: KtDeclaration = (this as KtLightElement<*, *>).kotlinOrigin as? KtDeclaration ?: return true
44 
45         return origin.hasModifier(KtTokens.INTERNAL_KEYWORD) != true &&
46                 origin.hasModifier(KtTokens.PRIVATE_KEYWORD) != true
47     }
48 }
49 
50 class KotlinAsJavaDocumentationParser
51         @Inject constructor(val resolutionFacade: DokkaResolutionFacade,
52                             val descriptorDocumentationParser: DescriptorDocumentationParser) : JavaDocumentationParser
53 {
parseDocumentationnull54     override fun parseDocumentation(element: PsiNamedElement): JavadocParseResult {
55         val kotlinLightElement = element as? KtLightElement<*, *> ?: return JavadocParseResult.Empty
56         val origin = kotlinLightElement.kotlinOrigin as? KtDeclaration ?: return JavadocParseResult.Empty
57         if (origin is KtParameter) {
58             // LazyDeclarationResolver does not support setter parameters
59             val grandFather = origin.parent?.parent
60             if (grandFather is KtPropertyAccessor) {
61                 return JavadocParseResult.Empty
62             }
63         }
64         val descriptor = resolutionFacade.resolveToDescriptor(origin)
65         val content = descriptorDocumentationParser.parseDocumentation(descriptor, origin is KtParameter)
66         return JavadocParseResult(content, null, emptyList(), null, null)
67     }
68 }
69