From 046265ea04b645b4f8ff835aa6c3ff9879501aed Mon Sep 17 00:00:00 2001 From: Ilya Ryzhenkov Date: Sat, 12 Jul 2014 01:12:42 +0400 Subject: Add Owner references, cleanup --- src/Model/DocumentationBuilder.kt | 100 ------------------------------ src/Model/DocumentationBuildingVisitor.kt | 26 ++++---- src/Model/DocumentationModel.kt | 26 ++++++-- src/Model/DocumentationNodeBuilder.kt | 96 ++++++++++++++++++++++++++++ src/Model/DocumentationResolver.kt | 16 ++--- 5 files changed, 136 insertions(+), 128 deletions(-) delete mode 100644 src/Model/DocumentationBuilder.kt create mode 100644 src/Model/DocumentationNodeBuilder.kt (limited to 'src/Model') diff --git a/src/Model/DocumentationBuilder.kt b/src/Model/DocumentationBuilder.kt deleted file mode 100644 index 353e839e..00000000 --- a/src/Model/DocumentationBuilder.kt +++ /dev/null @@ -1,100 +0,0 @@ -package org.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.* -import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns - -fun BindingContext.createDocumentationModel(file: JetFile): DocumentationModel { - val model = DocumentationModel() - val packageFragment = getPackageFragment(file) - if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment") - - val visitor = DocumentationBuilderVisitor(this) - packageFragment.accept(DocumentationBuildingVisitor(this, visitor), model) - - return model -} - -class DocumentationBuilderVisitor(val context: BindingContext) : DeclarationDescriptorVisitorEmptyBodies() { - - override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Unknown) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = DocumentationNode(descriptor!!.getName().asString(), "", DocumentationNodeKind.Receiver) - data?.addReferenceTo(node, DocumentationReferenceKind.Detail) - return node - } - - override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter) - data?.addReferenceTo(node, DocumentationReferenceKind.Detail) - return node - } - - override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, - when (descriptor.getKind()) { - ClassKind.OBJECT -> DocumentationNodeKind.Object - else -> DocumentationNodeKind.Class - } - ) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.TypeParameter) - data?.addReferenceTo(node, DocumentationReferenceKind.Detail) - val builtIns = KotlinBuiltIns.getInstance() - for (constraint in descriptor.getUpperBounds()) { - if (constraint == builtIns.getDefaultBound()) - continue - val constraintNode = DocumentationNode(constraint.toString(), "", DocumentationNodeKind.UpperBound) - node.addReferenceTo(constraintNode, DocumentationReferenceKind.Detail) - } - for (constraint in descriptor.getLowerBounds()) { - if (builtIns.isNothing(constraint)) - continue - val constraintNode = DocumentationNode(constraint.toString(), "", DocumentationNodeKind.LowerBound) - node.addReferenceTo(constraintNode, DocumentationReferenceKind.Detail) - } - return node - } - - override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { - val doc = context.getDocumentation(descriptor!!) - val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Constructor) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } - - override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { - val node = DocumentationNode(descriptor!!.fqName.asString(), "", DocumentationNodeKind.Package) - data?.addReferenceTo(node, DocumentationReferenceKind.Member) - return node - } -} diff --git a/src/Model/DocumentationBuildingVisitor.kt b/src/Model/DocumentationBuildingVisitor.kt index 81a6474c..b078f753 100644 --- a/src/Model/DocumentationBuildingVisitor.kt +++ b/src/Model/DocumentationBuildingVisitor.kt @@ -28,19 +28,19 @@ class DocumentationBuildingVisitor(val context: BindingContext, private val work visitChildren(descriptor.getTypeParameters(), node) visitChild(descriptor.getReceiverParameter(), node) visitChildren(descriptor.getValueParameters(), node) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = createDocumentation(descriptor!!, data!!) visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitPackageViewDescriptor(descriptor: PackageViewDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = createDocumentation(descriptor!!, data!!) visitChildren(descriptor.getMemberScope().getAllDescriptors(), node) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitVariableDescriptor(descriptor: VariableDescriptor?, data: DocumentationNode?): DocumentationNode? { @@ -57,12 +57,12 @@ class DocumentationBuildingVisitor(val context: BindingContext, private val work public override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = processCallable(descriptor!!, data!!) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = createDocumentation(descriptor!!, data!!) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { @@ -77,43 +77,43 @@ class DocumentationBuildingVisitor(val context: BindingContext, private val work it !is CallableMemberDescriptor || it.isUserCode() } visitChildren(members, node) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitModuleDeclaration(descriptor: ModuleDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = createDocumentation(descriptor!!, data!!) visitChild(descriptor.getPackage(FqName.ROOT), node) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } public override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = visitFunctionDescriptor(descriptor!!, data) - return node?.resolve(context.getResolutionScope(descriptor)) + return node?.resolve() } public override fun visitScriptDescriptor(scriptDescriptor: ScriptDescriptor?, data: DocumentationNode?): DocumentationNode? { val classDescriptor = scriptDescriptor!!.getClassDescriptor() val node = visitClassDescriptor(classDescriptor, data) - return node?.resolve(context.getResolutionScope(classDescriptor)) + return node?.resolve() } public override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = visitVariableDescriptor(descriptor!!, data) - return node?.resolve(context.getResolutionScope(descriptor)) + return node?.resolve() } public override fun visitPropertyGetterDescriptor(descriptor: PropertyGetterDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = visitFunctionDescriptor(descriptor!!, data) - return node?.resolve(context.getResolutionScope(descriptor)) + return node?.resolve() } public override fun visitPropertySetterDescriptor(descriptor: PropertySetterDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = visitFunctionDescriptor(descriptor!!, data) - return node?.resolve(context.getResolutionScope(descriptor)) + return node?.resolve() } public override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { val node = createDocumentation(descriptor!!, data!!) - return node.resolve(context.getResolutionScope(descriptor)) + return node.resolve() } } diff --git a/src/Model/DocumentationModel.kt b/src/Model/DocumentationModel.kt index 77bd8373..06bc3775 100644 --- a/src/Model/DocumentationModel.kt +++ b/src/Model/DocumentationModel.kt @@ -1,5 +1,6 @@ package org.jetbrains.dokka +import org.jetbrains.jet.lang.resolve.scopes.JetScope import org.jetbrains.jet.lang.resolve.BindingContext import org.jetbrains.jet.lang.psi.JetFile @@ -9,9 +10,11 @@ public enum class DocumentationNodeKind { Package Class Object + Constructor Function Property + Parameter Receiver TypeParameter @@ -19,19 +22,21 @@ public enum class DocumentationNodeKind { LowerBound Exception - Page Model } public enum class DocumentationReferenceKind { + Owner Member Detail - Owner Link Override } -public open class DocumentationNode(val name: String, val doc: String, val kind: DocumentationNodeKind) { +public open class DocumentationNode(val name: String, + val doc: String, + val kind: DocumentationNodeKind, + val scope: JetScope) { private val references = arrayListOf() public val owner: DocumentationNode @@ -54,12 +59,12 @@ public open class DocumentationNode(val name: String, val doc: String, val kind: public fun references(kind: DocumentationReferenceKind): List = references.filter { it.kind == kind } - public override fun toString() : String { + public override fun toString(): String { return "$kind $name" } } -public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model) { +public class DocumentationModel : DocumentationNode("model", "", DocumentationNodeKind.Model, JetScope.EMPTY) { fun merge(other: DocumentationModel): DocumentationModel { val model = DocumentationModel() model.addAllReferencesFrom(other) @@ -72,3 +77,14 @@ public class DocumentationModel : DocumentationNode("model", "", DocumentationNo } public data class DocumentationReference(val from: DocumentationNode, val to: DocumentationNode, val kind: DocumentationReferenceKind) + +fun BindingContext.createDocumentationModel(file: JetFile): DocumentationModel { + val model = DocumentationModel() + val packageFragment = getPackageFragment(file) + if (packageFragment == null) throw IllegalArgumentException("File $file should have package fragment") + + val visitor = DocumentationNodeBuilder(this) + packageFragment.accept(DocumentationBuildingVisitor(this, visitor), model) + + return model +} diff --git a/src/Model/DocumentationNodeBuilder.kt b/src/Model/DocumentationNodeBuilder.kt new file mode 100644 index 00000000..1c451465 --- /dev/null +++ b/src/Model/DocumentationNodeBuilder.kt @@ -0,0 +1,96 @@ +package org.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.* +import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns + +class DocumentationNodeBuilder(val context: BindingContext) : DeclarationDescriptorVisitorEmptyBodies() { + + override fun visitDeclarationDescriptor(descriptor: DeclarationDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Unknown, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Link) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitReceiverParameterDescriptor(descriptor: ReceiverParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = DocumentationNode(descriptor!!.getName().asString(), "", DocumentationNodeKind.Receiver, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Detail) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitValueParameterDescriptor(descriptor: ValueParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Parameter, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Detail) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitClassDescriptor(descriptor: ClassDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, when (descriptor.getKind()) { + ClassKind.OBJECT -> DocumentationNodeKind.Object + else -> DocumentationNodeKind.Class + }, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Member) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitFunctionDescriptor(descriptor: FunctionDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Function, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Member) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitTypeParameterDescriptor(descriptor: TypeParameterDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.TypeParameter, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Detail) + val builtIns = KotlinBuiltIns.getInstance() + for (constraint in descriptor.getUpperBounds()) { + if (constraint == builtIns.getDefaultBound()) + continue + val constraintNode = DocumentationNode(constraint.toString(), "", 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)) + node.addReferenceTo(constraintNode, DocumentationReferenceKind.Detail) + } + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitPropertyDescriptor(descriptor: PropertyDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Property, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Member) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor?, data: DocumentationNode?): DocumentationNode? { + val doc = context.getDocumentation(descriptor!!) + val node = DocumentationNode(descriptor.getName().asString(), doc, DocumentationNodeKind.Constructor, context.getResolutionScope(descriptor)) + data!!.addReferenceTo(node, DocumentationReferenceKind.Member) + node.addReferenceTo(data, DocumentationReferenceKind.Owner) + return node + } + + override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor?, data: DocumentationNode?): DocumentationNode? { + val node = DocumentationNode(descriptor!!.fqName.asString(), "", 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 index 1d470867..dee68f49 100644 --- a/src/Model/DocumentationResolver.kt +++ b/src/Model/DocumentationResolver.kt @@ -2,20 +2,15 @@ package org.jetbrains.dokka import org.jetbrains.jet.lang.resolve.scopes.* import org.jetbrains.jet.lang.resolve.name.* -import org.jetbrains.jet.lang.descriptors.FunctionDescriptor - +import org.jetbrains.jet.lang.descriptors.* fun DocumentationNode.resolve(): DocumentationNode { - return this -} - -fun DocumentationNode.resolve(scope: JetScope): DocumentationNode { for (detail in details) { - val symbol = when (detail.kind) { + val symbol: DeclarationDescriptor? = when (detail.kind) { DocumentationNodeKind.Receiver -> (scope.getContainingDeclaration() as FunctionDescriptor).getReceiverParameter() DocumentationNodeKind.Parameter -> scope.getLocalVariable(Name.guess(detail.name)) - DocumentationNodeKind.Function -> scope.getFunctions(Name.guess(detail.name)).single() - DocumentationNodeKind.Property -> scope.getProperties(Name.guess(detail.name)).single() + DocumentationNodeKind.Function -> scope.getFunctions(Name.guess(detail.name)).firstOrNull() + DocumentationNodeKind.Property -> scope.getProperties(Name.guess(detail.name)).firstOrNull() DocumentationNodeKind.TypeParameter -> scope.getClassifier(Name.guess(detail.name)) else -> scope.getClassifier(Name.guess(detail.name)) } @@ -24,4 +19,5 @@ fun DocumentationNode.resolve(scope: JetScope): DocumentationNode { throw IllegalStateException("Cannot resolve node $this detail $detail") } return this -} \ No newline at end of file +} + -- cgit