From af81d5a311125fc30dcf9c14fc9bd9c8680e532b Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 8 Nov 2016 20:21:15 +0300 Subject: Now all sample blocks in kotlin-website wrapped in
...
And small refactoring --- .../kotlin/Formats/KotlinWebsiteFormatService.kt | 26 ++++++++++++++-------- .../main/kotlin/Formats/StructuredFormatService.kt | 20 ++++++++++++----- .../kotlin/Kotlin/DescriptorDocumentationParser.kt | 22 +++++++++--------- core/src/main/kotlin/Model/Content.kt | 3 ++- 4 files changed, 46 insertions(+), 25 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index bc9358ac..edae9976 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -30,11 +30,13 @@ class KotlinWebsiteOutputBuilder(to: StringBuilder, override fun appendStrikethrough(body: () -> Unit) = wrapInTag("s", body) - private fun div(to: StringBuilder, cssClass: String, block: () -> Unit) { - to.append("
") - insideDiv++ + private fun div(to: StringBuilder, cssClass: String, markdown: Boolean = false, block: () -> Unit) { + to.append("
") + if (!markdown) insideDiv++ block() - insideDiv-- + if (!markdown) insideDiv-- to.append("
\n") } @@ -51,12 +53,18 @@ class KotlinWebsiteOutputBuilder(to: StringBuilder, } } + override fun appendSampleBlockCode(language: String, body: () -> Unit) { + div(to, "sample", true) { + appendBlockCode(language, body) + } + } + override fun appendAsOverloadGroup(to: StringBuilder, block: () -> Unit) { - to.append("
") - ensureParagraph() - block() - ensureParagraph() - to.append("
") + div(to, "overload-group", true) { + ensureParagraph() + block() + ensureParagraph() + } } override fun appendLink(href: String, body: () -> Unit) = wrap("", "", body) diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index f24c9c2d..3bd06130 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -47,6 +47,7 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, protected abstract fun ensureParagraph() + open fun appendSampleBlockCode(language: String, body: () -> Unit) = appendBlockCode(language, body) abstract fun appendBlockCode(language: String, body: () -> Unit) abstract fun appendHeader(level: Int = 1, body: () -> Unit) abstract fun appendParagraph(body: () -> Unit) @@ -143,13 +144,22 @@ abstract class StructuredOutputBuilder(val to: StringBuilder, } } - is ContentBlockCode -> appendBlockCode(content.language) { - for ((index, contentNode) in content.children.withIndex()) { - appendContent(contentNode) - if (index < content.children.size - 1) { - to.append("\n") + is ContentBlockSampleCode, is ContentBlockCode -> { + content as ContentBlockCode + fun appendBlockCodeContent() { + for ((index, contentNode) in content.children.withIndex()) { + appendContent(contentNode) + if (index < content.children.size - 1) { + to.append("\n") + } } } + when (content) { + is ContentBlockSampleCode -> + appendSampleBlockCode(content.language, ::appendBlockCodeContent) + is ContentBlockCode -> + appendBlockCode(content.language, ::appendBlockCodeContent) + } } is ContentHeading -> appendHeader(content.level) { appendContent(content.children) } is ContentBlock -> appendContent(content.children) diff --git a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt index 941d071d..2fc268e8 100644 --- a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt +++ b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.idea.kdoc.findKDoc import org.jetbrains.kotlin.idea.kdoc.getKDocLinkResolutionScope import org.jetbrains.kotlin.incremental.components.NoLookupLocation +import org.jetbrains.kotlin.kdoc.parser.KDocKnownTag import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection import org.jetbrains.kotlin.kdoc.psi.impl.KDocTag import org.jetbrains.kotlin.load.java.descriptors.JavaCallableMemberDescriptor @@ -60,10 +61,10 @@ class DescriptorDocumentationParser if (kdoc is KDocSection) { val tags = kdoc.getTags() tags.forEach { - when (it.name) { - "sample" -> + when (it.knownTag) { + KDocKnownTag.SAMPLE -> content.append(functionBody(descriptor, it.getSubjectName())) - "see" -> + KDocKnownTag.SEE -> content.addTagToSeeAlso(descriptor, it) else -> { val section = content.addSection(javadocSectionDisplayName(it.name), it.getSubjectName()) @@ -147,7 +148,7 @@ class DescriptorDocumentationParser private fun functionBody(descriptor: DeclarationDescriptor, functionName: String?): ContentNode { if (functionName == null) { logger.warn("Missing function name in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Missing function name in @sample")); it } + return ContentBlockSampleCode().let { it.append(ContentText("//Missing function name in @sample")); it } } val scope = getKDocLinkResolutionScope(resolutionFacade, descriptor) val rootPackage = resolutionFacade.moduleDescriptor.getPackage(FqName.ROOT) @@ -155,16 +156,16 @@ class DescriptorDocumentationParser val symbol = resolveInScope(functionName, scope) ?: resolveInScope(functionName, rootScope) if (symbol == null) { logger.warn("Unresolved function $functionName in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Unresolved: $functionName")); it } + return ContentBlockSampleCode().let { it.append(ContentText("//Unresolved: $functionName")); it } } val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(symbol) if (psiElement == null) { logger.warn("Can't find source for function $functionName in @sample in ${descriptor.signature()}") - return ContentBlockCode().let() { it.append(ContentText("Source not found: $functionName")); it } + return ContentBlockSampleCode().let { it.append(ContentText("//Source not found: $functionName")); it } } val text = when (psiElement) { - is KtDeclarationWithBody -> ContentBlockCode().let() { + is KtDeclarationWithBody -> ContentBlockCode().let { val bodyExpression = psiElement.bodyExpression when (bodyExpression) { is KtBlockExpression -> bodyExpression.text.removeSurrounding("{", "}") @@ -174,10 +175,10 @@ class DescriptorDocumentationParser else -> psiElement.text } - val lines = text.trimEnd().split("\n".toRegex()).toTypedArray().filterNot { it.length == 0 } - val indent = lines.map { it.takeWhile { it.isWhitespace() }.count() }.min() ?: 0 + val lines = text.trimEnd().split("\n".toRegex()).toTypedArray().filterNot(String::isEmpty) + val indent = lines.map { it.takeWhile(Char::isWhitespace).count() }.min() ?: 0 val finalText = lines.map { it.drop(indent) }.joinToString("\n") - return ContentBlockCode("kotlin").let() { it.append(ContentText(finalText)); it } + return ContentBlockSampleCode().let { it.append(ContentText(finalText)); it } } private fun resolveInScope(functionName: String, scope: ResolutionScope): DeclarationDescriptor? { @@ -197,6 +198,7 @@ class DescriptorDocumentationParser symbol = null break } + @Suppress("IfThenToElvis") currentScope = if (partSymbol is ClassDescriptor) partSymbol.defaultType.memberScope else if (partSymbol is PackageViewDescriptor) diff --git a/core/src/main/kotlin/Model/Content.kt b/core/src/main/kotlin/Model/Content.kt index 0a38a524..fcf94c12 100644 --- a/core/src/main/kotlin/Model/Content.kt +++ b/core/src/main/kotlin/Model/Content.kt @@ -82,7 +82,8 @@ class ContentEmphasis() : ContentBlock() class ContentStrong() : ContentBlock() class ContentStrikethrough() : ContentBlock() class ContentCode() : ContentBlock() -class ContentBlockCode(val language: String = "") : ContentBlock() +open class ContentBlockCode(val language: String = "") : ContentBlock() +class ContentBlockSampleCode(language: String = "kotlin") : ContentBlockCode(language) abstract class ContentNodeLink() : ContentBlock() { abstract val node: DocumentationNode? -- cgit