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 | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 6 | 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 | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 13 | abstract class StructuredFormatService(locationService: LocationService, |
Dmitry Jemerov | ea1f4cc | 2015-02-19 19:51:01 +0100 | [diff] [blame] | 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 | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 18 | abstract fun appendBlockCode(to: StringBuilder, line: String, language: String) |
| 19 | abstract fun appendHeader(to: StringBuilder, text: String, level: Int = 1) |
| 20 | abstract fun appendParagraph(to: StringBuilder, text: String) |
| 21 | abstract fun appendLine(to: StringBuilder, text: String) |
| 22 | abstract fun appendLine(to: StringBuilder) |
| 23 | abstract fun appendAnchor(to: StringBuilder, anchor: String) |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 24 | |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 25 | abstract fun appendTable(to: StringBuilder, body: () -> Unit) |
| 26 | abstract fun appendTableHeader(to: StringBuilder, body: () -> Unit) |
| 27 | abstract fun appendTableBody(to: StringBuilder, body: () -> Unit) |
| 28 | abstract fun appendTableRow(to: StringBuilder, body: () -> Unit) |
| 29 | abstract fun appendTableCell(to: StringBuilder, body: () -> Unit) |
Ilya Ryzhenkov | 499d082 | 2014-07-15 16:18:53 +0400 | [diff] [blame] | 30 | |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 31 | abstract fun formatText(text: String): String |
| 32 | abstract fun formatSymbol(text: String): String |
| 33 | abstract fun formatKeyword(text: String): String |
| 34 | abstract fun formatIdentifier(text: String, kind: IdentifierKind): String |
| 35 | fun formatEntity(text: String): String = text |
| 36 | abstract fun formatLink(text: String, href: String): String |
| 37 | open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href) |
| 38 | abstract fun formatStrong(text: String): String |
| 39 | abstract fun formatStrikethrough(text: String): String |
| 40 | abstract fun formatEmphasis(text: String): String |
| 41 | abstract fun formatCode(code: String): String |
| 42 | abstract fun formatUnorderedList(text: String): String |
| 43 | abstract fun formatOrderedList(text: String): String |
| 44 | abstract fun formatListItem(text: String, kind: ListKind): String |
| 45 | abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String |
| 46 | abstract fun formatNonBreakingSpace(): String |
| 47 | open fun formatSoftLineBreak(): String = "" |
| 48 | 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 | |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 99 | open fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 100 | |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 101 | open 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) { |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 117 | appendAsOverloadGroup(to) { |
| 118 | items.forEach { |
| 119 | val rendered = languageService.render(it) |
| 120 | appendAsSignature(to, rendered) { |
| 121 | to.append(formatCode(formatText(location, rendered))) |
| 122 | it.appendSourceLink(to) |
| 123 | } |
| 124 | it.appendOverrides(to) |
| 125 | it.appendDeprecation(location, to) |
Dmitry Jemerov | f90ee50 | 2015-02-26 13:55:37 +0100 | [diff] [blame] | 126 | } |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 127 | // All items have exactly the same documentation, so we can use any item to render it |
| 128 | val item = items.first() |
| 129 | item.details(DocumentationNode.Kind.OverloadGroupNote).forEach { |
| 130 | to.append(formatText(location, it.content)) |
| 131 | } |
| 132 | to.append(formatText(location, item.content.summary)) |
| 133 | appendDescription(location, to, item) |
| 134 | appendLine(to) |
| 135 | appendLine(to) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 136 | } |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Dmitry Jemerov | 76e4d3c | 2015-03-23 18:52:05 +0100 | [diff] [blame] | 140 | private fun DocumentationNode.isModuleOrPackage(): Boolean = |
| 141 | kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package |
| 142 | |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 143 | protected open fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) { |
Dmitry Jemerov | f90ee50 | 2015-02-26 13:55:37 +0100 | [diff] [blame] | 144 | block() |
| 145 | } |
| 146 | |
Dmitry Jemerov | f99ec73 | 2015-10-29 11:54:27 +0100 | [diff] [blame^] | 147 | protected open fun appendAsOverloadGroup(to: StringBuilder, block: () -> Unit) { |
| 148 | block() |
| 149 | } |
| 150 | |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 151 | fun appendDescription(location: Location, to: StringBuilder, node: DocumentationNode) { |
| 152 | if (node.content.description != ContentEmpty) { |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 153 | appendLine(to, formatText(location, node.content.description)) |
| 154 | appendLine(to) |
| 155 | } |
| 156 | node.content.getSectionsWithSubjects().forEach { |
Dmitry Jemerov | b8f2435 | 2015-10-21 18:26:10 +0200 | [diff] [blame] | 157 | appendSectionWithSubject(it.key, location, it.value, to) |
Dmitry Jemerov | e1a3884 | 2015-02-10 18:55:12 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | for (section in node.content.sections.filter { it.subjectName == null }) { |
| 161 | appendLine(to, formatStrong(formatText(section.tag))) |
| 162 | appendLine(to, formatText(location, section)) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 166 | fun Content.getSectionsWithSubjects(): Map<String, List<ContentSection>> = |
| 167 | sections.filter { it.subjectName != null }.groupBy { it.tag } |
| 168 | |
| 169 | fun appendSectionWithSubject(title: String, location: Location, subjectSections: List<ContentSection>, to: StringBuilder) { |
Dmitry Jemerov | bfd9ffd | 2015-01-30 17:59:15 +0100 | [diff] [blame] | 170 | appendHeader(to, title, 3) |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 171 | subjectSections.forEach { |
| 172 | val subjectName = it.subjectName |
| 173 | if (subjectName != null) { |
Dmitry Jemerov | 85a3ae7 | 2015-02-20 14:08:30 +0100 | [diff] [blame] | 174 | appendAnchor(to, subjectName) |
Dmitry Jemerov | 0fac1d9 | 2015-01-30 19:01:40 +0100 | [diff] [blame] | 175 | to.append(formatCode(subjectName)).append(" - ") |
| 176 | to.append(formatText(location, it)) |
Dmitry Jemerov | bfd9ffd | 2015-01-30 17:59:15 +0100 | [diff] [blame] | 177 | appendLine(to) |
| 178 | } |
| 179 | } |
Dmitry Jemerov | c43a437 | 2014-12-29 20:22:43 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 182 | private fun DocumentationNode.appendOverrides(to: StringBuilder) { |
| 183 | overrides.forEach { |
| 184 | to.append("Overrides ") |
Dmitry Jemerov | ea1f4cc | 2015-02-19 19:51:01 +0100 | [diff] [blame] | 185 | val location = locationService.relativePathToLocation(this, it) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 186 | appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location))) |
| 187 | } |
| 188 | } |
| 189 | |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 190 | private fun DocumentationNode.appendDeprecation(location: Location, to: StringBuilder) { |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 191 | if (deprecation != null) { |
| 192 | val deprecationParameter = deprecation!!.details(DocumentationNode.Kind.Parameter).firstOrNull() |
| 193 | val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull() |
| 194 | if (deprecationValue != null) { |
Dmitry Jemerov | 7003ad8 | 2015-02-26 13:20:41 +0100 | [diff] [blame] | 195 | to.append(formatStrong("Deprecated:")).append(" ") |
Dmitry Jemerov | 64414ce | 2015-05-29 13:52:43 +0200 | [diff] [blame] | 196 | appendLine(to, formatText(deprecationValue.name.removeSurrounding("\""))) |
Dmitry Jemerov | f7c2f2a | 2015-02-26 19:45:36 +0100 | [diff] [blame] | 197 | appendLine(to) |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 198 | } else if (deprecation?.content != Content.Empty) { |
Dmitry Jemerov | 7003ad8 | 2015-02-26 13:20:41 +0100 | [diff] [blame] | 199 | to.append(formatStrong("Deprecated:")).append(" ") |
Dmitry Jemerov | a60d8ba | 2015-02-24 16:24:00 +0100 | [diff] [blame] | 200 | to.append(formatText(location, deprecation!!.content)) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 201 | } else { |
| 202 | appendLine(to, formatStrong("Deprecated")) |
Dmitry Jemerov | f7c2f2a | 2015-02-26 19:45:36 +0100 | [diff] [blame] | 203 | appendLine(to) |
Dmitry Jemerov | 0dd5ea3 | 2015-01-14 13:30:43 +0100 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 208 | private fun DocumentationNode.appendSourceLink(to: StringBuilder) { |
| 209 | val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull() |
| 210 | if (sourceUrl != null) { |
Dmitry Jemerov | ebbf265 | 2015-02-10 19:35:27 +0100 | [diff] [blame] | 211 | to.append(" ") |
| 212 | appendLine(to, formatLink("(source)", sourceUrl.name)) |
| 213 | } else { |
| 214 | appendLine(to) |
Dmitry Jemerov | 6146fa8 | 2015-01-14 18:46:36 +0100 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 218 | fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
Dmitry Jemerov | 76e4d3c | 2015-03-23 18:52:05 +0100 | [diff] [blame] | 219 | val singleNode = nodes.singleOrNull() |
| 220 | if (singleNode != null && singleNode.isModuleOrPackage()) { |
| 221 | if (singleNode.kind == DocumentationNode.Kind.Package) { |
| 222 | appendHeader(to, "Package " + formatText(singleNode.name), 2) |
| 223 | } |
| 224 | to.append(formatText(location, singleNode.content)) |
| 225 | } else { |
| 226 | val breakdownByName = nodes.groupBy { node -> node.name } |
| 227 | for ((name, items) in breakdownByName) { |
| 228 | appendHeader(to, formatText(name)) |
| 229 | appendDocumentation(location, to, items) |
| 230 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 234 | 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] | 235 | if (nodes.any()) { |
| 236 | appendHeader(to, caption, 3) |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 237 | |
Dmitry Jemerov | 9725737 | 2015-09-07 20:57:17 +0200 | [diff] [blame] | 238 | val children = nodes.sortedBy { it.name } |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 239 | val membersMap = children.groupBy { link(node, it) } |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 240 | |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 241 | appendTable(to) { |
| 242 | appendTableBody(to) { |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 243 | for ((memberLocation, members) in membersMap) { |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 244 | appendTableRow(to) { |
| 245 | appendTableCell(to) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 246 | to.append(formatLink(memberLocation)) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 247 | } |
| 248 | appendTableCell(to) { |
Ilya Ryzhenkov | 280dc29 | 2014-10-14 16:08:10 +0400 | [diff] [blame] | 249 | val breakdownBySummary = members.groupBy { formatText(location, it.summary) } |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 250 | for ((summary, items) in breakdownBySummary) { |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 251 | appendSummarySignatures(items, location, to) |
Ilya Ryzhenkov | a52e1d5 | 2014-10-03 15:57:16 +0400 | [diff] [blame] | 252 | if (!summary.isEmpty()) { |
Dmitry Jemerov | 8ef6818 | 2014-12-30 12:36:14 +0100 | [diff] [blame] | 253 | to.append(summary) |
Ilya Ryzhenkov | aa59acb | 2014-07-15 20:05:55 +0400 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
Ilya Ryzhenkov | e8447fd | 2014-07-15 16:37:50 +0400 | [diff] [blame] | 257 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 258 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 259 | } |
| 260 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 264 | private fun appendSummarySignatures(items: List<DocumentationNode>, location: Location, to: StringBuilder) { |
| 265 | val summarySignature = languageService.summarizeSignatures(items) |
| 266 | if (summarySignature != null) { |
Dmitry Jemerov | 0efda36 | 2015-10-28 11:38:40 +0100 | [diff] [blame] | 267 | appendAsSignature(to, summarySignature) { |
| 268 | appendLine(to, summarySignature.signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 269 | } |
| 270 | return |
| 271 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 272 | val renderedSignatures = items.map { languageService.render(it, RenderMode.SUMMARY) } |
| 273 | renderedSignatures.subList(0, renderedSignatures.size - 1).forEach { |
| 274 | appendAsSignature(to, it) { |
| 275 | appendLine(to, it.signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 276 | } |
| 277 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 278 | appendAsSignature(to, renderedSignatures.last()) { |
| 279 | to.append(renderedSignatures.last().signatureToText(location)) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 280 | } |
Dmitry Jemerov | 8291bee | 2015-10-27 18:37:35 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | private fun ContentNode.signatureToText(location: Location): String { |
| 284 | return if (this is ContentBlock && this.isEmpty()) { |
| 285 | "" |
| 286 | } else { |
| 287 | val signatureAsCode = ContentCode() |
| 288 | signatureAsCode.append(this) |
| 289 | formatText(location, signatureAsCode) |
Dmitry Jemerov | 3faa3f2 | 2015-10-27 17:12:31 +0100 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 293 | override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) { |
| 294 | val breakdownByLocation = nodes.groupBy { node -> |
Dmitry Jemerov | d9bfa02 | 2015-02-19 18:59:00 +0100 | [diff] [blame] | 295 | formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) }) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | for ((breadcrumbs, items) in breakdownByLocation) { |
| 299 | appendLine(to, breadcrumbs) |
| 300 | appendLine(to) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 301 | appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass }) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | for (node in nodes) { |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 305 | if (node.kind == DocumentationNode.Kind.ExternalClass) { |
| 306 | appendSection(location, "Extensions for ${node.name}", node.members, node, to) |
| 307 | continue |
| 308 | } |
| 309 | |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 310 | appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to) |
| 311 | appendSection(location, "Types", node.members.filter { |
| 312 | it.kind in setOf( |
| 313 | DocumentationNode.Kind.Class, |
| 314 | DocumentationNode.Kind.Interface, |
| 315 | DocumentationNode.Kind.Enum, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 316 | DocumentationNode.Kind.Object, |
| 317 | DocumentationNode.Kind.AnnotationClass) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 318 | }, node, to) |
Dmitry Jemerov | 4b0dcee | 2015-01-09 19:48:44 +0100 | [diff] [blame] | 319 | appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to) |
Dmitry Jemerov | c81053d | 2015-10-28 17:34:44 +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, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to) |
| 322 | appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to) |
Dmitry Jemerov | 36f4b91 | 2015-10-28 15:23:50 +0100 | [diff] [blame] | 323 | appendSection(location, "Inherited Properties", node.inheritedMembers(DocumentationNode.Kind.Property), node, to) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 324 | appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to) |
Dmitry Jemerov | 36f4b91 | 2015-10-28 15:23:50 +0100 | [diff] [blame] | 325 | appendSection(location, "Inherited Functions", node.inheritedMembers(DocumentationNode.Kind.Function), node, to) |
Dmitry Jemerov | c7916f7 | 2015-03-17 19:54:11 +0100 | [diff] [blame] | 326 | appendSection(location, "Companion Object Properties", node.members(DocumentationNode.Kind.CompanionObjectProperty), node, to) |
| 327 | appendSection(location, "Companion Object Functions", node.members(DocumentationNode.Kind.CompanionObjectFunction), node, to) |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 328 | appendSection(location, "Other members", node.members.filter { |
| 329 | it.kind !in setOf( |
| 330 | DocumentationNode.Kind.Class, |
| 331 | DocumentationNode.Kind.Interface, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 332 | DocumentationNode.Kind.Enum, |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 333 | DocumentationNode.Kind.Object, |
Dmitry Jemerov | 716483c | 2014-12-30 17:41:14 +0100 | [diff] [blame] | 334 | DocumentationNode.Kind.AnnotationClass, |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 335 | DocumentationNode.Kind.Constructor, |
| 336 | DocumentationNode.Kind.Property, |
| 337 | DocumentationNode.Kind.Package, |
| 338 | DocumentationNode.Kind.Function, |
Dmitry Jemerov | c7916f7 | 2015-03-17 19:54:11 +0100 | [diff] [blame] | 339 | DocumentationNode.Kind.CompanionObjectProperty, |
| 340 | DocumentationNode.Kind.CompanionObjectFunction, |
Dmitry Jemerov | c4f40a0 | 2015-01-12 16:32:30 +0100 | [diff] [blame] | 341 | DocumentationNode.Kind.ExternalClass, |
| 342 | DocumentationNode.Kind.EnumItem |
Ilya Ryzhenkov | 4907736 | 2014-10-14 19:53:13 +0400 | [diff] [blame] | 343 | ) |
| 344 | }, node, to) |
Dmitry Jemerov | d1177b3 | 2015-10-28 17:28:38 +0100 | [diff] [blame] | 345 | |
| 346 | val allExtensions = collectAllExtensions(node) |
| 347 | appendSection(location, "Extension Properties", allExtensions.filter { it.kind == DocumentationNode.Kind.Property }, node, to) |
| 348 | appendSection(location, "Extension Functions", allExtensions.filter { it.kind == DocumentationNode.Kind.Function }, node, to) |
| 349 | appendSection(location, "Companion Object Extension Properties", allExtensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectProperty }, node, to) |
| 350 | appendSection(location, "Companion Object Extension Functions", allExtensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectFunction }, node, to) |
Dmitry Jemerov | c4f40a0 | 2015-01-12 16:32:30 +0100 | [diff] [blame] | 351 | appendSection(location, "Inheritors", |
| 352 | node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to) |
Ilya Ryzhenkov | d6fd045 | 2014-10-03 20:20:02 +0400 | [diff] [blame] | 353 | appendSection(location, "Links", node.links, node, to) |
| 354 | |
| 355 | } |
| 356 | } |
Dmitry Jemerov | d1177b3 | 2015-10-28 17:28:38 +0100 | [diff] [blame] | 357 | |
| 358 | private fun collectAllExtensions(node: DocumentationNode): Collection<DocumentationNode> { |
| 359 | val result = LinkedHashSet<DocumentationNode>() |
| 360 | val visited = hashSetOf<DocumentationNode>() |
| 361 | |
| 362 | fun collect(node: DocumentationNode) { |
| 363 | if (!visited.add(node)) return |
| 364 | result.addAll(node.extensions) |
| 365 | node.references(DocumentationReference.Kind.Superclass).forEach { collect(it.to) } |
| 366 | } |
| 367 | |
| 368 | collect(node) |
| 369 | |
| 370 | return result |
| 371 | |
| 372 | } |
Ilya Ryzhenkov | 62cb509 | 2014-07-15 15:54:05 +0400 | [diff] [blame] | 373 | } |