blob: d8be9d5ad340f5e02b167cad1eda224de612eadc [file] [log] [blame]
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +04001package org.jetbrains.dokka
2
3import org.jetbrains.jet.lang.descriptors.*
Ilya Ryzhenkovc9ca0d82014-12-15 20:54:16 +03004import org.jetbrains.dokka.DocumentationNode.*
5import org.jetbrains.jet.lang.types.*
6import org.jetbrains.jet.lang.types.lang.*
7import org.jetbrains.jet.lang.resolve.scopes.*
8import org.jetbrains.jet.lang.resolve.name.*
9import org.jetbrains.jet.lang.resolve.lazy.*
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040010
Ilya Ryzhenkov471039e2014-10-12 22:37:07 +040011public data class DocumentationOptions(val includeNonPublic: Boolean = false)
12
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040013class DocumentationBuilder(val session: ResolveSession, val options: DocumentationOptions) {
Ilya Ryzhenkovba1f12d2014-10-12 23:25:12 +040014 val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040015 val descriptorToNode = hashMapOf<DeclarationDescriptor, DocumentationNode>()
16 val nodeToDescriptor = hashMapOf<DocumentationNode, DeclarationDescriptor>()
17 val links = hashMapOf<DocumentationNode, DeclarationDescriptor>()
18 val packages = hashMapOf<FqName, DocumentationNode>()
19
20 fun parseDocumentation(descriptor: DeclarationDescriptor): Content {
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040021 val docText = descriptor.getDocumentationElements().map { it.extractText() }.join("\n")
Ilya Ryzhenkovc9ca0d82014-12-15 20:54:16 +030022 val tree = parseMarkdown(docText)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040023 //println(tree.toTestString())
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040024 val content = buildContent(tree, descriptor)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040025 return content
26 }
27
28 fun link(node: DocumentationNode, descriptor: DeclarationDescriptor) {
29 links.put(node, descriptor)
30 }
31
32 fun register(descriptor: DeclarationDescriptor, node: DocumentationNode) {
33 descriptorToNode.put(descriptor, node)
34 nodeToDescriptor.put(node, descriptor)
35 }
36
37 fun DocumentationNode<T>(descriptor: T, kind: Kind): DocumentationNode where T : DeclarationDescriptor, T : Named {
38 val doc = parseDocumentation(descriptor)
39 val node = DocumentationNode(descriptor.getName().asString(), doc, kind)
40 if (descriptor is MemberDescriptor) {
Ilya Ryzhenkov92b82522014-10-14 19:14:13 +040041 node.appendVisibility(descriptor)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040042 if (descriptor !is ConstructorDescriptor) {
43 node.appendModality(descriptor)
44 }
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040045 }
46 return node
47 }
48
49 fun DocumentationNode.append(child: DocumentationNode, kind: DocumentationReference.Kind) {
50 addReferenceTo(child, kind)
51 when (kind) {
52 DocumentationReference.Kind.Detail -> child.addReferenceTo(this, DocumentationReference.Kind.Owner)
53 DocumentationReference.Kind.Member -> child.addReferenceTo(this, DocumentationReference.Kind.Owner)
54 DocumentationReference.Kind.Owner -> child.addReferenceTo(this, DocumentationReference.Kind.Member)
55 }
56 }
57
58 fun DocumentationNode.appendModality(descriptor: MemberDescriptor) {
59 val modifier = descriptor.getModality().name().toLowerCase()
60 val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier)
61 append(node, DocumentationReference.Kind.Detail)
62 }
63
64 fun DocumentationNode.appendVisibility(descriptor: DeclarationDescriptorWithVisibility) {
65 val modifier = descriptor.getVisibility().toString()
66 val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier)
67 append(node, DocumentationReference.Kind.Detail)
68 }
69
70 fun DocumentationNode.appendSupertypes(descriptor: ClassDescriptor) {
71 val superTypes = descriptor.getTypeConstructor().getSupertypes()
72 for (superType in superTypes) {
73 if (superType.toString() != "Any")
74 appendType(superType, DocumentationNode.Kind.Supertype)
75 }
76 }
77
78 fun DocumentationNode.appendProjection(projection: TypeProjection, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type) {
79 val prefix = when (projection.getProjectionKind()) {
80 Variance.IN_VARIANCE -> "in "
81 Variance.OUT_VARIANCE -> "out "
82 else -> ""
83 }
84 appendType(projection.getType(), kind, prefix)
85 }
86
87 fun DocumentationNode.appendType(jetType: JetType?, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type, prefix: String = "") {
88 if (jetType == null)
89 return
90 val classifierDescriptor = jetType.getConstructor().getDeclarationDescriptor()
91 val name = when (classifierDescriptor) {
Ilya Ryzhenkov5d0af0b2014-12-04 15:52:12 +030092 is Named -> prefix + classifierDescriptor.getName().asString() + if (jetType.isMarkedNullable()) "?" else ""
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +040093 else -> "<anonymous>"
94 }
95 val node = DocumentationNode(name, Content.Empty, kind)
96 if (classifierDescriptor != null)
97 link(node, classifierDescriptor)
98
99 append(node, DocumentationReference.Kind.Detail)
100 for (typeArgument in jetType.getArguments())
101 node.appendProjection(typeArgument)
102 }
103
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400104 fun DocumentationNode.appendChild(descriptor: DeclarationDescriptor, kind: DocumentationReference.Kind) {
105 // do not include generated code
106 if (descriptor is CallableMemberDescriptor && descriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION)
107 return
108
109 if (options.includeNonPublic
110 || descriptor !is MemberDescriptor
111 || descriptor.getVisibility() in visibleToDocumentation) {
112 append(descriptor.build(), kind)
113 }
114 }
115
116 fun DocumentationNode.appendChildren(descriptors: Iterable<DeclarationDescriptor>, kind: DocumentationReference.Kind) {
117 descriptors.forEach { descriptor -> appendChild(descriptor, kind) }
118 }
119
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +0400120 fun DocumentationNode.appendFragments(fragments: Collection<PackageFragmentDescriptor>) {
121 val descriptors = hashMapOf<String, List<DeclarationDescriptor>>()
122 for ((name, parts) in fragments.groupBy { it.fqName }) {
123 descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() })
Ilya Ryzhenkovba1f12d2014-10-12 23:25:12 +0400124 }
Ilya Ryzhenkov2ebfb982014-10-13 00:19:18 +0400125 for ((packageName, declarations) in descriptors) {
126 println(" package $packageName: ${declarations.count()} nodes")
127 val packageNode = DocumentationNode(packageName, Content.Empty, Kind.Package)
128 packageNode.appendChildren(declarations, DocumentationReference.Kind.Member)
129 append(packageNode, DocumentationReference.Kind.Member)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400130 }
131 }
132
133 fun DeclarationDescriptor.build(): DocumentationNode = when (this) {
134 is ClassDescriptor -> build()
135 is ConstructorDescriptor -> build()
136 is ScriptDescriptor -> build()
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400137 is PropertyDescriptor -> build()
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400138 is PropertyAccessorDescriptor -> build()
139 is FunctionDescriptor -> build()
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400140 is TypeParameterDescriptor -> build()
141 is ValueParameterDescriptor -> build()
142 is ReceiverParameterDescriptor -> build()
143 else -> throw IllegalStateException("Descriptor $this is not known")
144 }
145
146 fun ScriptDescriptor.build(): DocumentationNode = getClassDescriptor().build()
147 fun ClassDescriptor.build(): DocumentationNode {
148 val kind = when (getKind()) {
149 ClassKind.OBJECT -> Kind.Object
150 ClassKind.CLASS_OBJECT -> Kind.Object
151 ClassKind.TRAIT -> Kind.Interface
152 ClassKind.ENUM_CLASS -> Kind.Enum
153 ClassKind.ENUM_ENTRY -> Kind.EnumItem
154 else -> Kind.Class
155 }
156 val node = DocumentationNode(this, kind)
157 node.appendSupertypes(this)
158 if (getKind() != ClassKind.OBJECT) {
159 node.appendChildren(getTypeConstructor().getParameters(), DocumentationReference.Kind.Detail)
160 node.appendChildren(getConstructors(), DocumentationReference.Kind.Member)
161 val classObjectDescriptor = getClassObjectDescriptor()
162 if (classObjectDescriptor != null)
163 node.appendChild(classObjectDescriptor, DocumentationReference.Kind.Member)
164 }
165 node.appendChildren(getDefaultType().getMemberScope().getAllDescriptors(), DocumentationReference.Kind.Member)
166 register(this, node)
167 return node
168 }
169
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400170 fun ConstructorDescriptor.build(): DocumentationNode {
171 val node = DocumentationNode(this, Kind.Constructor)
172 node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail)
173 register(this, node)
174 return node
175 }
176
177 fun FunctionDescriptor.build(): DocumentationNode {
178 val node = DocumentationNode(this, Kind.Function)
179
180 node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail)
181 getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) }
182 node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail)
183 node.appendType(getReturnType())
184 register(this, node)
185 return node
186
187 }
188
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400189 fun PropertyAccessorDescriptor.build(): DocumentationNode {
190 val doc = parseDocumentation(this)
191 val specialName = getName().asString().drop(1).takeWhile { it != '-' }
192 val node = DocumentationNode(specialName, doc, Kind.PropertyAccessor)
193
194 node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail)
195 node.appendType(getReturnType())
196 register(this, node)
197 return node
198 }
199
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400200 fun PropertyDescriptor.build(): DocumentationNode {
201 val node = DocumentationNode(this, Kind.Property)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400202 node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail)
Ilya Ryzhenkov5efc1a32014-10-13 00:35:57 +0400203 getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) }
204 node.appendType(getReturnType())
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400205 getGetter()?.let {
206 if (!it.isDefault())
207 node.appendChild(it, DocumentationReference.Kind.Member)
208 }
209 getSetter()?.let {
210 if (!it.isDefault())
211 node.appendChild(it, DocumentationReference.Kind.Member)
212 }
213
214 register(this, node)
215 return node
216 }
217
218 fun ValueParameterDescriptor.build(): DocumentationNode {
219 val node = DocumentationNode(this, Kind.Parameter)
220 node.appendType(getType())
221 return node
222 }
223
224 fun TypeParameterDescriptor.build(): DocumentationNode {
225 val doc = parseDocumentation(this)
226 val name = getName().asString()
227 val prefix = when (getVariance()) {
228 Variance.IN_VARIANCE -> "in "
229 Variance.OUT_VARIANCE -> "out "
230 else -> ""
231 }
232
233 val node = DocumentationNode(prefix + name, doc, DocumentationNode.Kind.TypeParameter)
234
235 val builtIns = KotlinBuiltIns.getInstance()
236 for (constraint in getUpperBounds()) {
237 if (constraint == builtIns.getDefaultBound())
238 continue
239 val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.UpperBound)
240 node.append(constraintNode, DocumentationReference.Kind.Detail)
241 }
242
243 for (constraint in getLowerBounds()) {
Ilya Ryzhenkov21174162014-12-04 14:57:12 +0300244 if (KotlinBuiltIns.isNothing(constraint))
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400245 continue
246 val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.LowerBound)
247 node.append(constraintNode, DocumentationReference.Kind.Detail)
248 }
249 return node
250 }
251
252 fun ReceiverParameterDescriptor.build(): DocumentationNode {
Ilya Ryzhenkovba1f12d2014-10-12 23:25:12 +0400253 val node = DocumentationNode(getName().asString(), Content.Empty, Kind.Receiver)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400254 node.appendType(getType())
255 return node
256 }
257
258 /**
259 * Generates cross-references for documentation such as extensions for a type, inheritors, etc
260 *
261 * $receiver: [DocumentationContext] for node/descriptor resolutions
262 * $node: [DocumentationNode] to visit
263 */
264 public fun resolveReferences(node: DocumentationNode) {
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400265 if (node.kind != Kind.PropertyAccessor) {
266 node.details(DocumentationNode.Kind.Receiver).forEach { receiver ->
267 val receiverType = receiver.detail(DocumentationNode.Kind.Type)
268 val descriptor = links[receiverType]
269 if (descriptor != null) {
270 val typeNode = descriptorToNode[descriptor]
271 // if typeNode is null, extension is to external type like in a library
272 // should we create dummy node here?
273 typeNode?.addReferenceTo(node, DocumentationReference.Kind.Extension)
274 }
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400275 }
276 }
277 node.details(DocumentationNode.Kind.Supertype).forEach { detail ->
278 val descriptor = links[detail]
279 if (descriptor != null) {
280 val typeNode = descriptorToNode[descriptor]
281 typeNode?.addReferenceTo(node, DocumentationReference.Kind.Inheritor)
282 }
283 }
284 node.details.forEach { detail ->
285 val descriptor = links[detail]
286 if (descriptor != null) {
287 val typeNode = descriptorToNode[descriptor]
288 if (typeNode != null) {
289 detail.addReferenceTo(typeNode, DocumentationReference.Kind.Link)
290 }
291 }
292 }
293
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400294 resolveContentLinks(node, node.content)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400295
296 for (child in node.members) {
297 resolveReferences(child)
298 }
299 for (child in node.details) {
300 resolveReferences(child)
301 }
302 }
303
304 fun getResolutionScope(node: DocumentationNode): JetScope {
305 val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context")
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +0400306 return getResolutionScope(descriptor)
Ilya Ryzhenkov11355ce2014-10-12 22:35:47 +0400307 }
308
309 fun resolveContentLinks(node: DocumentationNode, content: ContentNode) {
310 val snapshot = content.children.toList()
311 for (child in snapshot) {
312 if (child is ContentExternalLink) {
313 val referenceText = child.href
314 if (Name.isValidIdentifier(referenceText)) {
315 val scope = getResolutionScope(node)
316 val symbolName = Name.guess(referenceText)
317 val symbol = scope.getLocalVariable(symbolName) ?:
318 scope.getProperties(symbolName).firstOrNull() ?:
319 scope.getFunctions(symbolName).firstOrNull() ?:
320 scope.getClassifier(symbolName)
321
322 if (symbol != null) {
323 val targetNode = descriptorToNode[symbol]
324 val contentLink = if (targetNode != null) ContentNodeLink(targetNode) else ContentExternalLink("#")
325
326 val index = content.children.indexOf(child)
327 content.children.remove(index)
328 contentLink.children.addAll(child.children)
329 content.children.add(index, contentLink)
330 }
331 }
332 }
333 resolveContentLinks(node, child)
334 }
335 }
336}