From d627d2cfdfebcddd63669734efb82dfc66e7c7fe Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Sat, 12 Jul 2014 03:25:39 +0400 Subject: Convert string doc to structured DocumentationContent --- src/Analysis/CommentsAPI.kt | 4 ---- src/Generation/ConsoleGenerator.kt | 2 +- src/Model/Diagnostics.kt | 40 ++++++++++++++++++++++++++++++++ src/Model/DocumentationModel.kt | 4 ++-- src/Model/DocumentationNodeBuilder.kt | 16 ++++++------- src/Model/DocumentationResolver.kt | 40 -------------------------------- src/Model/Sections.kt | 43 +++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 55 deletions(-) create mode 100644 src/Model/Diagnostics.kt delete mode 100644 src/Model/DocumentationResolver.kt create mode 100644 src/Model/Sections.kt (limited to 'src') diff --git a/src/Analysis/CommentsAPI.kt b/src/Analysis/CommentsAPI.kt index 6bdc3716..c8a2ed3c 100644 --- a/src/Analysis/CommentsAPI.kt +++ b/src/Analysis/CommentsAPI.kt @@ -5,10 +5,6 @@ import org.jetbrains.jet.lang.resolve.* import org.jetbrains.jet.kdoc.psi.api.* import org.jetbrains.jet.lang.psi.* -fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): String { - return getDocumentationElements(descriptor).map { it.extractText() }.join("\n") -} - fun BindingContext.getDocumentationElements(descriptor: DeclarationDescriptor): List { val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) if (psiElement == null) diff --git a/src/Generation/ConsoleGenerator.kt b/src/Generation/ConsoleGenerator.kt index 90bd53be..3ba42099 100644 --- a/src/Generation/ConsoleGenerator.kt +++ b/src/Generation/ConsoleGenerator.kt @@ -12,7 +12,7 @@ public class ConsoleGenerator() { public fun generateHeader(node: DocumentationNode, indent: String = "") { println("$indent${node.kind}: ${node.name}") - println("$indent\"${node.doc.replace("\n", "\n$indent")}\"") + println("$indent\"${node.doc.summary.replace("\n", "\n$indent")}\"") println() } diff --git a/src/Model/Diagnostics.kt b/src/Model/Diagnostics.kt new file mode 100644 index 00000000..78d711a0 --- /dev/null +++ b/src/Model/Diagnostics.kt @@ -0,0 +1,40 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.name.* + +fun DocumentationNode.checkResolve() { + val parentScope = scope + + for (item in details + members) { + val symbolName = item.name + val symbol: DeclarationDescriptor? = when (item.kind) { + DocumentationNodeKind.Receiver -> (parentScope.getContainingDeclaration() as FunctionDescriptor).getReceiverParameter() + DocumentationNodeKind.Parameter -> parentScope.getLocalVariable(Name.guess(symbolName)) + DocumentationNodeKind.Function -> parentScope.getFunctions(Name.guess(symbolName)).firstOrNull() + DocumentationNodeKind.Property -> parentScope.getProperties(Name.guess(symbolName)).firstOrNull() + DocumentationNodeKind.Constructor -> parentScope.getFunctions(Name.guess(symbolName)).firstOrNull() + + DocumentationNodeKind.Package -> { + // TODO: do not resolve constructors and packages for now + item.scope.getContainingDeclaration() + } + else -> parentScope.getClassifier(Name.guess(symbolName)) + } + + if (symbol == null) + println("WARNING: Cannot resolve $item in ${path(this)}") + } + + for (reference in allReferences().filterNot { it.kind == DocumentationReferenceKind.Owner }) { + reference.to.checkResolve() + } +} + +fun path(node: DocumentationNode): String { + val owner = node.owner + if (owner != null) + return "$node in ${path(owner)}" + else + return "$node" +} \ No newline at end of file diff --git a/src/Model/DocumentationModel.kt b/src/Model/DocumentationModel.kt index e24b3c41..55f524b3 100644 --- a/src/Model/DocumentationModel.kt +++ b/src/Model/DocumentationModel.kt @@ -36,7 +36,7 @@ public enum class DocumentationReferenceKind { } public open class DocumentationNode(val name: String, - val doc: String, + val doc: DocumentationContent, val kind: DocumentationNodeKind, val scope: JetScope) { private val references = arrayListOf() @@ -67,7 +67,7 @@ public open class DocumentationNode(val name: String, } } -public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model, JetScope.EMPTY) { +public class DocumentationModel : DocumentationNode("model", DocumentationContent.Empty, DocumentationNodeKind.Model, JetScope.EMPTY) { fun merge(other: DocumentationModel): DocumentationModel { val model = DocumentationModel() model.addAllReferencesFrom(other) diff --git a/src/Model/DocumentationNodeBuilder.kt b/src/Model/DocumentationNodeBuilder.kt index e3307f50..103e2e89 100644 --- a/src/Model/DocumentationNodeBuilder.kt +++ b/src/Model/DocumentationNodeBuilder.kt @@ -17,10 +17,10 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip } override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = DocumentationNode(descriptor!!.getName().asString(), "", DocumentationNodeKind.Receiver, context.getResolutionScope(descriptor)) + val node = DocumentationNode(descriptor!!.getName().asString(), DocumentationContent.Empty, DocumentationNodeKind.Receiver, context.getResolutionScope(descriptor)) data!!.addReferenceTo(node, DocumentationReferenceKind.Detail) - val typeNode = DocumentationNode(descriptor.getType().toString(), "", DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) + val typeNode = DocumentationNode(descriptor.getType().toString(), DocumentationContent.Empty, DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) node.addReferenceTo(typeNode, DocumentationReferenceKind.Detail) node.addReferenceTo(data, DocumentationReferenceKind.Owner) @@ -32,7 +32,7 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter, context.getResolutionScope(descriptor)) data!!.addReferenceTo(node, DocumentationReferenceKind.Detail) - val typeNode = DocumentationNode(descriptor.getType().toString(), "", DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) + val typeNode = DocumentationNode(descriptor.getType().toString(), DocumentationContent.Empty, DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) node.addReferenceTo(typeNode, DocumentationReferenceKind.Detail) node.addReferenceTo(data, DocumentationReferenceKind.Owner) @@ -56,7 +56,7 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function, context.getResolutionScope(descriptor)) data!!.addReferenceTo(node, DocumentationReferenceKind.Member) - val typeNode = DocumentationNode(descriptor.getReturnType().toString(), "", DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) + val typeNode = DocumentationNode(descriptor.getReturnType().toString(), DocumentationContent.Empty, DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) node.addReferenceTo(typeNode, DocumentationReferenceKind.Detail) node.addReferenceTo(data, DocumentationReferenceKind.Owner) @@ -71,13 +71,13 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip for (constraint in descriptor.getUpperBounds()) { if (constraint == builtIns.getDefaultBound()) continue - val constraintNode = DocumentationNode(constraint.toString(), "", DocumentationNodeKind.UpperBound, context.getResolutionScope(descriptor)) + val constraintNode = DocumentationNode(constraint.toString(), DocumentationContent.Empty, DocumentationNodeKind.UpperBound, context.getResolutionScope(descriptor)) node.addReferenceTo(constraintNode, DocumentationReferenceKind.Detail) } for (constraint in descriptor.getLowerBounds()) { if (builtIns.isNothing(constraint)) continue - val constraintNode = DocumentationNode(constraint.toString(), "", DocumentationNodeKind.LowerBound, context.getResolutionScope(descriptor)) + val constraintNode = DocumentationNode(constraint.toString(), DocumentationContent.Empty, DocumentationNodeKind.LowerBound, context.getResolutionScope(descriptor)) node.addReferenceTo(constraintNode, DocumentationReferenceKind.Detail) } node.addReferenceTo(data, DocumentationReferenceKind.Owner) @@ -89,7 +89,7 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property, context.getResolutionScope(descriptor)) data!!.addReferenceTo(node, DocumentationReferenceKind.Member) - val typeNode = DocumentationNode(descriptor.getType().toString(), "", DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) + val typeNode = DocumentationNode(descriptor.getType().toString(), DocumentationContent.Empty, DocumentationNodeKind.Type, context.getResolutionScope(descriptor)) node.addReferenceTo(typeNode, DocumentationReferenceKind.Detail) node.addReferenceTo(data, DocumentationReferenceKind.Owner) @@ -105,7 +105,7 @@ class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescrip } override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = DocumentationNode(descriptor!!.fqName.asString(), "", DocumentationNodeKind.Package, context.getResolutionScope(descriptor)) + val node = DocumentationNode(descriptor!!.fqName.asString(), DocumentationContent.Empty, DocumentationNodeKind.Package, context.getResolutionScope(descriptor)) data!!.addReferenceTo(node, DocumentationReferenceKind.Member) node.addReferenceTo(data, DocumentationReferenceKind.Owner) return node diff --git a/src/Model/DocumentationResolver.kt b/src/Model/DocumentationResolver.kt deleted file mode 100644 index b5b6b1a2..00000000 --- a/src/Model/DocumentationResolver.kt +++ /dev/null @@ -1,40 +0,0 @@ -package org.jetbrains.dokka - -import org.jetbrains.jet.lang.resolve.name.* -import org.jetbrains.jet.lang.descriptors.* - -fun DocumentationNode.checkResolve() { - val parentScope = scope - - for (item in details + members) { - val symbolName = item.name - val symbol: DeclarationDescriptor? = when (item.kind) { - DocumentationNodeKind.Receiver -> (parentScope.getContainingDeclaration() as FunctionDescriptor).getReceiverParameter() - DocumentationNodeKind.Parameter -> parentScope.getLocalVariable(Name.guess(symbolName)) - DocumentationNodeKind.Function -> parentScope.getFunctions(Name.guess(symbolName)).firstOrNull() - DocumentationNodeKind.Property -> parentScope.getProperties(Name.guess(symbolName)).firstOrNull() - DocumentationNodeKind.Constructor -> parentScope.getFunctions(Name.guess(symbolName)).firstOrNull() - - DocumentationNodeKind.Package -> { - // TODO: do not resolve constructors and packages for now - item.scope.getContainingDeclaration() - } - else -> parentScope.getClassifier(Name.guess(symbolName)) - } - - if (symbol == null) - println("WARNING: Cannot resolve $item in ${path(this)}") - } - - for (reference in allReferences().filterNot { it.kind == DocumentationReferenceKind.Owner }) { - reference.to.checkResolve() - } -} - -fun path(node: DocumentationNode) : String { - val owner = node.owner - if (owner != null) - return "$node in ${path(owner)}" - else - return "$node" -} \ No newline at end of file diff --git a/src/Model/Sections.kt b/src/Model/Sections.kt new file mode 100644 index 00000000..733c0d2f --- /dev/null +++ b/src/Model/Sections.kt @@ -0,0 +1,43 @@ +package org.jetbrains.dokka + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lang.resolve.BindingContext + +fun BindingContext.getDocumentation(descriptor: DeclarationDescriptor): DocumentationContent { + val docText = getDocumentationElements(descriptor).map { it.extractText() }.join("\n") + return DocumentationContent(docText, listOf()) +} + +class DocumentationContentSection(val label: String, val text: String) { + +} + +class DocumentationContent(val summary: String, val sections: List) { + + override fun equals(other: Any?): Boolean { + if (other !is DocumentationContent) + return false + if (summary != other.summary) + return false + if (sections.size != other.sections.size) + return false + for (index in sections.indices) + if (sections[index] != other.sections[index]) + return false + + return true + } + + override fun hashCode(): Int { + return summary.hashCode() + sections.map { it.hashCode() }.sum() + } + + override fun toString(): String { + return "$summary | " + sections.joinToString() + } + + class object { + val Empty = DocumentationContent("", listOf()) + } +} + -- cgit