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.* |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame] | 7 | import org.jetbrains.jet.lang.resolve.name.* |
| 8 | import org.jetbrains.jet.lang.resolve.lazy.* |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 9 | import org.jetbrains.jet.lang.descriptors.annotations.Annotated |
| 10 | import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 11 | import org.jetbrains.jet.lang.resolve.DescriptorUtils |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 12 | import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant |
| 13 | import com.intellij.openapi.util.text.StringUtil |
| 14 | import org.jetbrains.jet.lang.descriptors.impl.EnumEntrySyntheticClassDescriptor |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 15 | |
Ilya Ryzhenkov | 471039e | 2014-10-12 22:37:07 +0400 | [diff] [blame] | 16 | public data class DocumentationOptions(val includeNonPublic: Boolean = false) |
| 17 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 18 | private fun isSamePackage(descriptor1: DeclarationDescriptor, descriptor2: DeclarationDescriptor): Boolean { |
| 19 | val package1 = DescriptorUtils.getParentOfType(descriptor1, javaClass<PackageFragmentDescriptor>()) |
| 20 | val package2 = DescriptorUtils.getParentOfType(descriptor2, javaClass<PackageFragmentDescriptor>()) |
| 21 | return package1 != null && package2 != null && package1.fqName == package2.fqName |
| 22 | } |
| 23 | |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 24 | class DocumentationBuilder(val session: ResolveSession, val options: DocumentationOptions) { |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 25 | val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 26 | val descriptorToNode = hashMapOf<DeclarationDescriptor, DocumentationNode>() |
| 27 | val nodeToDescriptor = hashMapOf<DocumentationNode, DeclarationDescriptor>() |
| 28 | val links = hashMapOf<DocumentationNode, DeclarationDescriptor>() |
| 29 | val packages = hashMapOf<FqName, DocumentationNode>() |
| 30 | |
| 31 | fun parseDocumentation(descriptor: DeclarationDescriptor): Content { |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 32 | val docText = descriptor.getDocumentationElements().map { it.extractText() }.join("\n") |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame] | 33 | val tree = parseMarkdown(docText) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 34 | //println(tree.toTestString()) |
Ilya Ryzhenkov | ad14ea9 | 2014-10-13 18:22:02 +0400 | [diff] [blame] | 35 | val content = buildContent(tree, descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 36 | return content |
| 37 | } |
| 38 | |
| 39 | fun link(node: DocumentationNode, descriptor: DeclarationDescriptor) { |
| 40 | links.put(node, descriptor) |
| 41 | } |
| 42 | |
| 43 | fun register(descriptor: DeclarationDescriptor, node: DocumentationNode) { |
| 44 | descriptorToNode.put(descriptor, node) |
| 45 | nodeToDescriptor.put(node, descriptor) |
| 46 | } |
| 47 | |
| 48 | fun DocumentationNode<T>(descriptor: T, kind: Kind): DocumentationNode where T : DeclarationDescriptor, T : Named { |
| 49 | val doc = parseDocumentation(descriptor) |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 50 | val node = DocumentationNode(descriptor.getName().asString(), doc, kind).withModifiers(descriptor) |
| 51 | return node |
| 52 | } |
| 53 | |
| 54 | private fun DocumentationNode.withModifiers(descriptor: DeclarationDescriptor) : DocumentationNode{ |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 55 | if (descriptor is MemberDescriptor) { |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 56 | appendVisibility(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 57 | if (descriptor !is ConstructorDescriptor) { |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 58 | appendModality(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 59 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 60 | } |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 61 | return this |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | fun DocumentationNode.append(child: DocumentationNode, kind: DocumentationReference.Kind) { |
| 65 | addReferenceTo(child, kind) |
| 66 | when (kind) { |
| 67 | DocumentationReference.Kind.Detail -> child.addReferenceTo(this, DocumentationReference.Kind.Owner) |
| 68 | DocumentationReference.Kind.Member -> child.addReferenceTo(this, DocumentationReference.Kind.Owner) |
| 69 | DocumentationReference.Kind.Owner -> child.addReferenceTo(this, DocumentationReference.Kind.Member) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | fun DocumentationNode.appendModality(descriptor: MemberDescriptor) { |
| 74 | val modifier = descriptor.getModality().name().toLowerCase() |
| 75 | val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier) |
| 76 | append(node, DocumentationReference.Kind.Detail) |
| 77 | } |
| 78 | |
| 79 | fun DocumentationNode.appendVisibility(descriptor: DeclarationDescriptorWithVisibility) { |
| 80 | val modifier = descriptor.getVisibility().toString() |
| 81 | val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier) |
| 82 | append(node, DocumentationReference.Kind.Detail) |
| 83 | } |
| 84 | |
| 85 | fun DocumentationNode.appendSupertypes(descriptor: ClassDescriptor) { |
| 86 | val superTypes = descriptor.getTypeConstructor().getSupertypes() |
| 87 | for (superType in superTypes) { |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 88 | if (!ignoreSupertype(superType)) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 89 | appendType(superType, DocumentationNode.Kind.Supertype) |
| 90 | } |
| 91 | } |
| 92 | |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 93 | private fun ignoreSupertype(superType: JetType): Boolean { |
| 94 | val superClass = superType.getConstructor()?.getDeclarationDescriptor() as? ClassDescriptor |
| 95 | if (superClass != null) { |
| 96 | val fqName = DescriptorUtils.getFqNameSafe(superClass).asString() |
| 97 | return fqName == "kotlin.Annotation" || fqName == "kotlin.Enum" || fqName == "kotlin.Any" |
| 98 | } |
| 99 | return false |
| 100 | } |
| 101 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 102 | fun DocumentationNode.appendProjection(projection: TypeProjection, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type) { |
| 103 | val prefix = when (projection.getProjectionKind()) { |
| 104 | Variance.IN_VARIANCE -> "in " |
| 105 | Variance.OUT_VARIANCE -> "out " |
| 106 | else -> "" |
| 107 | } |
| 108 | appendType(projection.getType(), kind, prefix) |
| 109 | } |
| 110 | |
| 111 | fun DocumentationNode.appendType(jetType: JetType?, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type, prefix: String = "") { |
| 112 | if (jetType == null) |
| 113 | return |
| 114 | val classifierDescriptor = jetType.getConstructor().getDeclarationDescriptor() |
| 115 | val name = when (classifierDescriptor) { |
Ilya Ryzhenkov | 5d0af0b | 2014-12-04 15:52:12 +0300 | [diff] [blame] | 116 | is Named -> prefix + classifierDescriptor.getName().asString() + if (jetType.isMarkedNullable()) "?" else "" |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 117 | else -> "<anonymous>" |
| 118 | } |
| 119 | val node = DocumentationNode(name, Content.Empty, kind) |
| 120 | if (classifierDescriptor != null) |
| 121 | link(node, classifierDescriptor) |
| 122 | |
| 123 | append(node, DocumentationReference.Kind.Detail) |
| 124 | for (typeArgument in jetType.getArguments()) |
| 125 | node.appendProjection(typeArgument) |
| 126 | } |
| 127 | |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 128 | fun DocumentationNode.appendAnnotations(annotated: Annotated) { |
| 129 | annotated.getAnnotations().forEach { |
Dmitry Jemerov | e17eaa5 | 2015-01-09 20:59:58 +0100 | [diff] [blame^] | 130 | val annotationNode = it.build() |
| 131 | if (annotationNode != null) { |
| 132 | append(annotationNode, |
| 133 | if (annotationNode.name == "deprecated") DocumentationReference.Kind.Deprecation else DocumentationReference.Kind.Annotation) |
| 134 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 138 | fun DocumentationNode.appendChild(descriptor: DeclarationDescriptor, kind: DocumentationReference.Kind) { |
| 139 | // do not include generated code |
| 140 | if (descriptor is CallableMemberDescriptor && descriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION) |
| 141 | return |
| 142 | |
| 143 | if (options.includeNonPublic |
| 144 | || descriptor !is MemberDescriptor |
| 145 | || descriptor.getVisibility() in visibleToDocumentation) { |
| 146 | append(descriptor.build(), kind) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | fun DocumentationNode.appendChildren(descriptors: Iterable<DeclarationDescriptor>, kind: DocumentationReference.Kind) { |
| 151 | descriptors.forEach { descriptor -> appendChild(descriptor, kind) } |
| 152 | } |
| 153 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 154 | fun DocumentationNode.getParentForPackageMember(descriptor: DeclarationDescriptor, |
| 155 | externalClassNodes: MutableMap<FqName, DocumentationNode>): DocumentationNode { |
| 156 | if (descriptor is CallableMemberDescriptor) { |
| 157 | val extensionClassDescriptor = descriptor.getExtensionClassDescriptor() |
| 158 | if (extensionClassDescriptor != null && !isSamePackage(descriptor, extensionClassDescriptor)) { |
| 159 | val fqName = DescriptorUtils.getFqNameFromTopLevelClass(extensionClassDescriptor) |
| 160 | return externalClassNodes.getOrPut(fqName, { |
| 161 | val newNode = DocumentationNode(fqName.asString(), Content.Empty, Kind.ExternalClass) |
| 162 | append(newNode, DocumentationReference.Kind.Member) |
| 163 | newNode |
| 164 | }) |
| 165 | } |
| 166 | } |
| 167 | return this |
| 168 | } |
| 169 | |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 170 | fun DocumentationNode.appendFragments(fragments: Collection<PackageFragmentDescriptor>) { |
| 171 | val descriptors = hashMapOf<String, List<DeclarationDescriptor>>() |
| 172 | for ((name, parts) in fragments.groupBy { it.fqName }) { |
| 173 | descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() }) |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 174 | } |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 175 | for ((packageName, declarations) in descriptors) { |
| 176 | println(" package $packageName: ${declarations.count()} nodes") |
| 177 | val packageNode = DocumentationNode(packageName, Content.Empty, Kind.Package) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 178 | val externalClassNodes = hashMapOf<FqName, DocumentationNode>() |
| 179 | declarations.forEach { descriptor -> |
| 180 | val parent = packageNode.getParentForPackageMember(descriptor, externalClassNodes) |
| 181 | parent.appendChild(descriptor, DocumentationReference.Kind.Member) |
| 182 | } |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 183 | append(packageNode, DocumentationReference.Kind.Member) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | fun DeclarationDescriptor.build(): DocumentationNode = when (this) { |
| 188 | is ClassDescriptor -> build() |
| 189 | is ConstructorDescriptor -> build() |
| 190 | is ScriptDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 191 | is PropertyDescriptor -> build() |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 192 | is PropertyAccessorDescriptor -> build() |
| 193 | is FunctionDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 194 | is TypeParameterDescriptor -> build() |
| 195 | is ValueParameterDescriptor -> build() |
| 196 | is ReceiverParameterDescriptor -> build() |
| 197 | else -> throw IllegalStateException("Descriptor $this is not known") |
| 198 | } |
| 199 | |
| 200 | fun ScriptDescriptor.build(): DocumentationNode = getClassDescriptor().build() |
| 201 | fun ClassDescriptor.build(): DocumentationNode { |
| 202 | val kind = when (getKind()) { |
| 203 | ClassKind.OBJECT -> Kind.Object |
| 204 | ClassKind.CLASS_OBJECT -> Kind.Object |
| 205 | ClassKind.TRAIT -> Kind.Interface |
| 206 | ClassKind.ENUM_CLASS -> Kind.Enum |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 207 | ClassKind.ANNOTATION_CLASS -> Kind.AnnotationClass |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 208 | ClassKind.ENUM_ENTRY -> Kind.EnumItem |
| 209 | else -> Kind.Class |
| 210 | } |
| 211 | val node = DocumentationNode(this, kind) |
| 212 | node.appendSupertypes(this) |
| 213 | if (getKind() != ClassKind.OBJECT) { |
| 214 | node.appendChildren(getTypeConstructor().getParameters(), DocumentationReference.Kind.Detail) |
| 215 | node.appendChildren(getConstructors(), DocumentationReference.Kind.Member) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 216 | } |
| 217 | node.appendChildren(getDefaultType().getMemberScope().getAllDescriptors(), DocumentationReference.Kind.Member) |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 218 | val classObjectDescriptor = getClassObjectDescriptor() |
| 219 | if (classObjectDescriptor != null) { |
| 220 | node.appendChildren(classObjectDescriptor.getDefaultType().getMemberScope().getAllDescriptors(), |
| 221 | DocumentationReference.Kind.Member) |
| 222 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 223 | node.appendAnnotations(this) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 224 | register(this, node) |
| 225 | return node |
| 226 | } |
| 227 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 228 | fun ConstructorDescriptor.build(): DocumentationNode { |
| 229 | val node = DocumentationNode(this, Kind.Constructor) |
| 230 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 231 | register(this, node) |
| 232 | return node |
| 233 | } |
| 234 | |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 235 | private fun DeclarationDescriptor.inClassObject() = |
| 236 | getContainingDeclaration().let { it is ClassDescriptor && it.getKind() == ClassKind.CLASS_OBJECT } |
| 237 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 238 | fun CallableMemberDescriptor.getExtensionClassDescriptor(): ClassifierDescriptor? { |
| 239 | val extensionReceiver = getExtensionReceiverParameter() |
| 240 | if (extensionReceiver != null) { |
| 241 | val type = extensionReceiver.getType() |
| 242 | return type.getConstructor().getDeclarationDescriptor() as? ClassDescriptor |
| 243 | } |
| 244 | return null |
| 245 | } |
| 246 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 247 | fun FunctionDescriptor.build(): DocumentationNode { |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 248 | val node = DocumentationNode(this, if (inClassObject()) Kind.ClassObjectFunction else Kind.Function) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 249 | |
| 250 | node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail) |
| 251 | getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) } |
| 252 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 253 | node.appendType(getReturnType()) |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 254 | node.appendAnnotations(this) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 255 | register(this, node) |
| 256 | return node |
| 257 | |
| 258 | } |
| 259 | |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 260 | fun PropertyAccessorDescriptor.build(): DocumentationNode { |
| 261 | val doc = parseDocumentation(this) |
| 262 | val specialName = getName().asString().drop(1).takeWhile { it != '-' } |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 263 | val node = DocumentationNode(specialName, doc, Kind.PropertyAccessor).withModifiers(this) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 264 | |
| 265 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 266 | node.appendType(getReturnType()) |
| 267 | register(this, node) |
| 268 | return node |
| 269 | } |
| 270 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 271 | fun PropertyDescriptor.build(): DocumentationNode { |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 272 | val node = DocumentationNode(this, if (inClassObject()) Kind.ClassObjectProperty else Kind.Property) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 273 | node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail) |
Ilya Ryzhenkov | 5efc1a3 | 2014-10-13 00:35:57 +0400 | [diff] [blame] | 274 | getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) } |
| 275 | node.appendType(getReturnType()) |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 276 | node.appendAnnotations(this) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 277 | getGetter()?.let { |
| 278 | if (!it.isDefault()) |
| 279 | node.appendChild(it, DocumentationReference.Kind.Member) |
| 280 | } |
| 281 | getSetter()?.let { |
| 282 | if (!it.isDefault()) |
| 283 | node.appendChild(it, DocumentationReference.Kind.Member) |
| 284 | } |
| 285 | |
| 286 | register(this, node) |
| 287 | return node |
| 288 | } |
| 289 | |
| 290 | fun ValueParameterDescriptor.build(): DocumentationNode { |
| 291 | val node = DocumentationNode(this, Kind.Parameter) |
| 292 | node.appendType(getType()) |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 293 | node.appendAnnotations(this) |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 294 | register(this, node) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 295 | return node |
| 296 | } |
| 297 | |
| 298 | fun TypeParameterDescriptor.build(): DocumentationNode { |
| 299 | val doc = parseDocumentation(this) |
| 300 | val name = getName().asString() |
| 301 | val prefix = when (getVariance()) { |
| 302 | Variance.IN_VARIANCE -> "in " |
| 303 | Variance.OUT_VARIANCE -> "out " |
| 304 | else -> "" |
| 305 | } |
| 306 | |
| 307 | val node = DocumentationNode(prefix + name, doc, DocumentationNode.Kind.TypeParameter) |
| 308 | |
| 309 | val builtIns = KotlinBuiltIns.getInstance() |
| 310 | for (constraint in getUpperBounds()) { |
| 311 | if (constraint == builtIns.getDefaultBound()) |
| 312 | continue |
| 313 | val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.UpperBound) |
| 314 | node.append(constraintNode, DocumentationReference.Kind.Detail) |
| 315 | } |
| 316 | |
| 317 | for (constraint in getLowerBounds()) { |
Ilya Ryzhenkov | 2117416 | 2014-12-04 14:57:12 +0300 | [diff] [blame] | 318 | if (KotlinBuiltIns.isNothing(constraint)) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 319 | continue |
| 320 | val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.LowerBound) |
| 321 | node.append(constraintNode, DocumentationReference.Kind.Detail) |
| 322 | } |
| 323 | return node |
| 324 | } |
| 325 | |
| 326 | fun ReceiverParameterDescriptor.build(): DocumentationNode { |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 327 | val node = DocumentationNode(getName().asString(), Content.Empty, Kind.Receiver) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 328 | node.appendType(getType()) |
| 329 | return node |
| 330 | } |
| 331 | |
Dmitry Jemerov | 1a47943 | 2015-01-09 19:57:54 +0100 | [diff] [blame] | 332 | fun AnnotationDescriptor.build(): DocumentationNode? { |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 333 | val annotationClass = getType().getConstructor().getDeclarationDescriptor() |
Dmitry Jemerov | 1a47943 | 2015-01-09 19:57:54 +0100 | [diff] [blame] | 334 | if (ErrorUtils.isError(annotationClass)) { |
| 335 | return null |
| 336 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 337 | val node = DocumentationNode(annotationClass.getName().asString(), Content.Empty, DocumentationNode.Kind.Annotation) |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 338 | val arguments = getAllValueArguments().toList().sortBy { it.first.getIndex() } |
| 339 | arguments.forEach { |
| 340 | val valueNode = it.second.build() |
| 341 | if (valueNode != null) { |
| 342 | val paramNode = DocumentationNode(it.first.getName().asString(), Content.Empty, DocumentationNode.Kind.Parameter) |
| 343 | paramNode.append(valueNode, DocumentationReference.Kind.Detail) |
| 344 | node.append(paramNode, DocumentationReference.Kind.Detail) |
| 345 | } |
| 346 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 347 | return node |
| 348 | } |
| 349 | |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 350 | fun CompileTimeConstant<out Any?>.build(): DocumentationNode? { |
| 351 | val value = getValue() |
| 352 | val valueString = when(value) { |
| 353 | is String -> |
| 354 | "\"" + StringUtil.escapeStringCharacters(value) + "\"" |
| 355 | is EnumEntrySyntheticClassDescriptor -> |
| 356 | value.getContainingDeclaration().getName().asString() + "." + value.getName() |
| 357 | else -> value?.toString() |
| 358 | } |
| 359 | return if (valueString != null) DocumentationNode(valueString, Content.Empty, DocumentationNode.Kind.Value) else null |
| 360 | } |
| 361 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 362 | /** |
| 363 | * Generates cross-references for documentation such as extensions for a type, inheritors, etc |
| 364 | * |
| 365 | * $receiver: [DocumentationContext] for node/descriptor resolutions |
| 366 | * $node: [DocumentationNode] to visit |
| 367 | */ |
| 368 | public fun resolveReferences(node: DocumentationNode) { |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 369 | if (node.kind != Kind.PropertyAccessor) { |
| 370 | node.details(DocumentationNode.Kind.Receiver).forEach { receiver -> |
| 371 | val receiverType = receiver.detail(DocumentationNode.Kind.Type) |
| 372 | val descriptor = links[receiverType] |
| 373 | if (descriptor != null) { |
| 374 | val typeNode = descriptorToNode[descriptor] |
| 375 | // if typeNode is null, extension is to external type like in a library |
| 376 | // should we create dummy node here? |
| 377 | typeNode?.addReferenceTo(node, DocumentationReference.Kind.Extension) |
| 378 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | node.details(DocumentationNode.Kind.Supertype).forEach { detail -> |
| 382 | val descriptor = links[detail] |
| 383 | if (descriptor != null) { |
| 384 | val typeNode = descriptorToNode[descriptor] |
| 385 | typeNode?.addReferenceTo(node, DocumentationReference.Kind.Inheritor) |
| 386 | } |
| 387 | } |
| 388 | node.details.forEach { detail -> |
| 389 | val descriptor = links[detail] |
| 390 | if (descriptor != null) { |
| 391 | val typeNode = descriptorToNode[descriptor] |
| 392 | if (typeNode != null) { |
| 393 | detail.addReferenceTo(typeNode, DocumentationReference.Kind.Link) |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 398 | resolveContentLinks(node, node.content) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 399 | |
| 400 | for (child in node.members) { |
| 401 | resolveReferences(child) |
| 402 | } |
| 403 | for (child in node.details) { |
| 404 | resolveReferences(child) |
| 405 | } |
| 406 | } |
| 407 | |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 408 | fun getResolutionScope(node: DocumentationNode): DeclarationDescriptor { |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 409 | val descriptor = nodeToDescriptor[node] ?: throw IllegalArgumentException("Node is not known to this context") |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 410 | return descriptor |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | fun resolveContentLinks(node: DocumentationNode, content: ContentNode) { |
| 414 | val snapshot = content.children.toList() |
| 415 | for (child in snapshot) { |
| 416 | if (child is ContentExternalLink) { |
| 417 | val referenceText = child.href |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 418 | val symbol = resolveReference(getResolutionScope(node), referenceText) |
| 419 | if (symbol != null) { |
| 420 | val targetNode = descriptorToNode[symbol] |
| 421 | val contentLink = if (targetNode != null) ContentNodeLink(targetNode) else ContentExternalLink("#") |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 422 | |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 423 | val index = content.children.indexOf(child) |
| 424 | content.children.remove(index) |
| 425 | contentLink.children.addAll(child.children) |
| 426 | content.children.add(index, contentLink) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 427 | } |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 428 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 429 | } |
| 430 | resolveContentLinks(node, child) |
| 431 | } |
| 432 | } |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 433 | |
| 434 | private fun resolveReference(context: DeclarationDescriptor, reference: String): DeclarationDescriptor? { |
| 435 | if (Name.isValidIdentifier(reference)) { |
| 436 | val scope = getResolutionScope(context) |
| 437 | val symbolName = Name.guess(reference) |
| 438 | return scope.getLocalVariable(symbolName) ?: |
| 439 | scope.getProperties(symbolName).firstOrNull() ?: |
| 440 | scope.getFunctions(symbolName).firstOrNull() ?: |
| 441 | scope.getClassifier(symbolName) |
| 442 | |
| 443 | } |
| 444 | |
Ilya Ryzhenkov | 1839949 | 2014-12-22 09:50:17 +0200 | [diff] [blame] | 445 | if ("." !in reference) |
| 446 | return null |
| 447 | |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 448 | val names = reference.split('.') |
| 449 | val result = names.fold<String, DeclarationDescriptor?>(context) {(nextContext, name) -> |
| 450 | nextContext?.let { resolveReference(it, name) } |
| 451 | } |
| 452 | |
| 453 | return result |
| 454 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 455 | } |