Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 1 | package org.jetbrains.dokka |
| 2 | |
Alexander Udalov | 5dbadfe | 2015-01-12 12:36:54 +0300 | [diff] [blame] | 3 | import org.jetbrains.kotlin.descriptors.* |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame] | 4 | import org.jetbrains.dokka.DocumentationNode.* |
Alexander Udalov | 5dbadfe | 2015-01-12 12:36:54 +0300 | [diff] [blame] | 5 | import org.jetbrains.kotlin.types.* |
| 6 | import org.jetbrains.kotlin.builtins.* |
| 7 | import org.jetbrains.kotlin.name.* |
| 8 | import org.jetbrains.kotlin.resolve.lazy.* |
Dmitry Jemerov | 7158019 | 2015-01-12 17:27:41 +0100 | [diff] [blame] | 9 | import org.jetbrains.kotlin.resolve.DescriptorUtils |
| 10 | import org.jetbrains.kotlin.descriptors.annotations.Annotated |
| 11 | import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor |
| 12 | import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 13 | import com.intellij.openapi.util.text.StringUtil |
Dmitry Jemerov | 7158019 | 2015-01-12 17:27:41 +0100 | [diff] [blame] | 14 | import org.jetbrains.kotlin.descriptors.impl.EnumEntrySyntheticClassDescriptor |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 15 | import java.io.File |
| 16 | import com.intellij.psi.PsiDocumentManager |
| 17 | import com.intellij.psi.PsiNameIdentifierOwner |
| 18 | import com.intellij.psi.PsiElement |
Dmitry Jemerov | 807451f | 2015-01-14 20:30:54 +0100 | [diff] [blame] | 19 | import org.jetbrains.kotlin.resolve.source.getPsi |
Dmitry Jemerov | f36d9b0 | 2015-01-14 19:38:58 +0100 | [diff] [blame] | 20 | import org.jetbrains.kotlin.psi.JetParameter |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 21 | |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 22 | public data class DocumentationOptions(val includeNonPublic: Boolean = false, |
| 23 | val sourceLinks: List<SourceLinkDefinition>) |
Ilya Ryzhenkov | 471039e | 2014-10-12 22:37:07 +0400 | [diff] [blame] | 24 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 25 | private fun isSamePackage(descriptor1: DeclarationDescriptor, descriptor2: DeclarationDescriptor): Boolean { |
| 26 | val package1 = DescriptorUtils.getParentOfType(descriptor1, javaClass<PackageFragmentDescriptor>()) |
| 27 | val package2 = DescriptorUtils.getParentOfType(descriptor2, javaClass<PackageFragmentDescriptor>()) |
| 28 | return package1 != null && package2 != null && package1.fqName == package2.fqName |
| 29 | } |
| 30 | |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 31 | class DocumentationBuilder(val session: ResolveSession, val options: DocumentationOptions) { |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 32 | val visibleToDocumentation = setOf(Visibilities.INTERNAL, Visibilities.PROTECTED, Visibilities.PUBLIC) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 33 | val descriptorToNode = hashMapOf<DeclarationDescriptor, DocumentationNode>() |
| 34 | val nodeToDescriptor = hashMapOf<DocumentationNode, DeclarationDescriptor>() |
| 35 | val links = hashMapOf<DocumentationNode, DeclarationDescriptor>() |
| 36 | val packages = hashMapOf<FqName, DocumentationNode>() |
| 37 | |
| 38 | fun parseDocumentation(descriptor: DeclarationDescriptor): Content { |
Ilya Ryzhenkov | 1cb3af9 | 2014-10-13 20:14:45 +0400 | [diff] [blame] | 39 | val docText = descriptor.getDocumentationElements().map { it.extractText() }.join("\n") |
Ilya Ryzhenkov | c9ca0d8 | 2014-12-15 20:54:16 +0300 | [diff] [blame] | 40 | val tree = parseMarkdown(docText) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 41 | //println(tree.toTestString()) |
Ilya Ryzhenkov | ad14ea9 | 2014-10-13 18:22:02 +0400 | [diff] [blame] | 42 | val content = buildContent(tree, descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 43 | return content |
| 44 | } |
| 45 | |
| 46 | fun link(node: DocumentationNode, descriptor: DeclarationDescriptor) { |
| 47 | links.put(node, descriptor) |
| 48 | } |
| 49 | |
| 50 | fun register(descriptor: DeclarationDescriptor, node: DocumentationNode) { |
| 51 | descriptorToNode.put(descriptor, node) |
| 52 | nodeToDescriptor.put(node, descriptor) |
| 53 | } |
| 54 | |
| 55 | fun DocumentationNode<T>(descriptor: T, kind: Kind): DocumentationNode where T : DeclarationDescriptor, T : Named { |
| 56 | val doc = parseDocumentation(descriptor) |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 57 | val node = DocumentationNode(descriptor.getName().asString(), doc, kind).withModifiers(descriptor) |
| 58 | return node |
| 59 | } |
| 60 | |
| 61 | private fun DocumentationNode.withModifiers(descriptor: DeclarationDescriptor) : DocumentationNode{ |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 62 | if (descriptor is MemberDescriptor) { |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 63 | appendVisibility(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 64 | if (descriptor !is ConstructorDescriptor) { |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 65 | appendModality(descriptor) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 66 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 67 | } |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 68 | return this |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | fun DocumentationNode.append(child: DocumentationNode, kind: DocumentationReference.Kind) { |
| 72 | addReferenceTo(child, kind) |
| 73 | when (kind) { |
| 74 | DocumentationReference.Kind.Detail -> child.addReferenceTo(this, DocumentationReference.Kind.Owner) |
| 75 | DocumentationReference.Kind.Member -> child.addReferenceTo(this, DocumentationReference.Kind.Owner) |
| 76 | DocumentationReference.Kind.Owner -> child.addReferenceTo(this, DocumentationReference.Kind.Member) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fun DocumentationNode.appendModality(descriptor: MemberDescriptor) { |
Dmitry Jemerov | 6d23430 | 2015-01-13 17:15:52 +0100 | [diff] [blame] | 81 | var modality = descriptor.getModality() |
| 82 | if (modality == Modality.OPEN) { |
| 83 | val containingClass = descriptor.getContainingDeclaration() as? ClassDescriptor |
| 84 | if (containingClass?.getModality() == Modality.FINAL) { |
| 85 | modality = Modality.FINAL |
| 86 | } |
| 87 | } |
| 88 | val modifier = modality.name().toLowerCase() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 89 | val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier) |
| 90 | append(node, DocumentationReference.Kind.Detail) |
| 91 | } |
| 92 | |
| 93 | fun DocumentationNode.appendVisibility(descriptor: DeclarationDescriptorWithVisibility) { |
| 94 | val modifier = descriptor.getVisibility().toString() |
| 95 | val node = DocumentationNode(modifier, Content.Empty, DocumentationNode.Kind.Modifier) |
| 96 | append(node, DocumentationReference.Kind.Detail) |
| 97 | } |
| 98 | |
| 99 | fun DocumentationNode.appendSupertypes(descriptor: ClassDescriptor) { |
| 100 | val superTypes = descriptor.getTypeConstructor().getSupertypes() |
| 101 | for (superType in superTypes) { |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 102 | if (!ignoreSupertype(superType)) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 103 | appendType(superType, DocumentationNode.Kind.Supertype) |
| 104 | } |
| 105 | } |
| 106 | |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 107 | private fun ignoreSupertype(superType: JetType): Boolean { |
| 108 | val superClass = superType.getConstructor()?.getDeclarationDescriptor() as? ClassDescriptor |
| 109 | if (superClass != null) { |
| 110 | val fqName = DescriptorUtils.getFqNameSafe(superClass).asString() |
| 111 | return fqName == "kotlin.Annotation" || fqName == "kotlin.Enum" || fqName == "kotlin.Any" |
| 112 | } |
| 113 | return false |
| 114 | } |
| 115 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 116 | fun DocumentationNode.appendProjection(projection: TypeProjection, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type) { |
| 117 | val prefix = when (projection.getProjectionKind()) { |
| 118 | Variance.IN_VARIANCE -> "in " |
| 119 | Variance.OUT_VARIANCE -> "out " |
| 120 | else -> "" |
| 121 | } |
| 122 | appendType(projection.getType(), kind, prefix) |
| 123 | } |
| 124 | |
| 125 | fun DocumentationNode.appendType(jetType: JetType?, kind: DocumentationNode.Kind = DocumentationNode.Kind.Type, prefix: String = "") { |
| 126 | if (jetType == null) |
| 127 | return |
| 128 | val classifierDescriptor = jetType.getConstructor().getDeclarationDescriptor() |
| 129 | val name = when (classifierDescriptor) { |
Ilya Ryzhenkov | 5d0af0b | 2014-12-04 15:52:12 +0300 | [diff] [blame] | 130 | is Named -> prefix + classifierDescriptor.getName().asString() + if (jetType.isMarkedNullable()) "?" else "" |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 131 | else -> "<anonymous>" |
| 132 | } |
| 133 | val node = DocumentationNode(name, Content.Empty, kind) |
| 134 | if (classifierDescriptor != null) |
| 135 | link(node, classifierDescriptor) |
| 136 | |
| 137 | append(node, DocumentationReference.Kind.Detail) |
| 138 | for (typeArgument in jetType.getArguments()) |
| 139 | node.appendProjection(typeArgument) |
| 140 | } |
| 141 | |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 142 | fun DocumentationNode.appendAnnotations(annotated: Annotated) { |
| 143 | annotated.getAnnotations().forEach { |
Dmitry Jemerov | e17eaa5 | 2015-01-09 20:59:58 +0100 | [diff] [blame] | 144 | val annotationNode = it.build() |
| 145 | if (annotationNode != null) { |
| 146 | append(annotationNode, |
| 147 | if (annotationNode.name == "deprecated") DocumentationReference.Kind.Deprecation else DocumentationReference.Kind.Annotation) |
| 148 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 152 | fun DocumentationNode.appendSourceLink(sourceElement: SourceElement) { |
| 153 | val psi = getTargetElement(sourceElement) |
| 154 | val path = psi?.getContainingFile()?.getVirtualFile()?.getPath() |
| 155 | if (path == null) { |
| 156 | return |
| 157 | } |
| 158 | val absPath = File(path).getAbsolutePath() |
| 159 | val linkDef = findSourceLinkDefinition(absPath) |
| 160 | if (linkDef != null) { |
| 161 | var url = linkDef.url + path.substring(linkDef.path.length()) |
| 162 | if (linkDef.lineSuffix != null) { |
| 163 | val doc = PsiDocumentManager.getInstance(psi!!.getProject()).getDocument(psi.getContainingFile()) |
| 164 | if (doc != null) { |
| 165 | // IJ uses 0-based line-numbers; external source browsers use 1-based |
| 166 | val line = doc.getLineNumber(psi.getTextRange().getStartOffset()) + 1 |
| 167 | url += linkDef.lineSuffix + line.toString() |
| 168 | } |
| 169 | } |
| 170 | append(DocumentationNode(url, Content.Empty, DocumentationNode.Kind.SourceUrl), |
| 171 | DocumentationReference.Kind.Detail); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private fun getTargetElement(sourceElement: SourceElement): PsiElement? { |
| 176 | val psi = sourceElement.getPsi() |
| 177 | return if (psi is PsiNameIdentifierOwner) psi.getNameIdentifier() else psi |
| 178 | } |
| 179 | |
| 180 | fun findSourceLinkDefinition(path: String) = options.sourceLinks.firstOrNull { path.startsWith(it.path) } |
| 181 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 182 | fun DocumentationNode.appendChild(descriptor: DeclarationDescriptor, kind: DocumentationReference.Kind) { |
| 183 | // do not include generated code |
| 184 | if (descriptor is CallableMemberDescriptor && descriptor.getKind() != CallableMemberDescriptor.Kind.DECLARATION) |
| 185 | return |
| 186 | |
| 187 | if (options.includeNonPublic |
| 188 | || descriptor !is MemberDescriptor |
| 189 | || descriptor.getVisibility() in visibleToDocumentation) { |
| 190 | append(descriptor.build(), kind) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fun DocumentationNode.appendChildren(descriptors: Iterable<DeclarationDescriptor>, kind: DocumentationReference.Kind) { |
| 195 | descriptors.forEach { descriptor -> appendChild(descriptor, kind) } |
| 196 | } |
| 197 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 198 | fun DocumentationNode.getParentForPackageMember(descriptor: DeclarationDescriptor, |
| 199 | externalClassNodes: MutableMap<FqName, DocumentationNode>): DocumentationNode { |
| 200 | if (descriptor is CallableMemberDescriptor) { |
| 201 | val extensionClassDescriptor = descriptor.getExtensionClassDescriptor() |
| 202 | if (extensionClassDescriptor != null && !isSamePackage(descriptor, extensionClassDescriptor)) { |
| 203 | val fqName = DescriptorUtils.getFqNameFromTopLevelClass(extensionClassDescriptor) |
| 204 | return externalClassNodes.getOrPut(fqName, { |
| 205 | val newNode = DocumentationNode(fqName.asString(), Content.Empty, Kind.ExternalClass) |
| 206 | append(newNode, DocumentationReference.Kind.Member) |
| 207 | newNode |
| 208 | }) |
| 209 | } |
| 210 | } |
| 211 | return this |
| 212 | } |
| 213 | |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 214 | fun DocumentationNode.appendFragments(fragments: Collection<PackageFragmentDescriptor>) { |
| 215 | val descriptors = hashMapOf<String, List<DeclarationDescriptor>>() |
| 216 | for ((name, parts) in fragments.groupBy { it.fqName }) { |
| 217 | descriptors.put(name.asString(), parts.flatMap { it.getMemberScope().getAllDescriptors() }) |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 218 | } |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 219 | for ((packageName, declarations) in descriptors) { |
Dmitry Jemerov | c5fc45c | 2015-01-12 13:25:58 +0100 | [diff] [blame] | 220 | println(" package $packageName: ${declarations.count()} declarations") |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 221 | val packageNode = DocumentationNode(packageName, Content.Empty, Kind.Package) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 222 | val externalClassNodes = hashMapOf<FqName, DocumentationNode>() |
| 223 | declarations.forEach { descriptor -> |
| 224 | val parent = packageNode.getParentForPackageMember(descriptor, externalClassNodes) |
| 225 | parent.appendChild(descriptor, DocumentationReference.Kind.Member) |
| 226 | } |
Ilya Ryzhenkov | 2ebfb98 | 2014-10-13 00:19:18 +0400 | [diff] [blame] | 227 | append(packageNode, DocumentationReference.Kind.Member) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | fun DeclarationDescriptor.build(): DocumentationNode = when (this) { |
| 232 | is ClassDescriptor -> build() |
| 233 | is ConstructorDescriptor -> build() |
| 234 | is ScriptDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 235 | is PropertyDescriptor -> build() |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 236 | is PropertyAccessorDescriptor -> build() |
| 237 | is FunctionDescriptor -> build() |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 238 | is TypeParameterDescriptor -> build() |
| 239 | is ValueParameterDescriptor -> build() |
| 240 | is ReceiverParameterDescriptor -> build() |
| 241 | else -> throw IllegalStateException("Descriptor $this is not known") |
| 242 | } |
| 243 | |
| 244 | fun ScriptDescriptor.build(): DocumentationNode = getClassDescriptor().build() |
| 245 | fun ClassDescriptor.build(): DocumentationNode { |
| 246 | val kind = when (getKind()) { |
| 247 | ClassKind.OBJECT -> Kind.Object |
| 248 | ClassKind.CLASS_OBJECT -> Kind.Object |
| 249 | ClassKind.TRAIT -> Kind.Interface |
| 250 | ClassKind.ENUM_CLASS -> Kind.Enum |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 251 | ClassKind.ANNOTATION_CLASS -> Kind.AnnotationClass |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 252 | ClassKind.ENUM_ENTRY -> Kind.EnumItem |
| 253 | else -> Kind.Class |
| 254 | } |
| 255 | val node = DocumentationNode(this, kind) |
| 256 | node.appendSupertypes(this) |
Dmitry Jemerov | d33f5b8 | 2015-01-12 17:49:34 +0100 | [diff] [blame] | 257 | if (getKind() != ClassKind.OBJECT && getKind() != ClassKind.ENUM_ENTRY) { |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 258 | node.appendChildren(getTypeConstructor().getParameters(), DocumentationReference.Kind.Detail) |
Dmitry Jemerov | 1ce5373 | 2015-01-13 16:19:42 +0100 | [diff] [blame] | 259 | val constructorsToDocument = if (getKind() == ClassKind.ENUM_CLASS) |
| 260 | getConstructors().filter { it.getValueParameters().size() > 0 } |
| 261 | else |
| 262 | getConstructors() |
| 263 | node.appendChildren(constructorsToDocument, DocumentationReference.Kind.Member) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 264 | } |
| 265 | node.appendChildren(getDefaultType().getMemberScope().getAllDescriptors(), DocumentationReference.Kind.Member) |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 266 | val classObjectDescriptor = getClassObjectDescriptor() |
| 267 | if (classObjectDescriptor != null) { |
| 268 | node.appendChildren(classObjectDescriptor.getDefaultType().getMemberScope().getAllDescriptors(), |
| 269 | DocumentationReference.Kind.Member) |
| 270 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 271 | node.appendAnnotations(this) |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 272 | node.appendSourceLink(getSource()) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 273 | register(this, node) |
| 274 | return node |
| 275 | } |
| 276 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 277 | fun ConstructorDescriptor.build(): DocumentationNode { |
| 278 | val node = DocumentationNode(this, Kind.Constructor) |
| 279 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 280 | register(this, node) |
| 281 | return node |
| 282 | } |
| 283 | |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 284 | private fun DeclarationDescriptor.inClassObject() = |
| 285 | getContainingDeclaration().let { it is ClassDescriptor && it.getKind() == ClassKind.CLASS_OBJECT } |
| 286 | |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 287 | fun CallableMemberDescriptor.getExtensionClassDescriptor(): ClassifierDescriptor? { |
| 288 | val extensionReceiver = getExtensionReceiverParameter() |
| 289 | if (extensionReceiver != null) { |
| 290 | val type = extensionReceiver.getType() |
| 291 | return type.getConstructor().getDeclarationDescriptor() as? ClassDescriptor |
| 292 | } |
| 293 | return null |
| 294 | } |
| 295 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 296 | fun FunctionDescriptor.build(): DocumentationNode { |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 297 | val node = DocumentationNode(this, if (inClassObject()) Kind.ClassObjectFunction else Kind.Function) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 298 | |
| 299 | node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail) |
| 300 | getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) } |
| 301 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 302 | node.appendType(getReturnType()) |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 303 | node.appendAnnotations(this) |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 304 | node.appendSourceLink(getSource()) |
| 305 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 306 | register(this, node) |
| 307 | return node |
| 308 | |
| 309 | } |
| 310 | |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 311 | fun PropertyAccessorDescriptor.build(): DocumentationNode { |
| 312 | val doc = parseDocumentation(this) |
| 313 | val specialName = getName().asString().drop(1).takeWhile { it != '-' } |
Ilya Ryzhenkov | c759876 | 2014-12-22 17:38:56 +0300 | [diff] [blame] | 314 | val node = DocumentationNode(specialName, doc, Kind.PropertyAccessor).withModifiers(this) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 315 | |
| 316 | node.appendChildren(getValueParameters(), DocumentationReference.Kind.Detail) |
| 317 | node.appendType(getReturnType()) |
| 318 | register(this, node) |
| 319 | return node |
| 320 | } |
| 321 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 322 | fun PropertyDescriptor.build(): DocumentationNode { |
Dmitry Jemerov | cedaeb4 | 2014-12-29 20:50:26 +0100 | [diff] [blame] | 323 | val node = DocumentationNode(this, if (inClassObject()) Kind.ClassObjectProperty else Kind.Property) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 324 | node.appendChildren(getTypeParameters(), DocumentationReference.Kind.Detail) |
Ilya Ryzhenkov | 5efc1a3 | 2014-10-13 00:35:57 +0400 | [diff] [blame] | 325 | getExtensionReceiverParameter()?.let { node.appendChild(it, DocumentationReference.Kind.Detail) } |
| 326 | node.appendType(getReturnType()) |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 327 | node.appendAnnotations(this) |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 328 | node.appendSourceLink(getSource()) |
Dmitry Jemerov | faad901 | 2015-01-14 14:00:55 +0100 | [diff] [blame] | 329 | if (isVar()) { |
| 330 | node.append(DocumentationNode("var", Content.Empty, DocumentationNode.Kind.Modifier), |
| 331 | DocumentationReference.Kind.Detail) |
| 332 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 333 | getGetter()?.let { |
| 334 | if (!it.isDefault()) |
| 335 | node.appendChild(it, DocumentationReference.Kind.Member) |
| 336 | } |
| 337 | getSetter()?.let { |
| 338 | if (!it.isDefault()) |
| 339 | node.appendChild(it, DocumentationReference.Kind.Member) |
| 340 | } |
| 341 | |
| 342 | register(this, node) |
| 343 | return node |
| 344 | } |
| 345 | |
| 346 | fun ValueParameterDescriptor.build(): DocumentationNode { |
| 347 | val node = DocumentationNode(this, Kind.Parameter) |
Dmitry Jemerov | 2e277ba | 2015-01-13 16:38:41 +0100 | [diff] [blame] | 348 | val varargType = getVarargElementType() |
| 349 | if (varargType != null) { |
| 350 | node.append(DocumentationNode("vararg", Content.Empty, Kind.Annotation), DocumentationReference.Kind.Annotation) |
| 351 | node.appendType(varargType) |
| 352 | } else { |
| 353 | node.appendType(getType()) |
| 354 | } |
Dmitry Jemerov | f36d9b0 | 2015-01-14 19:38:58 +0100 | [diff] [blame] | 355 | if (hasDefaultValue()) { |
| 356 | val psi = getSource().getPsi() as? JetParameter |
| 357 | if (psi != null) { |
| 358 | val defaultValueText = psi.getDefaultValue()?.getText() |
| 359 | if (defaultValueText != null) { |
| 360 | node.append(DocumentationNode(defaultValueText, Content.Empty, Kind.Value), DocumentationReference.Kind.Detail) |
| 361 | } |
| 362 | } |
| 363 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 364 | node.appendAnnotations(this) |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 365 | register(this, node) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 366 | return node |
| 367 | } |
| 368 | |
| 369 | fun TypeParameterDescriptor.build(): DocumentationNode { |
| 370 | val doc = parseDocumentation(this) |
| 371 | val name = getName().asString() |
| 372 | val prefix = when (getVariance()) { |
| 373 | Variance.IN_VARIANCE -> "in " |
| 374 | Variance.OUT_VARIANCE -> "out " |
| 375 | else -> "" |
| 376 | } |
| 377 | |
| 378 | val node = DocumentationNode(prefix + name, doc, DocumentationNode.Kind.TypeParameter) |
| 379 | |
| 380 | val builtIns = KotlinBuiltIns.getInstance() |
| 381 | for (constraint in getUpperBounds()) { |
| 382 | if (constraint == builtIns.getDefaultBound()) |
| 383 | continue |
| 384 | val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.UpperBound) |
| 385 | node.append(constraintNode, DocumentationReference.Kind.Detail) |
| 386 | } |
| 387 | |
| 388 | for (constraint in getLowerBounds()) { |
Ilya Ryzhenkov | 2117416 | 2014-12-04 14:57:12 +0300 | [diff] [blame] | 389 | if (KotlinBuiltIns.isNothing(constraint)) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 390 | continue |
| 391 | val constraintNode = DocumentationNode(constraint.toString(), Content.Empty, DocumentationNode.Kind.LowerBound) |
| 392 | node.append(constraintNode, DocumentationReference.Kind.Detail) |
| 393 | } |
| 394 | return node |
| 395 | } |
| 396 | |
| 397 | fun ReceiverParameterDescriptor.build(): DocumentationNode { |
Ilya Ryzhenkov | ba1f12d | 2014-10-12 23:25:12 +0400 | [diff] [blame] | 398 | val node = DocumentationNode(getName().asString(), Content.Empty, Kind.Receiver) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 399 | node.appendType(getType()) |
| 400 | return node |
| 401 | } |
| 402 | |
Dmitry Jemerov | 1a47943 | 2015-01-09 19:57:54 +0100 | [diff] [blame] | 403 | fun AnnotationDescriptor.build(): DocumentationNode? { |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 404 | val annotationClass = getType().getConstructor().getDeclarationDescriptor() |
Dmitry Jemerov | 1a47943 | 2015-01-09 19:57:54 +0100 | [diff] [blame] | 405 | if (ErrorUtils.isError(annotationClass)) { |
| 406 | return null |
| 407 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 408 | val node = DocumentationNode(annotationClass.getName().asString(), Content.Empty, DocumentationNode.Kind.Annotation) |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 409 | val arguments = getAllValueArguments().toList().sortBy { it.first.getIndex() } |
| 410 | arguments.forEach { |
| 411 | val valueNode = it.second.build() |
| 412 | if (valueNode != null) { |
| 413 | val paramNode = DocumentationNode(it.first.getName().asString(), Content.Empty, DocumentationNode.Kind.Parameter) |
| 414 | paramNode.append(valueNode, DocumentationReference.Kind.Detail) |
| 415 | node.append(paramNode, DocumentationReference.Kind.Detail) |
| 416 | } |
| 417 | } |
Dmitry Jemerov | ef51f7e | 2014-12-30 16:34:08 +0100 | [diff] [blame] | 418 | return node |
| 419 | } |
| 420 | |
Dmitry Jemerov | 69dd298 | 2014-12-30 18:47:03 +0100 | [diff] [blame] | 421 | fun CompileTimeConstant<out Any?>.build(): DocumentationNode? { |
| 422 | val value = getValue() |
| 423 | val valueString = when(value) { |
| 424 | is String -> |
| 425 | "\"" + StringUtil.escapeStringCharacters(value) + "\"" |
| 426 | is EnumEntrySyntheticClassDescriptor -> |
| 427 | value.getContainingDeclaration().getName().asString() + "." + value.getName() |
| 428 | else -> value?.toString() |
| 429 | } |
| 430 | return if (valueString != null) DocumentationNode(valueString, Content.Empty, DocumentationNode.Kind.Value) else null |
| 431 | } |
| 432 | |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 433 | /** |
| 434 | * Generates cross-references for documentation such as extensions for a type, inheritors, etc |
| 435 | * |
| 436 | * $receiver: [DocumentationContext] for node/descriptor resolutions |
| 437 | * $node: [DocumentationNode] to visit |
| 438 | */ |
| 439 | public fun resolveReferences(node: DocumentationNode) { |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 440 | if (node.kind != Kind.PropertyAccessor) { |
| 441 | node.details(DocumentationNode.Kind.Receiver).forEach { receiver -> |
| 442 | val receiverType = receiver.detail(DocumentationNode.Kind.Type) |
| 443 | val descriptor = links[receiverType] |
| 444 | if (descriptor != null) { |
| 445 | val typeNode = descriptorToNode[descriptor] |
| 446 | // if typeNode is null, extension is to external type like in a library |
| 447 | // should we create dummy node here? |
| 448 | typeNode?.addReferenceTo(node, DocumentationReference.Kind.Extension) |
| 449 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | node.details(DocumentationNode.Kind.Supertype).forEach { detail -> |
| 453 | val descriptor = links[detail] |
| 454 | if (descriptor != null) { |
| 455 | val typeNode = descriptorToNode[descriptor] |
| 456 | typeNode?.addReferenceTo(node, DocumentationReference.Kind.Inheritor) |
| 457 | } |
| 458 | } |
| 459 | node.details.forEach { detail -> |
| 460 | val descriptor = links[detail] |
| 461 | if (descriptor != null) { |
| 462 | val typeNode = descriptorToNode[descriptor] |
| 463 | if (typeNode != null) { |
| 464 | detail.addReferenceTo(typeNode, DocumentationReference.Kind.Link) |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 469 | val descriptor = nodeToDescriptor[node] |
| 470 | if (descriptor is FunctionDescriptor) { |
| 471 | val overrides = descriptor.getOverriddenDescriptors(); |
| 472 | overrides?.forEach { |
| 473 | addOverrideLink(node, it) |
| 474 | } |
| 475 | } |
| 476 | |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 477 | resolveContentLinks(node, node.content) |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 478 | |
| 479 | for (child in node.members) { |
| 480 | resolveReferences(child) |
| 481 | } |
| 482 | for (child in node.details) { |
| 483 | resolveReferences(child) |
| 484 | } |
| 485 | } |
| 486 | |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 487 | /** |
| 488 | * Add an override link from a function node to the node corresponding to the specified descriptor. |
| 489 | * Note that this descriptor may be contained in a class where the function is not actually overridden |
| 490 | * (just inherited from the parent), so we need to go further up the override chain to find a function |
| 491 | * which exists in the code and for which we do have a documentation node. |
| 492 | */ |
| 493 | private fun addOverrideLink(node: DocumentationNode, overriddenDescriptor: FunctionDescriptor) { |
| 494 | val overriddenNode = descriptorToNode[overriddenDescriptor.getOriginal()] |
| 495 | if (overriddenNode != null) { |
| 496 | node.addReferenceTo(overriddenNode, DocumentationReference.Kind.Override) |
| 497 | } else { |
| 498 | overriddenDescriptor.getOverriddenDescriptors().forEach { addOverrideLink(node, it) } |
| 499 | } |
| 500 | } |
| 501 | |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 502 | fun getResolutionScope(node: DocumentationNode): DeclarationDescriptor { |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 503 | 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] | 504 | return descriptor |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | fun resolveContentLinks(node: DocumentationNode, content: ContentNode) { |
Dmitry Jemerov | d093fc1 | 2015-01-16 13:25:16 +0100 | [diff] [blame] | 508 | val resolvedContentChildren = content.children.map { resolveContentLink(node, it) } |
| 509 | content.children.clear() |
| 510 | content.children.addAll(resolvedContentChildren) |
| 511 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 512 | |
Dmitry Jemerov | d093fc1 | 2015-01-16 13:25:16 +0100 | [diff] [blame] | 513 | private fun resolveContentLink(node: DocumentationNode, content: ContentNode): ContentNode { |
| 514 | if (content is ContentExternalLink) { |
| 515 | val referenceText = content.href |
| 516 | val symbol = resolveReference(getResolutionScope(node), referenceText) |
| 517 | // don't include unresolved links in generated doc |
| 518 | // assume that if an href doesn't contain '/', it's not an attempt to reference an external file |
| 519 | if (symbol != null || "/" !in referenceText) { |
| 520 | val targetNode = descriptorToNode[symbol] |
| 521 | val contentLink = if (targetNode != null) ContentNodeLink(targetNode) else ContentExternalLink("#") |
| 522 | contentLink.children.addAll(content.children.map { resolveContentLink(node, it) }) |
| 523 | return contentLink |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 524 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 525 | } |
Dmitry Jemerov | d093fc1 | 2015-01-16 13:25:16 +0100 | [diff] [blame] | 526 | resolveContentLinks(node, content) |
| 527 | return content |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 528 | } |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 529 | |
| 530 | private fun resolveReference(context: DeclarationDescriptor, reference: String): DeclarationDescriptor? { |
| 531 | if (Name.isValidIdentifier(reference)) { |
| 532 | val scope = getResolutionScope(context) |
| 533 | val symbolName = Name.guess(reference) |
| 534 | return scope.getLocalVariable(symbolName) ?: |
| 535 | scope.getProperties(symbolName).firstOrNull() ?: |
| 536 | scope.getFunctions(symbolName).firstOrNull() ?: |
| 537 | scope.getClassifier(symbolName) |
| 538 | |
| 539 | } |
| 540 | |
Ilya Ryzhenkov | 1839949 | 2014-12-22 09:50:17 +0200 | [diff] [blame] | 541 | if ("." !in reference) |
| 542 | return null |
| 543 | |
Ilya Ryzhenkov | bd6cddd | 2014-12-16 21:41:32 +0300 | [diff] [blame] | 544 | val names = reference.split('.') |
| 545 | val result = names.fold<String, DeclarationDescriptor?>(context) {(nextContext, name) -> |
| 546 | nextContext?.let { resolveReference(it, name) } |
| 547 | } |
| 548 | |
| 549 | return result |
| 550 | } |
Ilya Ryzhenkov | 11355ce | 2014-10-12 22:35:47 +0400 | [diff] [blame] | 551 | } |