aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/main/kotlin/KotlinAsJavaDescriptorToDocumentationTranslator.kt
blob: 618ecfcc1fdf872727d113850c42dd88c44cc492 (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
67
68
69
70
71
72
73
74
75
76
package  org.jetbrains.dokka.kotlinAsJava

import org.jetbrains.dokka.analysis.DokkaResolutionFacade
import org.jetbrains.dokka.base.translators.descriptors.DRIWithPlatformInfo
import org.jetbrains.dokka.base.translators.descriptors.DokkaDescriptorVisitor
import org.jetbrains.dokka.base.translators.descriptors.withEmptyInfo
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.withClass
import org.jetbrains.dokka.model.*
import org.jetbrains.dokka.model.Function
import org.jetbrains.dokka.pages.PlatformData
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentationTranslator
import org.jetbrains.kotlin.descriptors.*

class KotlinAsJavaDescriptorToDocumentationTranslator(
    private val context: DokkaContext
) : DescriptorToDocumentationTranslator {
    override fun invoke(
        moduleName: String,
        packageFragments: Iterable<PackageFragmentDescriptor>,
        platformData: PlatformData
    ): Module =
        KotlinAsJavaDokkaDescriptorVisitor(platformData, context.platforms[platformData]?.facade!!).run {
            packageFragments.map { visitPackageFragmentDescriptor(it, DRI.topLevel.withEmptyInfo()) }
        }.let { Module(moduleName, it) }
}

class KotlinAsJavaDokkaDescriptorVisitor(
    platformData: PlatformData,
    resolutionFacade: DokkaResolutionFacade
) : DokkaDescriptorVisitor(platformData, resolutionFacade) {
    override fun visitPackageFragmentDescriptor(
        descriptor: PackageFragmentDescriptor,
        parent: DRIWithPlatformInfo
    ): Package {
        val dri = DRI(packageName = descriptor.fqName.asString())
        DescriptorCache.add(dri, descriptor)
        return super.visitPackageFragmentDescriptor(descriptor, parent)
    }

    override fun visitClassDescriptor(descriptor: ClassDescriptor, parent: DRIWithPlatformInfo): Classlike {
        val dri = parent.dri.withClass(descriptor.name.asString())
        DescriptorCache.add(dri, descriptor)
        return super.visitClassDescriptor(descriptor, parent)
    }

    override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, parent: DRIWithPlatformInfo): Property {
        val dri = parent.dri.copy(callable = Callable.from(descriptor))
        DescriptorCache.add(dri, descriptor)
        return super.visitPropertyDescriptor(descriptor, parent)
    }

    override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, parent: DRIWithPlatformInfo): Function {
        val dri = parent.dri.copy(callable = Callable.from(descriptor))
        DescriptorCache.add(dri, descriptor)
        return super.visitFunctionDescriptor(descriptor, parent)
    }

    override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, parent: DRIWithPlatformInfo): Function {
        val dri = parent.dri.copy(callable = Callable.from(descriptor))
        DescriptorCache.add(dri, descriptor)
        return super.visitConstructorDescriptor(descriptor, parent)
    }

    override fun visitPropertyAccessorDescriptor(
        descriptor: PropertyAccessorDescriptor,
        propertyDescriptor: PropertyDescriptor,
        parent: DRI
    ): Function {
        val dri = parent.copy(callable = Callable.from(descriptor))
        DescriptorCache.add(dri, descriptor)
        return super.visitPropertyAccessorDescriptor(descriptor, propertyDescriptor, parent)
    }
}