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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
package org.jetbrains.dokka.Generation
import org.jetbrains.dokka.*
class DocumentationMerger(
private val documentationModules: List<DocumentationModule>,
val logger: DokkaLogger
) {
private val producedNodeRefGraph: NodeReferenceGraph = NodeReferenceGraph()
private val signatureMap: Map<DocumentationNode, String>
private val oldToNewNodeMap: MutableMap<DocumentationNode, DocumentationNode> = mutableMapOf()
init {
if (documentationModules.groupBy { it.name }.size > 1) {
throw IllegalArgumentException("Modules should have similar names: ${documentationModules.joinToString(", ") {it.name}}")
}
signatureMap = documentationModules
.flatMap { it.nodeRefGraph.nodeMapView.entries }
.associate { (k, v) -> v to k }
documentationModules.map { it.nodeRefGraph }
.flatMap { it.references }
.forEach { producedNodeRefGraph.addReference(it) }
}
private fun mergePackageReferences(
from: DocumentationNode,
packages: List<DocumentationReference>
): List<DocumentationReference> {
val packagesByName = packages
.map { it.to }
.groupBy { it.name }
val resultReferences = mutableListOf<DocumentationReference>()
for ((name, listOfPackages) in packagesByName) {
try {
val producedPackage = mergePackagesWithEqualNames(name, from, listOfPackages)
updatePendingReferences()
resultReferences.add(
DocumentationReference(from, producedPackage, RefKind.Member)
)
} catch (t: Throwable) {
val entries = listOfPackages.joinToString(",") { "references:${it.allReferences().size}" }
throw Error("Failed to merge package $name from $from with entries $entries. ${t.message}", t)
}
}
return resultReferences
}
private fun mergePackagesWithEqualNames(
name: String,
from: DocumentationNode,
packages: List<DocumentationNode>
): DocumentationNode {
val mergedPackage = DocumentationNode(name, Content.Empty, NodeKind.Package)
for (contentToAppend in packages.map { it.content }.distinct()) {
mergedPackage.updateContent {
for (otherChild in contentToAppend.children) {
children.add(otherChild)
}
}
}
for (node in packages) {
oldToNewNodeMap[node] = mergedPackage
}
val references = packages.flatMap { it.allReferences() }
val mergedReferences = mergeReferences(mergedPackage, references)
for (ref in mergedReferences) {
if (ref.kind == RefKind.Owner) {
continue
}
mergedPackage.addReference(ref)
}
from.append(mergedPackage, RefKind.Member)
return mergedPackage
}
private fun mergeMemberGroupBy(it: DocumentationNode): String {
val signature = signatureMap[it]
if (signature != null) {
return signature
}
logger.error("Failed to find signature for $it in \n${it.allReferences().joinToString { "\n ${it.kind} ${it.to}" }}")
return "<ERROR>"
}
private fun mergeMemberReferences(
from: DocumentationNode,
refs: List<DocumentationReference>
): List<DocumentationReference> {
val membersBySignature: Map<String, List<DocumentationNode>> = refs.map { it.to }
.groupBy(this::mergeMemberGroupBy)
val mergedMembers: MutableList<DocumentationReference> = mutableListOf()
for ((signature, members) in membersBySignature) {
val newNode = mergeMembersWithEqualSignature(signature, members)
producedNodeRefGraph.register(signature, newNode)
updatePendingReferences()
from.append(newNode, RefKind.Member)
mergedMembers.add(DocumentationReference(from, newNode, RefKind.Member))
}
return mergedMembers
}
private fun mergeMembersWithEqualSignature(
signature: String,
nodes: List<DocumentationNode>
): DocumentationNode {
require(nodes.isNotEmpty())
val singleNode = nodes.singleOrNull()
if (singleNode != null) {
singleNode.dropReferences { it.kind == RefKind.Owner }
return singleNode
}
val nodeWithMaxPlatforms = nodes.maxBy { it.platforms.size }!!
val maxPlatforms = nodeWithMaxPlatforms.platforms.toSet()
val notContained = nodes.filterNot { maxPlatforms.containsAll(it.platforms) }
val reducedDuplicates = notContained + nodeWithMaxPlatforms
if (!reducedDuplicates.containsAll(nodes)) {
return mergeMembersWithEqualSignature(signature, reducedDuplicates)
}
val groupNode = DocumentationNode(nodes.first().name, Content.Empty, NodeKind.GroupNode)
groupNode.appendTextNode(signature, NodeKind.Signature, RefKind.Detail)
for (node in nodes) {
node.dropReferences { it.kind == RefKind.Owner }
groupNode.append(node, RefKind.Origin)
oldToNewNodeMap[node] = groupNode
}
// if nodes are classes, nested members should be also merged and
// inserted at the same level with class
if (nodes.all { it.kind == NodeKind.Class }) {
val members = nodes.flatMap { it.allReferences() }.filter { it.kind == RefKind.Member }
val mergedMembers = mergeMemberReferences(groupNode, members)
for (ref in mergedMembers) {
if (ref.kind == RefKind.Owner) {
continue
}
groupNode.append(ref.to, RefKind.Member)
}
}
return groupNode
}
private fun mergeReferences(
from: DocumentationNode,
refs: List<DocumentationReference>
): List<DocumentationReference> {
val (refsToPackages, otherRefs) = refs.partition { it.to.kind == NodeKind.Package }
val mergedPackages = mergePackageReferences(from, refsToPackages)
val (refsToMembers, refsNotToMembers) = otherRefs.partition { it.kind == RefKind.Member }
val mergedMembers = mergeMemberReferences(from, refsToMembers)
return mergedPackages + mergedMembers + refsNotToMembers
}
fun merge(): DocumentationModule {
val mergedDocumentationModule = DocumentationModule(
name = documentationModules.first().name,
nodeRefGraph = producedNodeRefGraph
)
val refs = documentationModules.flatMap {
it.allReferences()
}
mergeReferences(mergedDocumentationModule, refs)
return mergedDocumentationModule
}
private fun updatePendingReferences() {
for (ref in producedNodeRefGraph.references) {
ref.lazyNodeFrom.update()
ref.lazyNodeTo.update()
}
}
private fun NodeResolver.update() {
if (this is NodeResolver.Exact && exactNode in oldToNewNodeMap) {
exactNode = oldToNewNodeMap[exactNode]!!
}
}
}
|