From 8291bee3a86f5f2409ba4ae44c87f291c664ac19 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Tue, 27 Oct 2015 18:37:35 +0100 Subject: wrapping and nicer formatting for signatures --- src/Formats/KotlinWebsiteFormatService.kt | 27 +++++++++++++---- src/Formats/StructuredFormatService.kt | 49 ++++++++++++++++++------------- 2 files changed, 50 insertions(+), 26 deletions(-) (limited to 'src/Formats') diff --git a/src/Formats/KotlinWebsiteFormatService.kt b/src/Formats/KotlinWebsiteFormatService.kt index 97419c58..b66fa1a7 100644 --- a/src/Formats/KotlinWebsiteFormatService.kt +++ b/src/Formats/KotlinWebsiteFormatService.kt @@ -3,6 +3,8 @@ package org.jetbrains.dokka public class KotlinWebsiteFormatService(locationService: LocationService, signatureGenerator: LanguageService) : JekyllFormatService(locationService, signatureGenerator) { + private var needHardLineBreaks = false + override fun appendFrontMatter(nodes: Iterable, to: StringBuilder) { super.appendFrontMatter(nodes, to) to.appendln("layout: api") @@ -24,12 +26,17 @@ public class KotlinWebsiteFormatService(locationService: LocationService, override fun formatStrikethrough(text: String): String = "$text" - override fun appendAsSignature(to: StringBuilder, block: () -> Unit) { - val oldLength = to.length - block() - if (to.length > oldLength) { - to.append("
") // since we've used HTML to format the signature, add an HTML line break following it + override fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) { + val contentLength = node.textLength + if (contentLength == 0) return + to.append("
") + needHardLineBreaks = contentLength >= 62 + try { + block() + } finally { + needHardLineBreaks = false } + to.append("
") } override fun formatLink(text: String, href: String): String { @@ -88,6 +95,16 @@ public class KotlinWebsiteFormatService(locationService: LocationService, return "${formatText(text)}" } + override fun formatSoftLineBreak(): String = if (needHardLineBreaks) + "
" + else + "" + + override fun formatIndentedSoftLineBreak(): String = if (needHardLineBreaks) + "
    " + else + "" + private fun identifierClassName(kind: IdentifierKind) = when(kind) { IdentifierKind.ParameterName -> "parameterName" IdentifierKind.SummarizedTypeName -> "summarizedTypeName" diff --git a/src/Formats/StructuredFormatService.kt b/src/Formats/StructuredFormatService.kt index 405a8b10..5a8109dc 100644 --- a/src/Formats/StructuredFormatService.kt +++ b/src/Formats/StructuredFormatService.kt @@ -44,6 +44,8 @@ public abstract class StructuredFormatService(locationService: LocationService, public abstract fun formatListItem(text: String, kind: ListKind): String public abstract fun formatBreadcrumbs(items: Iterable): String public abstract fun formatNonBreakingSpace(): String + public open fun formatSoftLineBreak(): String = "" + public open fun formatIndentedSoftLineBreak(): String = "" open fun formatText(location: Location, nodes: Iterable, listKind: ListKind = ListKind.Unordered): String { return nodes.map { formatText(location, it, listKind) }.joinToString("") @@ -57,6 +59,8 @@ public abstract class StructuredFormatService(locationService: LocationService, is ContentKeyword -> append(formatKeyword(content.text)) is ContentIdentifier -> append(formatIdentifier(content.text, content.kind)) is ContentNonBreakingSpace -> append(formatNonBreakingSpace()) + is ContentSoftLineBreak -> append(formatSoftLineBreak()) + is ContentIndentedSoftLineBreak -> append(formatIndentedSoftLineBreak()) is ContentEntity -> append(formatEntity(content.text)) is ContentStrong -> append(formatStrong(formatText(location, content.children))) is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children))) @@ -111,8 +115,9 @@ public abstract class StructuredFormatService(locationService: LocationService, for ((summary, items) in breakdownBySummary) { items.forEach { - appendAsSignature(to) { - to.append(formatCode(formatText(location, languageService.render(it)))) + val rendered = languageService.render(it) + appendAsSignature(to, rendered) { + to.append(formatCode(formatText(location, rendered))) it.appendSourceLink(to) } it.appendOverrides(to) @@ -133,7 +138,7 @@ public abstract class StructuredFormatService(locationService: LocationService, private fun DocumentationNode.isModuleOrPackage(): Boolean = kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package - protected open fun appendAsSignature(to: StringBuilder, block: () -> Unit) { + protected open fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) { block() } @@ -254,30 +259,32 @@ public abstract class StructuredFormatService(locationService: LocationService, private fun appendSummarySignatures(items: List, location: Location, to: StringBuilder) { val summarySignature = languageService.summarizeSignatures(items) if (summarySignature != null) { - appendAsSignature(to) { - val signatureAsCode = ContentCode() - signatureAsCode.append(summarySignature) - to.append(formatText(location, signatureAsCode)) + val signatureAsCode = ContentCode() + signatureAsCode.append(summarySignature) + appendAsSignature(to, signatureAsCode) { + appendLine(to, signatureAsCode.signatureToText(location)) } return } - val signatureTexts = items.map { signature -> - val signatureText = languageService.render(signature, RenderMode.SUMMARY) - if (signatureText is ContentBlock && signatureText.isEmpty()) { - "" - } else { - val signatureAsCode = ContentCode() - signatureAsCode.append(signatureText) - formatText(location, signatureAsCode) + val renderedSignatures = items.map { languageService.render(it, RenderMode.SUMMARY) } + renderedSignatures.subList(0, renderedSignatures.size - 1).forEach { + appendAsSignature(to, it) { + appendLine(to, it.signatureToText(location)) } + appendLine(to) } - signatureTexts.subList(0, signatureTexts.size - 1).forEach { - appendAsSignature(to) { - appendLine(to, it) - } + appendAsSignature(to, renderedSignatures.last()) { + to.append(renderedSignatures.last().signatureToText(location)) } - appendAsSignature(to) { - to.append(signatureTexts.last()) + } + + private fun ContentNode.signatureToText(location: Location): String { + return if (this is ContentBlock && this.isEmpty()) { + "" + } else { + val signatureAsCode = ContentCode() + signatureAsCode.append(this) + formatText(location, signatureAsCode) } } -- cgit