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