xref: /aosp_15_r20/external/dokka/core/src/main/kotlin/Formats/PackageListService.kt (revision 1b2d298c530bf0473cc943e8414a5ff577a994bc)

<lambda>null1 package org.jetbrains.dokka
2 
3 import com.google.inject.Inject
4 
5 
6 interface PackageListService {
7     fun formatPackageList(module: DocumentationModule): String
8 }
9 
10 class DefaultPackageListService @Inject constructor(
11         val generator: NodeLocationAwareGenerator,
12         val formatService: FormatService
13 ) : PackageListService {
14 
formatPackageListnull15     override fun formatPackageList(module: DocumentationModule): String {
16         val packages = mutableSetOf<String>()
17         val nonStandardLocations = mutableMapOf<String, String>()
18 
19         fun visit(node: DocumentationNode, relocated: Boolean = false) {
20             val nodeKind = node.kind
21 
22             when (nodeKind) {
23                 NodeKind.Package -> {
24                     packages.add(node.qualifiedName())
25                     node.members.forEach { visit(it) }
26                 }
27                 NodeKind.Signature -> {
28                     if (relocated)
29                         nonStandardLocations[node.name] = generator.relativePathToLocation(module, node.owner!!)
30                 }
31                 NodeKind.ExternalClass -> {
32                     node.members.forEach { visit(it, relocated = true) }
33                 }
34                 NodeKind.GroupNode -> {
35                     //only children of top-level GN records interesting for us, since link to top-level ones should point to GN
36                     node.members.forEach { it.members.forEach { visit(it, relocated = true) } }
37                     //record signature of GN as signature of type alias and class merged to GN, so link to it should point to GN
38                     node.detailOrNull(NodeKind.Signature)?.let { visit(it, relocated = true) }
39                 }
40                 else -> {
41                     if (nodeKind in NodeKind.classLike || nodeKind in NodeKind.memberLike) {
42                         node.details(NodeKind.Signature).forEach { visit(it, relocated) }
43                         node.members.forEach { visit(it, relocated) }
44                     }
45                 }
46             }
47         }
48 
49         module.members.forEach { visit(it) }
50 
51         return buildString {
52             appendln("\$dokka.linkExtension:${formatService.linkExtension}")
53 
54             nonStandardLocations.map { (signature, location) -> "\$dokka.location:$signature\u001f$location" }
55                     .sorted().joinTo(this, separator = "\n", postfix = "\n")
56 
57             packages.sorted().joinTo(this, separator = "\n", postfix = "\n")
58         }
59 
60     }
61 
62 }
63 
64