Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 1 | package org.jetbrains.dokka |
| 2 | |
| 3 | import org.jetbrains.jet.lang.descriptors.* |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame^] | 4 | import org.jetbrains.dokka.DocumentationNode.* |
| 5 | import org.jetbrains.jet.lang.types.* |
| 6 | import org.jetbrains.jet.lang.types.lang.* |
| 7 | import org.jetbrains.jet.lang.resolve.scopes.* |
| 8 | import org.jetbrains.jet.lang.resolve.name.* |
| 9 | import org.jetbrains.jet.lang.resolve.lazy.* |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 10 | |
Ilya Ryzhenkov | 471039e | 2014-10-12 22:37:07 +0400 | [diff] [blame] | 11 | public data class DocumentationOptions(val includeNonPublic: Boolean = false) |
| 12 | |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 13 | class DocumentationBuilder(val session: ResolveSession, val options: DocumentationOptions) { |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 14 | val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 15 | 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 Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 21 | val docText = descriptor.getDocumentationElements().map { it.extractText() }.join("\n") |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame^] | 22 | val tree = parseMarkdown(docText) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 23 | //println(tree.toTestString()) |
Ilya Ryzhenkov | ad14ea9 | 2014-10-13 18:22:02 +0400 | [diff] [blame] | 24 | val content = buildContent(tree, descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 25 | 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 Ryzhenkov | 92b8252 | 2014-10-14 19:14:13 +0400 | [diff] [blame] | 41 | node.appendVisibility(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 42 | if (descriptor !is ConstructorDescriptor) { |
| 43 | node.appendModality(descriptor) |
| 44 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 45 | } |
| 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 Ryzhenkov | 5d0af0b | 2014-12-04 15:52:12 +0300 | [diff] [blame] | 92 | is Named -> prefix + classifierDescriptor.getName().asString() + if (jetType.isMarkedNullable()) "?" else "" |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 93 | 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 Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 104 | 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 Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 120 | 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 Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 124 | } |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 125 | 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 Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | fun DeclarationDescriptor.build(): DocumentationNode = when (this) { |
| 134 | is ClassDescriptor -> build() |
| 135 | is ConstructorDescriptor -> build() |
| 136 | is ScriptDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 137 | is PropertyDescriptor -> build() |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 138 | is PropertyAccessorDescriptor -> build() |
| 139 | is FunctionDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 140 | 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 Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 170 | 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 Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 189 | 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 Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 200 | fun PropertyDescriptor.build(): DocumentationNode { |
| 201 | val node = DocumentationNode(this, Kind.Property) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 202 | node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail) |
Ilya Ryzhenkov | 5efc1a3 | 2014-10-13 00:35:57 +0400 | [diff] [blame] | 203 | getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) } |
| 204 | node.appendType(getReturnType()) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 205 | 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 Ryzhenkov | 2117416 | 2014-12-04 14:57:12 +0300 | [diff] [blame] | 244 | if (KotlinBuiltIns.isNothing(constraint)) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 245 | 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 Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 253 | val node = DocumentationNode(getName().asString(), Content.Empty, Kind.Receiver) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 254 | 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 Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 265 | 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 Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 275 | } |
| 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 Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 294 | resolveContentLinks(node, node.content) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 295 | |
| 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 Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 306 | return getResolutionScope(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 307 | } |
| 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 | } |