Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 1 | package org.jetbrains.dokka |
| 2 | |
Dmitry Jemerov | 0c584d0 | 2015-01-12 17:23:07 +0100 | [diff] [blame] | 3 | import org.jetbrains.dokka.LanguageService.RenderMode |
Dmitry Jemerov | 8827d30 | 2015-10-20 12:53:17 +0200 | [diff] [blame] | 4 | import java.util.* |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 5 | |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 6 | public data class FormatLink(val text: String, val href: String) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 7 | |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 8 | enum class ListKind { |
Dmitry Jemerov | 64414ce | 2015-05-29 13:52:43 +0200 | [diff] [blame] | 9 | Ordered, |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 10 | Unordered |
| 11 | } |
| 12 | |
Dmitry Jemerov | ea1f4cc | 2015-02-19 19:51:01 +0100 | [diff] [blame] | 13 | public abstract class StructuredFormatService(locationService: LocationService, |
| 14 | val languageService: LanguageService, |
| 15 | override val extension: String) : FormatService { |
| 16 | val locationService: LocationService = locationService.withExtension(extension) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 17 | |
Dmitry Jemerov | 44cef5c | 2015-02-26 19:34:49 +0100 | [diff] [blame] | 18 | abstract public fun appendBlockCode(to: StringBuilder, line: String, language: String) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 19 | abstract public fun appendHeader(to: StringBuilder, text: String, level: Int = 1) |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 20 | abstract public fun appendParagraph(to: StringBuilder, text: String) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 21 | abstract public fun appendLine(to: StringBuilder, text: String) |
| 22 | public abstract fun appendLine(to: StringBuilder) |
Dmitry Jemerov | 85a3ae7 | 2015-02-20 14:08:30 +0100 | [diff] [blame] | 23 | public abstract fun appendAnchor(to: StringBuilder, anchor: String) |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 24 | |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 25 | public abstract fun appendTable(to: StringBuilder, body: () -> Unit) |
| 26 | public abstract fun appendTableHeader(to: StringBuilder, body: () -> Unit) |
| 27 | public abstract fun appendTableBody(to: StringBuilder, body: () -> Unit) |
| 28 | public abstract fun appendTableRow(to: StringBuilder, body: () -> Unit) |
| 29 | public abstract fun appendTableCell(to: StringBuilder, body: () -> Unit) |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 30 | |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 31 | public abstract fun formatText(text: String): String |
Ilya Ryzhenkov | 7c6da4b | 2014-10-03 19:09:31 +0400 | [diff] [blame] | 32 | public abstract fun formatSymbol(text: String): String |
| 33 | public abstract fun formatKeyword(text: String): String |
Dmitry Jemerov | 4494fd0 | 2015-02-26 21:34:27 +0100 | [diff] [blame] | 34 | public abstract fun formatIdentifier(text: String, kind: IdentifierKind): String |
Dmitry Jemerov | 2e6eda9 | 2015-03-10 17:00:14 +0100 | [diff] [blame] | 35 | public fun formatEntity(text: String): String = text |
Ilya Ryzhenkov | 71cd87e | 2014-10-03 22:51:44 +0400 | [diff] [blame] | 36 | public abstract fun formatLink(text: String, href: String): String |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 37 | public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href) |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 38 | public abstract fun formatStrong(text: String): String |
Dmitry Jemerov | e17eaa5 | 2015-01-09 20:59:58 +0100 | [diff] [blame] | 39 | public abstract fun formatStrikethrough(text: String): String |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 40 | public abstract fun formatEmphasis(text: String): String |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 41 | public abstract fun formatCode(code: String): String |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 42 | public abstract fun formatUnorderedList(text: String): String |
| 43 | public abstract fun formatOrderedList(text: String): String |
| 44 | public abstract fun formatListItem(text: String, kind: ListKind): String |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 45 | public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String |
Dmitry Jemerov | 722c9af | 2015-02-26 16:28:05 +0100 | [diff] [blame] | 46 | public abstract fun formatNonBreakingSpace(): String |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 47 | public open fun formatSoftLineBreak(): String = "" |
| 48 | public open fun formatIndentedSoftLineBreak(): String = "" |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 49 | |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 50 | open fun formatText(location: Location, nodes: Iterable<ContentNode>, listKind: ListKind = ListKind.Unordered): String { |
Dmitry Jemerov | b8f2435 | 2015-10-21 18:26:10 +0200 | [diff] [blame] | 51 | return nodes.map { formatText(location, it, listKind) }.joinToString("") |
Ilya Ryzhenkov | 778e2b3 | 2014-09-29 20:54:59 +0400 | [diff] [blame] | 52 | } |
| 53 | |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 54 | open fun formatText(location: Location, content: ContentNode, listKind: ListKind = ListKind.Unordered): String { |
Dmitry Jemerov | b8f2435 | 2015-10-21 18:26:10 +0200 | [diff] [blame] | 55 | return StringBuilder().apply { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 56 | when (content) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 57 | is ContentText -> append(formatText(content.text)) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 58 | is ContentSymbol -> append(formatSymbol(content.text)) |
| 59 | is ContentKeyword -> append(formatKeyword(content.text)) |
Dmitry Jemerov | 4494fd0 | 2015-02-26 21:34:27 +0100 | [diff] [blame] | 60 | is ContentIdentifier -> append(formatIdentifier(content.text, content.kind)) |
Dmitry Jemerov | 722c9af | 2015-02-26 16:28:05 +0100 | [diff] [blame] | 61 | is ContentNonBreakingSpace -> append(formatNonBreakingSpace()) |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 62 | is ContentSoftLineBreak -> append(formatSoftLineBreak()) |
| 63 | is ContentIndentedSoftLineBreak -> append(formatIndentedSoftLineBreak()) |
Dmitry Jemerov | 2e6eda9 | 2015-03-10 17:00:14 +0100 | [diff] [blame] | 64 | is ContentEntity -> append(formatEntity(content.text)) |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 65 | is ContentStrong -> append(formatStrong(formatText(location, content.children))) |
Dmitry Jemerov | e17eaa5 | 2015-01-09 20:59:58 +0100 | [diff] [blame] | 66 | is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children))) |
Ilya Ryzhenkov | 9f0ff55 | 2014-10-13 13:38:40 +0400 | [diff] [blame] | 67 | is ContentCode -> append(formatCode(formatText(location, content.children))) |
| 68 | is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children))) |
Dmitry Jemerov | 6659337 | 2015-03-03 19:35:56 +0100 | [diff] [blame] | 69 | is ContentUnorderedList -> append(formatUnorderedList(formatText(location, content.children, ListKind.Unordered))) |
| 70 | is ContentOrderedList -> append(formatOrderedList(formatText(location, content.children, ListKind.Ordered))) |
| 71 | is ContentListItem -> append(formatListItem(formatText(location, content.children), listKind)) |
Ilya Ryzhenkov | 1839949 | 2014-12-22 09:50:17 +0200 | [diff] [blame] | 72 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 73 | is ContentNodeLink -> { |
Dmitry Jemerov | 184a24c | 2015-02-25 19:03:51 +0100 | [diff] [blame] | 74 | val node = content.node |
| 75 | val linkTo = if (node != null) locationHref(location, node) else "#" |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 76 | val linkText = formatText(location, content.children) |
Dmitry Jemerov | 4b61be3 | 2015-02-26 21:17:13 +0100 | [diff] [blame] | 77 | if (linkTo == ".") { |
| 78 | append(linkText) |
| 79 | } else { |
| 80 | append(formatLink(linkText, linkTo)) |
| 81 | } |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 82 | } |
Ilya Ryzhenkov | 71cd87e | 2014-10-03 22:51:44 +0400 | [diff] [blame] | 83 | is ContentExternalLink -> { |
| 84 | val linkText = formatText(location, content.children) |
Dmitry Jemerov | 4b61be3 | 2015-02-26 21:17:13 +0100 | [diff] [blame] | 85 | if (content.href == ".") { |
| 86 | append(linkText) |
| 87 | } else { |
| 88 | append(formatLink(linkText, content.href)) |
| 89 | } |
Ilya Ryzhenkov | 71cd87e | 2014-10-03 22:51:44 +0400 | [diff] [blame] | 90 | } |
Dmitry Jemerov | 76e4d3c | 2015-03-23 18:52:05 +0100 | [diff] [blame] | 91 | is ContentParagraph -> appendParagraph(this, formatText(location, content.children)) |
| 92 | is ContentBlockCode -> appendBlockCode(this, formatText(location, content.children), content.language) |
| 93 | is ContentHeading -> appendHeader(this, formatText(location, content.children), content.level) |
Dmitry Jemerov | 0d0fc1f | 2015-02-10 18:32:12 +0100 | [diff] [blame] | 94 | is ContentBlock -> append(formatText(location, content.children)) |
Ilya Ryzhenkov | 455d74a | 2014-09-19 22:25:27 +0300 | [diff] [blame] | 95 | } |
| 96 | }.toString() |
| 97 | } |
| 98 | |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 99 | open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension) |
| 100 | |
| 101 | open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink { |
Dmitry Jemerov | ea1f4cc | 2015-02-19 19:51:01 +0100 | [diff] [blame] | 102 | return FormatLink(to.name, locationService.relativePathToLocation(from, to)) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 103 | } |
| 104 | |
Dmitry Jemerov | 85a3ae7 | 2015-02-20 14:08:30 +0100 | [diff] [blame] | 105 | fun locationHref(from: Location, to: DocumentationNode): String { |
| 106 | val topLevelPage = to.references(DocumentationReference.Kind.TopLevelPage).singleOrNull()?.to |
| 107 | if (topLevelPage != null) { |
| 108 | return from.relativePathTo(locationService.location(topLevelPage), to.name) |
| 109 | } |
| 110 | return from.relativePathTo(locationService.location(to)) |
| 111 | } |
| 112 | |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 113 | fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) { |
| 114 | val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content } |
Dmitry Jemerov | bfd9ffd | 2015-01-30 17:59:15 +0100 | [diff] [blame] | 115 | |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 116 | for ((summary, items) in breakdownBySummary) { |
| 117 | items.forEach { |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 118 | val rendered = languageService.render(it) |
| 119 | appendAsSignature(to, rendered) { |
| 120 | to.append(formatCode(formatText(location, rendered))) |
Dmitry Jemerov | f90ee50 | 2015-02-26 13:55:37 +0100 | [diff] [blame] | 121 | it.appendSourceLink(to) |
| 122 | } |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 123 | it.appendOverrides(to) |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 124 | it.appendDeprecation(location, to) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 125 | } |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 126 | // All items have exactly the same documentation, so we can use any item to render it |
| 127 | val item = items.first() |
Dmitry Jemerov | f462317 | 2015-03-02 16:17:28 +0100 | [diff] [blame] | 128 | item.details(DocumentationNode.Kind.OverloadGroupNote).forEach { |
| 129 | to.append(formatText(location, it.content)) |
| 130 | } |
Dmitry Jemerov | ebbf265 | 2015-02-10 19:35:27 +0100 | [diff] [blame] | 131 | to.append(formatText(location, item.content.summary)) |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 132 | appendDescription(location, to, item) |
Dmitry Jemerov | ebbf265 | 2015-02-10 19:35:27 +0100 | [diff] [blame] | 133 | appendLine(to) |
| 134 | appendLine(to) |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Dmitry Jemerov | 76e4d3c | 2015-03-23 18:52:05 +0100 | [diff] [blame] | 138 | private fun DocumentationNode.isModuleOrPackage(): Boolean = |
| 139 | kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package |
| 140 | |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 141 | protected open fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) { |
Dmitry Jemerov | f90ee50 | 2015-02-26 13:55:37 +0100 | [diff] [blame] | 142 | block() |
| 143 | } |
| 144 | |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 145 | fun appendDescription(location: Location, to: StringBuilder, node: DocumentationNode) { |
| 146 | if (node.content.description != ContentEmpty) { |
| 147 | appendHeader(to, "Description", 3) |
| 148 | appendLine(to, formatText(location, node.content.description)) |
| 149 | appendLine(to) |
| 150 | } |
| 151 | node.content.getSectionsWithSubjects().forEach { |
Dmitry Jemerov | b8f2435 | 2015-10-21 18:26:10 +0200 | [diff] [blame] | 152 | appendSectionWithSubject(it.key, location, it.value, to) |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | for (section in node.content.sections.filter { it.subjectName == null }) { |
| 156 | appendLine(to, formatStrong(formatText(section.tag))) |
| 157 | appendLine(to, formatText(location, section)) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 161 | fun Content.getSectionsWithSubjects(): Map<String, List<ContentSection>> = |
| 162 | sections.filter { it.subjectName != null }.groupBy { it.tag } |
| 163 | |
| 164 | fun appendSectionWithSubject(title: String, location: Location, subjectSections: List<ContentSection>, to: StringBuilder) { |
Dmitry Jemerov | bfd9ffd | 2015-01-30 17:59:15 +0100 | [diff] [blame] | 165 | appendHeader(to, title, 3) |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 166 | subjectSections.forEach { |
| 167 | val subjectName = it.subjectName |
| 168 | if (subjectName != null) { |
Dmitry Jemerov | 85a3ae7 | 2015-02-20 14:08:30 +0100 | [diff] [blame] | 169 | appendAnchor(to, subjectName) |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 170 | to.append(formatCode(subjectName)).append(" - ") |
| 171 | to.append(formatText(location, it)) |
Dmitry Jemerov | bfd9ffd | 2015-01-30 17:59:15 +0100 | [diff] [blame] | 172 | appendLine(to) |
| 173 | } |
| 174 | } |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 177 | private fun DocumentationNode.appendOverrides(to: StringBuilder) { |
| 178 | overrides.forEach { |
| 179 | to.append("Overrides ") |
Dmitry Jemerov | ea1f4cc | 2015-02-19 19:51:01 +0100 | [diff] [blame] | 180 | val location = locationService.relativePathToLocation(this, it) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 181 | appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location))) |
| 182 | } |
| 183 | } |
| 184 | |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 185 | private fun DocumentationNode.appendDeprecation(location: Location, to: StringBuilder) { |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 186 | if (deprecation != null) { |
| 187 | val deprecationParameter = deprecation!!.details(DocumentationNode.Kind.Parameter).firstOrNull() |
| 188 | val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull() |
| 189 | if (deprecationValue != null) { |
Dmitry Jemerov | 7003ad8 | 2015-02-26 13:20:41 +0100 | [diff] [blame] | 190 | to.append(formatStrong("Deprecated:")).append(" ") |
Dmitry Jemerov | 64414ce | 2015-05-29 13:52:43 +0200 | [diff] [blame] | 191 | appendLine(to, formatText(deprecationValue.name.removeSurrounding("\""))) |
Dmitry Jemerov | f7c2f2a | 2015-02-26 19:45:36 +0100 | [diff] [blame] | 192 | appendLine(to) |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 193 | } else if (deprecation?.content != Content.Empty) { |
Dmitry Jemerov | 7003ad8 | 2015-02-26 13:20:41 +0100 | [diff] [blame] | 194 | to.append(formatStrong("Deprecated:")).append(" ") |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 195 | to.append(formatText(location, deprecation!!.content)) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 196 | } else { |
| 197 | appendLine(to, formatStrong("Deprecated")) |
Dmitry Jemerov | f7c2f2a | 2015-02-26 19:45:36 +0100 | [diff] [blame] | 198 | appendLine(to) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 203 | private fun DocumentationNode.appendSourceLink(to: StringBuilder) { |
| 204 | val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull() |
| 205 | if (sourceUrl != null) { |
Dmitry Jemerov | ebbf265 | 2015-02-10 19:35:27 +0100 | [diff] [blame] | 206 | to.append(" ") |
| 207 | appendLine(to, formatLink("(source)", sourceUrl.name)) |
| 208 | } else { |
| 209 | appendLine(to) |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 213 | fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Dmitry Jemerov | 76e4d3c | 2015-03-23 18:52:05 +0100 | [diff] [blame] | 214 | val singleNode = nodes.singleOrNull() |
| 215 | if (singleNode != null && singleNode.isModuleOrPackage()) { |
| 216 | if (singleNode.kind == DocumentationNode.Kind.Package) { |
| 217 | appendHeader(to, "Package " + formatText(singleNode.name), 2) |
| 218 | } |
| 219 | to.append(formatText(location, singleNode.content)) |
| 220 | } else { |
| 221 | val breakdownByName = nodes.groupBy { node -> node.name } |
| 222 | for ((name, items) in breakdownByName) { |
| 223 | appendHeader(to, formatText(name)) |
| 224 | appendDocumentation(location, to, items) |
| 225 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 229 | private fun appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) { |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 230 | if (nodes.any()) { |
| 231 | appendHeader(to, caption, 3) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 232 | |
Dmitry Jemerov | 9725737 | 2015-09-07 20:57:17 +0200 | [diff] [blame] | 233 | val children = nodes.sortedBy { it.name } |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 234 | val membersMap = children.groupBy { link(node, it) } |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 235 | |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 236 | appendTable(to) { |
| 237 | appendTableBody(to) { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 238 | for ((memberLocation, members) in membersMap) { |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 239 | appendTableRow(to) { |
| 240 | appendTableCell(to) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 241 | to.append(formatLink(memberLocation)) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 242 | } |
| 243 | appendTableCell(to) { |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 244 | val breakdownBySummary = members.groupBy { formatText(location, it.summary) } |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 245 | for ((summary, items) in breakdownBySummary) { |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 246 | appendSummarySignatures(items, location, to) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 247 | if (!summary.isEmpty()) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 248 | to.append(summary) |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
Ilya Ryzhenkov | e8447fd | 2014-07-15 16:37:50 +0400 | [diff] [blame] | 252 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 253 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 254 | } |
| 255 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 259 | private fun appendSummarySignatures(items: List<DocumentationNode>, location: Location, to: StringBuilder) { |
| 260 | val summarySignature = languageService.summarizeSignatures(items) |
| 261 | if (summarySignature != null) { |
Dmitry Jemerov | 0efda36 | 2015-10-28 11:38:40 +0100 | [diff] [blame] | 262 | appendAsSignature(to, summarySignature) { |
| 263 | appendLine(to, summarySignature.signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 264 | } |
| 265 | return |
| 266 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 267 | val renderedSignatures = items.map { languageService.render(it, RenderMode.SUMMARY) } |
| 268 | renderedSignatures.subList(0, renderedSignatures.size - 1).forEach { |
| 269 | appendAsSignature(to, it) { |
| 270 | appendLine(to, it.signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 273 | appendAsSignature(to, renderedSignatures.last()) { |
| 274 | to.append(renderedSignatures.last().signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 275 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | private fun ContentNode.signatureToText(location: Location): String { |
| 279 | return if (this is ContentBlock && this.isEmpty()) { |
| 280 | "" |
| 281 | } else { |
| 282 | val signatureAsCode = ContentCode() |
| 283 | signatureAsCode.append(this) |
| 284 | formatText(location, signatureAsCode) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 288 | override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
| 289 | val breakdownByLocation = nodes.groupBy { node -> |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 290 | formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) }) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | for ((breadcrumbs, items) in breakdownByLocation) { |
| 294 | appendLine(to, breadcrumbs) |
| 295 | appendLine(to) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 296 | appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass }) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | for (node in nodes) { |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 300 | if (node.kind == DocumentationNode.Kind.ExternalClass) { |
| 301 | appendSection(location, "Extensions for ${node.name}", node.members, node, to) |
| 302 | continue |
| 303 | } |
| 304 | |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 305 | appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to) |
| 306 | appendSection(location, "Types", node.members.filter { |
| 307 | it.kind in setOf( |
| 308 | DocumentationNode.Kind.Class, |
| 309 | DocumentationNode.Kind.Interface, |
| 310 | DocumentationNode.Kind.Enum, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 311 | DocumentationNode.Kind.Object, |
| 312 | DocumentationNode.Kind.AnnotationClass) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 313 | }, node, to) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 314 | appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 315 | appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to) |
| 316 | appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to) |
| 317 | appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to) |
Dmitry Jemerov | c7916f7 | 2015-03-17 19:54:11 +0100 | [diff] [blame] | 318 | appendSection(location, "Companion Object Properties", node.members(DocumentationNode.Kind.CompanionObjectProperty), node, to) |
| 319 | appendSection(location, "Companion Object Functions", node.members(DocumentationNode.Kind.CompanionObjectFunction), node, to) |
Dmitry Jemerov | c4f40a0 | 2015-01-12 16:32:30 +0100 | [diff] [blame] | 320 | appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 321 | appendSection(location, "Other members", node.members.filter { |
| 322 | it.kind !in setOf( |
| 323 | DocumentationNode.Kind.Class, |
| 324 | DocumentationNode.Kind.Interface, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 325 | DocumentationNode.Kind.Enum, |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 326 | DocumentationNode.Kind.Object, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 327 | DocumentationNode.Kind.AnnotationClass, |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 328 | DocumentationNode.Kind.Constructor, |
| 329 | DocumentationNode.Kind.Property, |
| 330 | DocumentationNode.Kind.Package, |
| 331 | DocumentationNode.Kind.Function, |
Dmitry Jemerov | c7916f7 | 2015-03-17 19:54:11 +0100 | [diff] [blame] | 332 | DocumentationNode.Kind.CompanionObjectProperty, |
| 333 | DocumentationNode.Kind.CompanionObjectFunction, |
Dmitry Jemerov | c4f40a0 | 2015-01-12 16:32:30 +0100 | [diff] [blame] | 334 | DocumentationNode.Kind.ExternalClass, |
| 335 | DocumentationNode.Kind.EnumItem |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 336 | ) |
| 337 | }, node, to) |
Dmitry Jemerov | 4f59034 | 2015-02-26 19:19:55 +0100 | [diff] [blame] | 338 | appendSection(location, "Extension Properties", node.extensions.filter { it.kind == DocumentationNode.Kind.Property }, node, to) |
| 339 | appendSection(location, "Extension Functions", node.extensions.filter { it.kind == DocumentationNode.Kind.Function }, node, to) |
Dmitry Jemerov | c7916f7 | 2015-03-17 19:54:11 +0100 | [diff] [blame] | 340 | appendSection(location, "Companion Object Extension Properties", node.extensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectProperty }, node, to) |
| 341 | appendSection(location, "Companion Object Extension Functions", node.extensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectFunction }, node, to) |
Dmitry Jemerov | c4f40a0 | 2015-01-12 16:32:30 +0100 | [diff] [blame] | 342 | appendSection(location, "Inheritors", |
| 343 | node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 344 | appendSection(location, "Links", node.links, node, to) |
| 345 | |
| 346 | } |
| 347 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 348 | } |