aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/parsers/moduleAndPackage/ModuleAndPackageDocumentationParsingContext.kt
blob: fa6c653ef369d9a44d0f5bd4f51145ce82849b48 (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
package org.jetbrains.dokka.base.parsers.moduleAndPackage

import org.jetbrains.dokka.analysis.DokkaResolutionFacade
import org.jetbrains.dokka.analysis.from
import org.jetbrains.dokka.base.parsers.MarkdownParser
import org.jetbrains.dokka.base.parsers.moduleAndPackage.ModuleAndPackageDocumentation.Classifier.Module
import org.jetbrains.dokka.base.parsers.moduleAndPackage.ModuleAndPackageDocumentation.Classifier.Package
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.doc.DocumentationNode
import org.jetbrains.dokka.utilities.DokkaLogger
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name

fun interface ModuleAndPackageDocumentationParsingContext {
    fun markdownParserFor(fragment: ModuleAndPackageDocumentationFragment, location: String): MarkdownParser
}

internal fun ModuleAndPackageDocumentationParsingContext.parse(
    fragment: ModuleAndPackageDocumentationFragment
): DocumentationNode {
    return markdownParserFor(fragment, fragment.source.sourceDescription).parse(fragment.documentation)
}

fun ModuleAndPackageDocumentationParsingContext(
    logger: DokkaLogger,
    facade: DokkaResolutionFacade? = null
) = ModuleAndPackageDocumentationParsingContext { fragment, sourceLocation ->
    val descriptor = when (fragment.classifier) {
        Module -> facade?.moduleDescriptor?.getPackage(FqName.topLevel(Name.identifier("")))
        Package -> facade?.moduleDescriptor?.getPackage(FqName(fragment.name))
    }

    val externalDri = { link: String ->
        try {
            if (facade != null && descriptor != null) {
                resolveKDocLink(
                    facade.resolveSession.bindingContext,
                    facade,
                    descriptor,
                    null,
                    link.split('.')
                ).sorted().firstOrNull()?.let { DRI.from(it) }
            } else null
        } catch (e1: IllegalArgumentException) {
            logger.warn("Couldn't resolve link for $link")
            null
        }
    }

    MarkdownParser(externalDri = externalDri, sourceLocation)
}

private fun Collection<DeclarationDescriptor>.sorted() = sortedWith(
    compareBy(
        { it is ClassDescriptor },
        { (it as? FunctionDescriptor)?.name },
        { (it as? FunctionDescriptor)?.valueParameters?.size },
        { (it as? FunctionDescriptor)?.valueParameters?.joinToString { it.type.toString() } }
    )
)