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