aboutsummaryrefslogtreecommitdiff
path: root/plugins/base/src/main/kotlin/resolvers/local/DefaultLocationProvider.kt
blob: 3647bfa70f2fdeb0f0ae10245e50aca280d7261d (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
77
package org.jetbrains.dokka.base.resolvers.local

import org.jetbrains.dokka.base.DokkaBase
import org.jetbrains.dokka.base.resolvers.external.DefaultExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.external.Dokka010ExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.external.ExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.external.javadoc.AndroidExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.external.javadoc.JavadocExternalLocationProvider
import org.jetbrains.dokka.base.resolvers.shared.ExternalDocumentation
import org.jetbrains.dokka.base.resolvers.shared.PackageList
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.model.DisplaySourceSet
import org.jetbrains.dokka.pages.RootPageNode
import org.jetbrains.dokka.plugability.DokkaContext
import org.jetbrains.dokka.plugability.plugin
import org.jetbrains.dokka.plugability.query

abstract class DefaultLocationProvider(
    protected val pageGraphRoot: RootPageNode,
    protected val dokkaContext: DokkaContext
) : LocationProvider {
    protected val externalLocationProviderFactories =
        dokkaContext.plugin<DokkaBase>().query { externalLocationProviderFactory }

    protected val externalLocationProviders: Map<ExternalDocumentation, ExternalLocationProvider?> = dokkaContext
            .configuration
            .sourceSets
            .flatMap { sourceSet ->
                sourceSet.externalDocumentationLinks.map {
                    PackageList.load(it.packageListUrl, sourceSet.jdkVersion, dokkaContext.configuration.offlineMode)
                            ?.let { packageList -> ExternalDocumentation(it.url, packageList) }
                }
            }
            .filterNotNull().associateWith { extDocInfo ->
                externalLocationProviderFactories
                    .mapNotNull { it.getExternalLocationProvider(extDocInfo) }
                    .firstOrNull()
                    ?: run { dokkaContext.logger.error("No ExternalLocationProvider for '${extDocInfo.packageList.url}' found"); null }
            }

    protected val packagesIndex: Map<String, ExternalLocationProvider?> =
        externalLocationProviders
            .flatMap { (extDocInfo, externalLocationProvider) ->
                extDocInfo.packageList.packages.map { packageName -> packageName to externalLocationProvider }
            }.groupBy { it.first }.mapValues { (_, lst) ->
                lst.map { it.second }
                    .sortedWith(compareBy(nullsLast(ExternalLocationProviderOrdering)) { it })
                    .firstOrNull()
            }
            .filterKeys(String::isNotBlank)


    protected val locationsIndex: Map<String, ExternalLocationProvider?> = externalLocationProviders
        .flatMap { (extDocInfo, externalLocationProvider) ->
            extDocInfo.packageList.locations.keys.map { relocatedDri -> relocatedDri to externalLocationProvider }
        }
        .toMap()
        .filterKeys(String::isNotBlank)

    protected open fun getExternalLocation(dri: DRI, sourceSets: Set<DisplaySourceSet>): String? =
        packagesIndex[dri.packageName]?.resolve(dri)
            ?: locationsIndex[dri.toString()]?.resolve(dri)
            ?: externalLocationProviders.values.mapNotNull { it?.resolve(dri) }.firstOrNull()

    private object ExternalLocationProviderOrdering : Comparator<ExternalLocationProvider> {
        private val desiredOrdering = listOf(
            DefaultExternalLocationProvider::class,
            Dokka010ExternalLocationProvider::class,
            AndroidExternalLocationProvider::class,
            JavadocExternalLocationProvider::class
        )

        override fun compare(o1: ExternalLocationProvider, o2: ExternalLocationProvider): Int =
            desiredOrdering.indexOf(o1::class).compareTo(desiredOrdering.indexOf(o2::class))
    }

}