blob: f83f4aa40b2747985b1ca9b500603eb4fd243644 [file] [log] [blame]
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04001package org.jetbrains.dokka
2
Dmitry Jemerov0c584d02015-01-12 17:23:07 +01003import org.jetbrains.dokka.LanguageService.RenderMode
Dmitry Jemerov8827d302015-10-20 12:53:17 +02004import java.util.*
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04005
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +01006public data class FormatLink(val text: String, val href: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04007
Dmitry Jemerov66593372015-03-03 19:35:56 +01008enum class ListKind {
Dmitry Jemerov64414ce2015-05-29 13:52:43 +02009 Ordered,
Dmitry Jemerov66593372015-03-03 19:35:56 +010010 Unordered
11}
12
Dmitry Jemerovea1f4cc2015-02-19 19:51:01 +010013public abstract class StructuredFormatService(locationService: LocationService,
14 val languageService: LanguageService,
15 override val extension: String) : FormatService {
16 val locationService: LocationService = locationService.withExtension(extension)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040017
Dmitry Jemerov44cef5c2015-02-26 19:34:49 +010018 abstract public fun appendBlockCode(to: StringBuilder, line: String, language: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040019 abstract public fun appendHeader(to: StringBuilder, text: String, level: Int = 1)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010020 abstract public fun appendParagraph(to: StringBuilder, text: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040021 abstract public fun appendLine(to: StringBuilder, text: String)
22 public abstract fun appendLine(to: StringBuilder)
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +010023 public abstract fun appendAnchor(to: StringBuilder, anchor: String)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040024
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040025 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 Ryzhenkov499d0822014-07-15 16:18:53 +040030
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040031 public abstract fun formatText(text: String): String
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040032 public abstract fun formatSymbol(text: String): String
33 public abstract fun formatKeyword(text: String): String
Dmitry Jemerov4494fd02015-02-26 21:34:27 +010034 public abstract fun formatIdentifier(text: String, kind: IdentifierKind): String
Dmitry Jemerov2e6eda92015-03-10 17:00:14 +010035 public fun formatEntity(text: String): String = text
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040036 public abstract fun formatLink(text: String, href: String): String
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +010037 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href)
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040038 public abstract fun formatStrong(text: String): String
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010039 public abstract fun formatStrikethrough(text: String): String
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040040 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040041 public abstract fun formatCode(code: String): String
Dmitry Jemerov66593372015-03-03 19:35:56 +010042 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 Ryzhenkov62cb5092014-07-15 15:54:05 +040045 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
Dmitry Jemerov722c9af2015-02-26 16:28:05 +010046 public abstract fun formatNonBreakingSpace(): String
Dmitry Jemerov8291bee2015-10-27 18:37:35 +010047 public open fun formatSoftLineBreak(): String = ""
48 public open fun formatIndentedSoftLineBreak(): String = ""
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040049
Dmitry Jemerov66593372015-03-03 19:35:56 +010050 open fun formatText(location: Location, nodes: Iterable<ContentNode>, listKind: ListKind = ListKind.Unordered): String {
Dmitry Jemerovb8f24352015-10-21 18:26:10 +020051 return nodes.map { formatText(location, it, listKind) }.joinToString("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040052 }
53
Dmitry Jemerov66593372015-03-03 19:35:56 +010054 open fun formatText(location: Location, content: ContentNode, listKind: ListKind = ListKind.Unordered): String {
Dmitry Jemerovb8f24352015-10-21 18:26:10 +020055 return StringBuilder().apply {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040056 when (content) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010057 is ContentText -> append(formatText(content.text))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040058 is ContentSymbol -> append(formatSymbol(content.text))
59 is ContentKeyword -> append(formatKeyword(content.text))
Dmitry Jemerov4494fd02015-02-26 21:34:27 +010060 is ContentIdentifier -> append(formatIdentifier(content.text, content.kind))
Dmitry Jemerov722c9af2015-02-26 16:28:05 +010061 is ContentNonBreakingSpace -> append(formatNonBreakingSpace())
Dmitry Jemerov8291bee2015-10-27 18:37:35 +010062 is ContentSoftLineBreak -> append(formatSoftLineBreak())
63 is ContentIndentedSoftLineBreak -> append(formatIndentedSoftLineBreak())
Dmitry Jemerov2e6eda92015-03-10 17:00:14 +010064 is ContentEntity -> append(formatEntity(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040065 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010066 is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children)))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040067 is ContentCode -> append(formatCode(formatText(location, content.children)))
68 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Dmitry Jemerov66593372015-03-03 19:35:56 +010069 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 Ryzhenkov18399492014-12-22 09:50:17 +020072
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040073 is ContentNodeLink -> {
Dmitry Jemerov184a24c2015-02-25 19:03:51 +010074 val node = content.node
75 val linkTo = if (node != null) locationHref(location, node) else "#"
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040076 val linkText = formatText(location, content.children)
Dmitry Jemerov4b61be32015-02-26 21:17:13 +010077 if (linkTo == ".") {
78 append(linkText)
79 } else {
80 append(formatLink(linkText, linkTo))
81 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040082 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040083 is ContentExternalLink -> {
84 val linkText = formatText(location, content.children)
Dmitry Jemerov4b61be32015-02-26 21:17:13 +010085 if (content.href == ".") {
86 append(linkText)
87 } else {
88 append(formatLink(linkText, content.href))
89 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040090 }
Dmitry Jemerov76e4d3c2015-03-23 18:52:05 +010091 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 Jemerov0d0fc1f2015-02-10 18:32:12 +010094 is ContentBlock -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030095 }
96 }.toString()
97 }
98
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040099 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 Jemerovea1f4cc2015-02-19 19:51:01 +0100102 return FormatLink(to.name, locationService.relativePathToLocation(from, to))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400103 }
104
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100105 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 Jemerove1a38842015-02-10 18:55:12 +0100113 fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) {
114 val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content }
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100115
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100116 for ((summary, items) in breakdownBySummary) {
117 items.forEach {
Dmitry Jemerov8291bee2015-10-27 18:37:35 +0100118 val rendered = languageService.render(it)
119 appendAsSignature(to, rendered) {
120 to.append(formatCode(formatText(location, rendered)))
Dmitry Jemerovf90ee502015-02-26 13:55:37 +0100121 it.appendSourceLink(to)
122 }
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100123 it.appendOverrides(to)
Dmitry Jemerova60d8ba2015-02-24 16:24:00 +0100124 it.appendDeprecation(location, to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400125 }
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100126 // All items have exactly the same documentation, so we can use any item to render it
127 val item = items.first()
Dmitry Jemerovf4623172015-03-02 16:17:28 +0100128 item.details(DocumentationNode.Kind.OverloadGroupNote).forEach {
129 to.append(formatText(location, it.content))
130 }
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100131 to.append(formatText(location, item.content.summary))
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100132 appendDescription(location, to, item)
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100133 appendLine(to)
134 appendLine(to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100135 }
136 }
137
Dmitry Jemerov76e4d3c2015-03-23 18:52:05 +0100138 private fun DocumentationNode.isModuleOrPackage(): Boolean =
139 kind == DocumentationNode.Kind.Module || kind == DocumentationNode.Kind.Package
140
Dmitry Jemerov8291bee2015-10-27 18:37:35 +0100141 protected open fun appendAsSignature(to: StringBuilder, node: ContentNode, block: () -> Unit) {
Dmitry Jemerovf90ee502015-02-26 13:55:37 +0100142 block()
143 }
144
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100145 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 Jemerovb8f24352015-10-21 18:26:10 +0200152 appendSectionWithSubject(it.key, location, it.value, to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100153 }
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 Ryzhenkov62cb5092014-07-15 15:54:05 +0400158 }
159 }
160
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100161 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 Jemerovbfd9ffd2015-01-30 17:59:15 +0100165 appendHeader(to, title, 3)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100166 subjectSections.forEach {
167 val subjectName = it.subjectName
168 if (subjectName != null) {
Dmitry Jemerov85a3ae72015-02-20 14:08:30 +0100169 appendAnchor(to, subjectName)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100170 to.append(formatCode(subjectName)).append(" - ")
171 to.append(formatText(location, it))
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100172 appendLine(to)
173 }
174 }
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100175 }
176
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100177 private fun DocumentationNode.appendOverrides(to: StringBuilder) {
178 overrides.forEach {
179 to.append("Overrides ")
Dmitry Jemerovea1f4cc2015-02-19 19:51:01 +0100180 val location = locationService.relativePathToLocation(this, it)
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100181 appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location)))
182 }
183 }
184
Dmitry Jemerova60d8ba2015-02-24 16:24:00 +0100185 private fun DocumentationNode.appendDeprecation(location: Location, to: StringBuilder) {
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100186 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 Jemerov7003ad82015-02-26 13:20:41 +0100190 to.append(formatStrong("Deprecated:")).append(" ")
Dmitry Jemerov64414ce2015-05-29 13:52:43 +0200191 appendLine(to, formatText(deprecationValue.name.removeSurrounding("\"")))
Dmitry Jemerovf7c2f2a2015-02-26 19:45:36 +0100192 appendLine(to)
Dmitry Jemerova60d8ba2015-02-24 16:24:00 +0100193 } else if (deprecation?.content != Content.Empty) {
Dmitry Jemerov7003ad82015-02-26 13:20:41 +0100194 to.append(formatStrong("Deprecated:")).append(" ")
Dmitry Jemerova60d8ba2015-02-24 16:24:00 +0100195 to.append(formatText(location, deprecation!!.content))
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100196 } else {
197 appendLine(to, formatStrong("Deprecated"))
Dmitry Jemerovf7c2f2a2015-02-26 19:45:36 +0100198 appendLine(to)
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100199 }
200 }
201 }
202
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100203 private fun DocumentationNode.appendSourceLink(to: StringBuilder) {
204 val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull()
205 if (sourceUrl != null) {
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100206 to.append(" ")
207 appendLine(to, formatLink("(source)", sourceUrl.name))
208 } else {
209 appendLine(to)
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100210 }
211 }
212
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400213 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Dmitry Jemerov76e4d3c2015-03-23 18:52:05 +0100214 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 Ryzhenkov62cb5092014-07-15 15:54:05 +0400226 }
227 }
228
Dmitry Jemerov3faa3f22015-10-27 17:12:31 +0100229 private fun appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400230 if (nodes.any()) {
231 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400232
Dmitry Jemerov97257372015-09-07 20:57:17 +0200233 val children = nodes.sortedBy { it.name }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400234 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400235
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400236 appendTable(to) {
237 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400238 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400239 appendTableRow(to) {
240 appendTableCell(to) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100241 to.append(formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400242 }
243 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400244 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400245 for ((summary, items) in breakdownBySummary) {
Dmitry Jemerov3faa3f22015-10-27 17:12:31 +0100246 appendSummarySignatures(items, location, to)
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400247 if (!summary.isEmpty()) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100248 to.append(summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400249 }
250 }
251 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400252 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400253 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400254 }
255 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400256 }
257 }
258
Dmitry Jemerov3faa3f22015-10-27 17:12:31 +0100259 private fun appendSummarySignatures(items: List<DocumentationNode>, location: Location, to: StringBuilder) {
260 val summarySignature = languageService.summarizeSignatures(items)
261 if (summarySignature != null) {
Dmitry Jemerov0efda362015-10-28 11:38:40 +0100262 appendAsSignature(to, summarySignature) {
263 appendLine(to, summarySignature.signatureToText(location))
Dmitry Jemerov3faa3f22015-10-27 17:12:31 +0100264 }
265 return
266 }
Dmitry Jemerov8291bee2015-10-27 18:37:35 +0100267 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 Jemerov3faa3f22015-10-27 17:12:31 +0100271 }
272 }
Dmitry Jemerov8291bee2015-10-27 18:37:35 +0100273 appendAsSignature(to, renderedSignatures.last()) {
274 to.append(renderedSignatures.last().signatureToText(location))
Dmitry Jemerov3faa3f22015-10-27 17:12:31 +0100275 }
Dmitry Jemerov8291bee2015-10-27 18:37:35 +0100276 }
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 Jemerov3faa3f22015-10-27 17:12:31 +0100285 }
286 }
287
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400288 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
289 val breakdownByLocation = nodes.groupBy { node ->
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100290 formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400291 }
292
293 for ((breadcrumbs, items) in breakdownByLocation) {
294 appendLine(to, breadcrumbs)
295 appendLine(to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100296 appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400297 }
298
299 for (node in nodes) {
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100300 if (node.kind == DocumentationNode.Kind.ExternalClass) {
301 appendSection(location, "Extensions for ${node.name}", node.members, node, to)
302 continue
303 }
304
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400305 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 Jemerov716483c2014-12-30 17:41:14 +0100311 DocumentationNode.Kind.Object,
312 DocumentationNode.Kind.AnnotationClass)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400313 }, node, to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100314 appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to)
Dmitry Jemerovc81053d2015-10-28 17:34:44 +0100315 appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400316 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
317 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
Dmitry Jemerov36f4b912015-10-28 15:23:50 +0100318 appendSection(location, "Inherited Properties", node.inheritedMembers(DocumentationNode.Kind.Property), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400319 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerov36f4b912015-10-28 15:23:50 +0100320 appendSection(location, "Inherited Functions", node.inheritedMembers(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerovc7916f72015-03-17 19:54:11 +0100321 appendSection(location, "Companion Object Properties", node.members(DocumentationNode.Kind.CompanionObjectProperty), node, to)
322 appendSection(location, "Companion Object Functions", node.members(DocumentationNode.Kind.CompanionObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400323 appendSection(location, "Other members", node.members.filter {
324 it.kind !in setOf(
325 DocumentationNode.Kind.Class,
326 DocumentationNode.Kind.Interface,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100327 DocumentationNode.Kind.Enum,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400328 DocumentationNode.Kind.Object,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100329 DocumentationNode.Kind.AnnotationClass,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400330 DocumentationNode.Kind.Constructor,
331 DocumentationNode.Kind.Property,
332 DocumentationNode.Kind.Package,
333 DocumentationNode.Kind.Function,
Dmitry Jemerovc7916f72015-03-17 19:54:11 +0100334 DocumentationNode.Kind.CompanionObjectProperty,
335 DocumentationNode.Kind.CompanionObjectFunction,
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100336 DocumentationNode.Kind.ExternalClass,
337 DocumentationNode.Kind.EnumItem
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400338 )
339 }, node, to)
Dmitry Jemerovd1177b32015-10-28 17:28:38 +0100340
341 val allExtensions = collectAllExtensions(node)
342 appendSection(location, "Extension Properties", allExtensions.filter { it.kind == DocumentationNode.Kind.Property }, node, to)
343 appendSection(location, "Extension Functions", allExtensions.filter { it.kind == DocumentationNode.Kind.Function }, node, to)
344 appendSection(location, "Companion Object Extension Properties", allExtensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectProperty }, node, to)
345 appendSection(location, "Companion Object Extension Functions", allExtensions.filter { it.kind == DocumentationNode.Kind.CompanionObjectFunction }, node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100346 appendSection(location, "Inheritors",
347 node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400348 appendSection(location, "Links", node.links, node, to)
349
350 }
351 }
Dmitry Jemerovd1177b32015-10-28 17:28:38 +0100352
353 private fun collectAllExtensions(node: DocumentationNode): Collection<DocumentationNode> {
354 val result = LinkedHashSet<DocumentationNode>()
355 val visited = hashSetOf<DocumentationNode>()
356
357 fun collect(node: DocumentationNode) {
358 if (!visited.add(node)) return
359 result.addAll(node.extensions)
360 node.references(DocumentationReference.Kind.Superclass).forEach { collect(it.to) }
361 }
362
363 collect(node)
364
365 return result
366
367 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400368}