aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 18:44:53 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-07-11 18:44:53 +0400
commit35b64fb01d6b4da4b0dab840a787abc84d1bff06 (patch)
tree6733eeb9f157188d0b7f935e8fecaf97707b8139 /src
parentc540bc28a79092b212e575fa18d33399faee83b9 (diff)
downloaddokka-35b64fb01d6b4da4b0dab840a787abc84d1bff06.tar.gz
dokka-35b64fb01d6b4da4b0dab840a787abc84d1bff06.tar.bz2
dokka-35b64fb01d6b4da4b0dab840a787abc84d1bff06.zip
Build model using recursive visitor, include packages in model, add test for function with parameters, unify model and node.
Diffstat (limited to 'src')
-rw-r--r--src/DocumentationBuilder.kt32
-rw-r--r--src/DocumentationBuildingVisitor.kt114
-rw-r--r--src/DocumentationModel.kt35
3 files changed, 158 insertions, 23 deletions
diff --git a/src/DocumentationBuilder.kt b/src/DocumentationBuilder.kt
new file mode 100644
index 00000000..02db3540
--- /dev/null
+++ b/src/DocumentationBuilder.kt
@@ -0,0 +1,32 @@
+package com.jetbrains.dokka
+
+import org.jetbrains.jet.lang.resolve.*
+import org.jetbrains.jet.lang.psi.*
+import org.jetbrains.jet.lang.descriptors.*
+import org.jetbrains.jet.lang.descriptors.impl.*
+
+fun BindingContext.createDocumentation(file: JetFile): DocumentationModel {
+ val model = DocumentationModel()
+ val packageFragment = getPackageFragment(file)
+ if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment")
+
+ val visitor = DocumentationBuilderVisitor()
+ visitDescriptor(packageFragment, model, visitor)
+
+ return model
+}
+
+class DocumentationBuilderVisitor() : DeclarationDescriptorVisitorEmptyBodies<DocumentationNode, DocumentationNode>() {
+
+ override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = DocumentationNode(descriptor!!.getName().asString(), "doc", DocumentationNodeKind.Function)
+ data?.addReferenceTo(node, DocumentationReferenceKind.Member)
+ return node
+ }
+
+ override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = DocumentationNode(descriptor!!.getName().asString(), "doc", DocumentationNodeKind.Function)
+ data?.addReferenceTo(node, DocumentationReferenceKind.Detail)
+ return node
+ }
+}
diff --git a/src/DocumentationBuildingVisitor.kt b/src/DocumentationBuildingVisitor.kt
new file mode 100644
index 00000000..6486ac90
--- /dev/null
+++ b/src/DocumentationBuildingVisitor.kt
@@ -0,0 +1,114 @@
+package com.jetbrains.dokka
+
+import org.jetbrains.jet.lang.descriptors.*
+import org.jetbrains.jet.lang.resolve.name.FqName
+
+class DocumentationBuildingVisitor(private val worker: DeclarationDescriptorVisitor<DocumentationNode, DocumentationNode>)
+: DeclarationDescriptorVisitor<DocumentationNode, DocumentationNode> {
+
+ private fun visitChildren(descriptors: Collection<DeclarationDescriptor>, data: DocumentationNode) {
+ for (descriptor in descriptors) {
+ descriptor.accept(this, data)
+ }
+ }
+
+ private fun visitChildren(descriptor: DeclarationDescriptor?, data: DocumentationNode) {
+ descriptor?.accept(this, data)
+ }
+
+ private fun createDocumentation(descriptor: DeclarationDescriptor, data: DocumentationNode): DocumentationNode {
+ return descriptor.accept(worker, data)
+ }
+
+ private fun processCallable(descriptor: CallableDescriptor, data: DocumentationNode): DocumentationNode {
+ val node = createDocumentation(descriptor, data)
+ visitChildren(descriptor.getTypeParameters(), node)
+ visitChildren(descriptor.getReceiverParameter(), node)
+ visitChildren(descriptor.getValueParameters(), node)
+ return node
+ }
+
+ public override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ visitChildren(descriptor.getMemberScope().getAllDescriptors(), node)
+ return node
+ }
+
+ public override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ visitChildren(descriptor.getMemberScope().getAllDescriptors(), node)
+ return node
+ }
+
+ public override fun visitVariableDescriptor(descriptor: VariableDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = processCallable(descriptor!!, data!!)
+ return node
+ }
+
+ public override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = processCallable(descriptor!!, data!!)
+ visitChildren(descriptor.getGetter(), node)
+ visitChildren(descriptor.getSetter(), node)
+ return node
+ }
+
+ public override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = processCallable(descriptor!!, data!!)
+ return node
+ }
+
+ public override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ return node
+ }
+
+ public override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ visitChildren(descriptor.getThisAsReceiverParameter(), node)
+ visitChildren(descriptor.getConstructors(), node)
+ visitChildren(descriptor.getTypeConstructor().getParameters(), node)
+ visitChildren(descriptor.getClassObjectDescriptor(), node)
+ visitChildren(descriptor.getDefaultType().getMemberScope().getAllDescriptors(), node)
+ return node
+ }
+
+ public override fun visitModuleDeclaration(descriptor: ModuleDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ visitChildren(descriptor.getPackage(FqName.ROOT), node)
+ return node
+ }
+
+ public override fun visitConstructorDescriptor(constructorDescriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = visitFunctionDescriptor(constructorDescriptor, data)
+ return node
+ }
+
+ public override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = visitClassDescriptor(scriptDescriptor!!.getClassDescriptor(), data)
+ return node
+ }
+
+ public override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = visitVariableDescriptor(descriptor, data)
+ return node
+ }
+
+ public override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = visitFunctionDescriptor(descriptor, data)
+ return node
+ }
+
+ public override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = visitFunctionDescriptor(descriptor, data)
+ return node
+ }
+
+ public override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? {
+ val node = createDocumentation(descriptor!!, data!!)
+ return node
+ }
+}
+
+public fun visitDescriptor(descriptor: DeclarationDescriptor, data: DocumentationNode, visitor: DeclarationDescriptorVisitor<DocumentationNode, DocumentationNode>): DocumentationNode {
+ return descriptor.accept(DocumentationBuildingVisitor(visitor), data)
+}
diff --git a/src/DocumentationModel.kt b/src/DocumentationModel.kt
index 3391c277..b8ef21b0 100644
--- a/src/DocumentationModel.kt
+++ b/src/DocumentationModel.kt
@@ -1,7 +1,7 @@
package com.jetbrains.dokka
-import com.intellij.psi.PsiFile
import org.jetbrains.jet.lang.resolve.BindingContext
+import org.jetbrains.jet.lang.psi.JetFile
public enum class DocumentationNodeKind {
Package
@@ -13,7 +13,7 @@ public enum class DocumentationNodeKind {
Exception
Page
- Module
+ Model
}
public enum class DocumentationReferenceKind {
@@ -24,7 +24,7 @@ public enum class DocumentationReferenceKind {
Override
}
-public class DocumentationNode(val name: String, val doc: String, val kind: DocumentationNodeKind) {
+public open class DocumentationNode(val name: String, val doc: String, val kind: DocumentationNodeKind) {
private val references = arrayListOf<DocumentationReference>()
public val owner: DocumentationNode
@@ -36,39 +36,28 @@ public class DocumentationNode(val name: String, val doc: String, val kind: Docu
public val links: List<DocumentationNode>
get() = references(DocumentationReferenceKind.Link).map { it.to }
+ // TODO: Should we allow node mutation? Model merge will copy by ref, so references are transparent, which could nice
public fun addReferenceTo(to: DocumentationNode, kind: DocumentationReferenceKind) {
references.add(DocumentationReference(this, to, kind))
}
+ public fun addAllReferencesFrom(other: DocumentationNode) {
+ references.addAll(other.references)
+ }
+
public fun references(kind: DocumentationReferenceKind): List<DocumentationReference> = references.filter { it.kind == kind }
}
-public class DocumentationModel {
- private val items = arrayListOf<DocumentationNode>()
-
+public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model) {
fun merge(other: DocumentationModel): DocumentationModel {
val model = DocumentationModel()
- model.addNodes(other.nodes)
- model.addNodes(this.nodes)
+ model.addAllReferencesFrom(other)
+ model.addAllReferencesFrom(this)
return model
}
- public fun addNode(node: DocumentationNode) {
- items.add(node)
- }
-
- public fun addNodes(nodes: List<DocumentationNode>) {
- items.addAll(nodes)
- }
-
public val nodes: List<DocumentationNode>
- get() = items
+ get() = members
}
public data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: DocumentationReferenceKind)
-
-fun BindingContext.createDocumentation(file: PsiFile): DocumentationModel {
- val model = DocumentationModel()
- model.addNode(DocumentationNode("fn", "doc", DocumentationNodeKind.Function))
- return model
-} \ No newline at end of file