From 8f159151f182f4561594a4c23c22e82830bcc9b4 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Fri, 6 Mar 2015 16:11:32 +0100 Subject: generate documentation for default object extension properties --- src/Formats/StructuredFormatService.kt | 2 ++ src/Kotlin/DocumentationBuilder.kt | 30 ++++++++++++++++++++++++----- test/data/classes/defaultObjectExtension.kt | 10 ++++++++++ test/data/format/defaultObjectExtension.kt | 10 ++++++++++ test/data/format/defaultObjectExtension.md | 23 ++++++++++++++++++++++ test/src/format/MarkdownFormatTest.kt | 8 +++++++- test/src/model/ClassTest.kt | 9 +++++++++ 7 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 test/data/classes/defaultObjectExtension.kt create mode 100644 test/data/format/defaultObjectExtension.kt create mode 100644 test/data/format/defaultObjectExtension.md diff --git a/src/Formats/StructuredFormatService.kt b/src/Formats/StructuredFormatService.kt index 7b9374ec..cc551eb4 100644 --- a/src/Formats/StructuredFormatService.kt +++ b/src/Formats/StructuredFormatService.kt @@ -306,6 +306,8 @@ public abstract class StructuredFormatService(locationService: LocationService, }, node, to) appendSection(location, "Extension Properties", node.extensions.filter { it.kind == DocumentationNode.Kind.Property }, node, to) appendSection(location, "Extension Functions", node.extensions.filter { it.kind == DocumentationNode.Kind.Function }, node, to) + appendSection(location, "Default Object Extension Properties", node.extensions.filter { it.kind == DocumentationNode.Kind.DefaultObjectProperty }, node, to) + appendSection(location, "Default Object Extension Functions", node.extensions.filter { it.kind == DocumentationNode.Kind.DefaultObjectFunction }, node, to) appendSection(location, "Inheritors", node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to) appendSection(location, "Links", node.links, node, to) diff --git a/src/Kotlin/DocumentationBuilder.kt b/src/Kotlin/DocumentationBuilder.kt index 9ca3ad79..aa4a0caf 100644 --- a/src/Kotlin/DocumentationBuilder.kt +++ b/src/Kotlin/DocumentationBuilder.kt @@ -144,11 +144,12 @@ class DocumentationBuilder(val session: ResolveSession, } fun JetType.signature(): String { - val typeName = getConstructor().getDeclarationDescriptor()?.getName()?.asString() + val declarationDescriptor = getConstructor().getDeclarationDescriptor() ?: return "" + val typeName = DescriptorUtils.getFqName(declarationDescriptor).asString() if (typeName == "Array" && getArguments().size() == 1) { return "Array<" + getArguments().first().getType().signature() + ">" } - return typeName ?: "" + return typeName } fun resolveContentLink(descriptor: DeclarationDescriptor, href: String): ContentBlock { @@ -266,6 +267,15 @@ class DocumentationBuilder(val session: ResolveSession, return val classifierDescriptor = jetType.getConstructor().getDeclarationDescriptor() val name = when (classifierDescriptor) { + is ClassDescriptor -> { + if (classifierDescriptor.isDefaultObject()) { + classifierDescriptor.getContainingDeclaration().getName().asString() + + "." + classifierDescriptor.getName().asString() + } + else { + classifierDescriptor.getName().asString() + } + } is Named -> classifierDescriptor.getName().asString() else -> "" } @@ -428,8 +438,14 @@ class DocumentationBuilder(val session: ResolveSession, return node } - private fun DeclarationDescriptor.inDefaultObject() = - getContainingDeclaration().let { it is ClassDescriptor && it.isDefaultObject() } + private fun CallableMemberDescriptor.inDefaultObject(): Boolean { + val containingDeclaration = getContainingDeclaration() + if ((containingDeclaration as? ClassDescriptor)?.isDefaultObject() ?: false) { + return true + } + val receiver = getExtensionReceiverParameter() + return (receiver?.getType()?.getConstructor()?.getDeclarationDescriptor() as? ClassDescriptor)?.isDefaultObject() ?: false + } fun CallableMemberDescriptor.getExtensionClassDescriptor(): ClassifierDescriptor? { val extensionReceiver = getExtensionReceiverParameter() @@ -614,7 +630,11 @@ class DocumentationBuilder(val session: ResolveSession, } fun ReceiverParameterDescriptor.build(): DocumentationNode { - link(getType().getConstructor().getDeclarationDescriptor(), + var receiverClass: DeclarationDescriptor = getType().getConstructor().getDeclarationDescriptor() + if ((receiverClass as? ClassDescriptor)?.isDefaultObject() ?: false) { + receiverClass = receiverClass.getContainingDeclaration()!! + } + link(receiverClass, getContainingDeclaration(), DocumentationReference.Kind.Extension) diff --git a/test/data/classes/defaultObjectExtension.kt b/test/data/classes/defaultObjectExtension.kt new file mode 100644 index 00000000..0bef8a49 --- /dev/null +++ b/test/data/classes/defaultObjectExtension.kt @@ -0,0 +1,10 @@ +class Foo { + class object Default { + } +} + + +/** + * The def + */ +val Foo.Default.x: Int get() = 1 diff --git a/test/data/format/defaultObjectExtension.kt b/test/data/format/defaultObjectExtension.kt new file mode 100644 index 00000000..e367aa41 --- /dev/null +++ b/test/data/format/defaultObjectExtension.kt @@ -0,0 +1,10 @@ +class Foo { + class object Default { + } +} + + +/** + * The default object property. + */ +val Foo.Default.x: Int get() = 1 diff --git a/test/data/format/defaultObjectExtension.md b/test/data/format/defaultObjectExtension.md new file mode 100644 index 00000000..4ede33e8 --- /dev/null +++ b/test/data/format/defaultObjectExtension.md @@ -0,0 +1,23 @@ +[test](test/index) / [Foo](test/-foo/index) + + +# Foo + +`class Foo` + + + +### Constructors + + +| [<init>](test/-foo/-init-) | `Foo()` | + + +### Default Object Extension Properties + + +| [x](test/x) | `val Foo.Default.x: Int` +The default object property. + + | + diff --git a/test/src/format/MarkdownFormatTest.kt b/test/src/format/MarkdownFormatTest.kt index 33718e87..1c46e662 100644 --- a/test/src/format/MarkdownFormatTest.kt +++ b/test/src/format/MarkdownFormatTest.kt @@ -155,9 +155,15 @@ public class MarkdownFormatTest { } } - Test fun javadocOrderedLIst() { + Test fun javadocOrderedList() { verifyOutput("test/data/format/javadocOrderedList.java", ".md") { model, output -> markdownService.appendNodes(tempLocation, output, model.members.single().members.filter { it.name == "Bar" }) } } + + Test fun defaultObjectExtension() { + verifyOutput("test/data/format/defaultObjectExtension.kt", ".md") { model, output -> + markdownService.appendNodes(tempLocation, output, model.members.single().members.filter { it.name == "Foo" }) + } + } } diff --git a/test/src/model/ClassTest.kt b/test/src/model/ClassTest.kt index d16b9491..61af5361 100644 --- a/test/src/model/ClassTest.kt +++ b/test/src/model/ClassTest.kt @@ -244,4 +244,13 @@ public class ClassTest { assertEquals("inner", modifiers[2].name) } } + + Test fun defaultObjectExtension() { + verifyModel("test/data/classes/defaultObjectExtension.kt") { model -> + val pkg = model.members.single() + val cls = pkg.members.single { it.name == "Foo" } + val extensions = cls.extensions.filter { it.kind == DocumentationNode.Kind.DefaultObjectProperty } + assertEquals(1, extensions.size()) + } + } } -- cgit