From 050b0452c82daf75974c0a8f2c26524985d53947 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Thu, 3 Dec 2015 17:52:57 +0100 Subject: overload group formatting fixes --- core/src/main/kotlin/Formats/HtmlFormatService.kt | 6 +-- .../kotlin/Formats/KotlinWebsiteFormatService.kt | 45 ++++++++++++++----- .../main/kotlin/Formats/MarkdownFormatService.kt | 4 -- .../main/kotlin/Formats/StructuredFormatService.kt | 50 +++++++++++++--------- 4 files changed, 65 insertions(+), 40 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/Formats/HtmlFormatService.kt b/core/src/main/kotlin/Formats/HtmlFormatService.kt index a58319bc..d513e41f 100644 --- a/core/src/main/kotlin/Formats/HtmlFormatService.kt +++ b/core/src/main/kotlin/Formats/HtmlFormatService.kt @@ -41,11 +41,7 @@ public open class HtmlFormatService @Inject constructor(@Named("folders") locati } override fun appendLine(to: StringBuilder, text: String) { - to.appendln("${text}
") - } - - override fun appendLine(to: StringBuilder) { - to.appendln("
") + to.appendln("$text
") } override fun appendAnchor(to: StringBuilder, anchor: String) { diff --git a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt index 4eda7910..870347ab 100644 --- a/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt +++ b/core/src/main/kotlin/Formats/KotlinWebsiteFormatService.kt @@ -6,6 +6,7 @@ public class KotlinWebsiteFormatService @Inject constructor(locationService: Loc signatureGenerator: LanguageService) : JekyllFormatService(locationService, signatureGenerator, "html") { private var needHardLineBreaks = false + private var insideDiv = 0 override fun appendFrontMatter(nodes: Iterable, to: StringBuilder) { super.appendFrontMatter(nodes, to) @@ -28,29 +29,53 @@ public class KotlinWebsiteFormatService @Inject constructor(locationService: Loc override fun formatStrikethrough(text: String): String = "$text" + private fun div(to: StringBuilder, cssClass: String, block: () -> Unit) { + to.append("
") + insideDiv++ + block() + insideDiv-- + to.append("
\n") + } + 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 + div(to, "signature") { + needHardLineBreaks = contentLength >= 62 + try { + block() + } finally { + needHardLineBreaks = false + } } - to.append("
") } override fun appendAsOverloadGroup(to: StringBuilder, block: () -> Unit) { - to.append("
\n") - block() - to.append("
\n") + div(to, "overload-group", block) } override fun formatLink(text: String, href: String): String { return "${text}" } + override fun appendHeader(to: StringBuilder, text: String, level: Int) { + if (insideDiv > 0) { + to.appendln("${text}") + } + else { + super.appendHeader(to, text, level) + } + } + + override fun appendLine(to: StringBuilder, text: String) { + if (insideDiv > 0) { + to.appendln("$text
") + } + else { + super.appendLine(to, text) + } + } + override fun appendTable(to: StringBuilder, body: () -> Unit) { to.appendln("") body() diff --git a/core/src/main/kotlin/Formats/MarkdownFormatService.kt b/core/src/main/kotlin/Formats/MarkdownFormatService.kt index f694ae3e..07202b7e 100644 --- a/core/src/main/kotlin/Formats/MarkdownFormatService.kt +++ b/core/src/main/kotlin/Formats/MarkdownFormatService.kt @@ -55,10 +55,6 @@ public open class MarkdownFormatService return "[$text]($href)" } - override public fun appendLine(to: StringBuilder) { - to.appendln() - } - override public fun appendLine(to: StringBuilder, text: String) { to.appendln(text) } diff --git a/core/src/main/kotlin/Formats/StructuredFormatService.kt b/core/src/main/kotlin/Formats/StructuredFormatService.kt index 32a2b68a..324f156a 100644 --- a/core/src/main/kotlin/Formats/StructuredFormatService.kt +++ b/core/src/main/kotlin/Formats/StructuredFormatService.kt @@ -19,8 +19,7 @@ abstract class StructuredFormatService(locationService: LocationService, abstract fun appendBlockCode(to: StringBuilder, line: String, language: String) abstract fun appendHeader(to: StringBuilder, text: String, level: Int = 1) abstract fun appendParagraph(to: StringBuilder, text: String) - abstract fun appendLine(to: StringBuilder, text: String) - abstract fun appendLine(to: StringBuilder) + abstract fun appendLine(to: StringBuilder, text: String = "") abstract fun appendAnchor(to: StringBuilder, anchor: String) abstract fun appendTable(to: StringBuilder, body: () -> Unit) @@ -114,30 +113,39 @@ abstract class StructuredFormatService(locationService: LocationService, fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable) { val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content } - for ((summary, items) in breakdownBySummary) { - appendAsOverloadGroup(to) { - items.forEach { - val rendered = languageService.render(it) - appendAsSignature(to, rendered) { - to.append(formatCode(formatText(location, rendered))) - it.appendSourceLink(to) - } - it.appendOverrides(to) - it.appendDeprecation(location, to) - } - // All items have exactly the same documentation, so we can use any item to render it - val item = items.first() - item.details(DocumentationNode.Kind.OverloadGroupNote).forEach { - to.append(formatText(location, it.content)) + if (breakdownBySummary.size == 1) { + formatOverloadGroup(breakdownBySummary.values.single(), location, to) + } + else { + for ((summary, items) in breakdownBySummary) { + appendAsOverloadGroup(to) { + formatOverloadGroup(items, location, to) } - to.append(formatText(location, item.content.summary)) - appendDescription(location, to, item) - appendLine(to) - appendLine(to) } } } + private fun formatOverloadGroup(items: MutableList, location: Location, to: StringBuilder) { + items.forEach { + val rendered = languageService.render(it) + appendAsSignature(to, rendered) { + to.append(formatCode(formatText(location, rendered))) + it.appendSourceLink(to) + } + it.appendOverrides(to) + it.appendDeprecation(location, to) + } + // All items have exactly the same documentation, so we can use any item to render it + val item = items.first() + item.details(DocumentationNode.Kind.OverloadGroupNote).forEach { + to.append(formatText(location, it.content)) + } + to.append(formatText(location, item.content.summary)) + appendDescription(location, to, item) + appendLine(to) + appendLine(to) + } + private fun DocumentationNode.isModuleOrPackage(): Boolean = kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package -- cgit