blob: 32e22fd7bcccfd5b1324f9f8c165730548ef4e51 [file] [log] [blame]
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +04001package org.jetbrains.dokka
2
3import java.util.LinkedHashMap
Dmitry Jemerov0c584d02015-01-12 17:23:07 +01004import org.jetbrains.dokka.LanguageService.RenderMode
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
8public abstract class StructuredFormatService(val locationService: LocationService,
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +04009 val languageService: LanguageService) : FormatService {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040010
11 abstract public fun appendBlockCode(to: StringBuilder, line: String)
12 abstract public fun appendBlockCode(to: StringBuilder, lines: Iterable<String>)
13 abstract public fun appendHeader(to: StringBuilder, text: String, level: Int = 1)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010014 abstract public fun appendParagraph(to: StringBuilder, text: String)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040015 abstract public fun appendLine(to: StringBuilder, text: String)
16 public abstract fun appendLine(to: StringBuilder)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040017
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040018 public abstract fun appendTable(to: StringBuilder, body: () -> Unit)
19 public abstract fun appendTableHeader(to: StringBuilder, body: () -> Unit)
20 public abstract fun appendTableBody(to: StringBuilder, body: () -> Unit)
21 public abstract fun appendTableRow(to: StringBuilder, body: () -> Unit)
22 public abstract fun appendTableCell(to: StringBuilder, body: () -> Unit)
Ilya Ryzhenkov499d0822014-07-15 16:18:53 +040023
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040024 public abstract fun formatText(text: String): String
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +040025 public abstract fun formatSymbol(text: String): String
26 public abstract fun formatKeyword(text: String): String
27 public abstract fun formatIdentifier(text: String): String
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040028 public abstract fun formatLink(text: String, href: String): String
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +010029 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.href)
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040030 public abstract fun formatStrong(text: String): String
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010031 public abstract fun formatStrikethrough(text: String): String
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040032 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040033 public abstract fun formatCode(code: String): String
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020034 public abstract fun formatList(text: String): String
35 public abstract fun formatListItem(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040036 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
37
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040038 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
39 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040040 }
41
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040042 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030043 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040044 when (content) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010045 is ContentText -> append(formatText(content.text))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040046 is ContentSymbol -> append(formatSymbol(content.text))
47 is ContentKeyword -> append(formatKeyword(content.text))
48 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040049 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010050 is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children)))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040051 is ContentCode -> append(formatCode(formatText(location, content.children)))
52 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020053 is ContentList -> append(formatList(formatText(location, content.children)))
54 is ContentListItem -> append(formatListItem(formatText(location, content.children)))
55
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040056 is ContentNodeLink -> {
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +010057 val linkTo = location.relativePathTo(locationService.location(content.node), extension)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040058 val linkText = formatText(location, content.children)
59 append(formatLink(linkText, linkTo))
60 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040061 is ContentExternalLink -> {
62 val linkText = formatText(location, content.children)
63 append(formatLink(linkText, content.href))
64 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040065 is ContentParagraph -> {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010066 appendParagraph(this, formatText(location, content.children))
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040067 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040068 is ContentBlockCode -> {
69 appendBlockCode(this, formatText(location, content.children))
70 }
Dmitry Jemerov0d0fc1f2015-02-10 18:32:12 +010071 is ContentBlock -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030072 }
73 }.toString()
74 }
75
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040076 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
77
78 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +010079 return FormatLink(to.name, locationService.relativePathToLocation(from, to, extension))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040080 }
81
Dmitry Jemerove1a38842015-02-10 18:55:12 +010082 fun appendDocumentation(location: Location, to: StringBuilder, overloads: Iterable<DocumentationNode>) {
83 val breakdownBySummary = overloads.groupByTo(LinkedHashMap()) { node -> node.content }
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +010084
Dmitry Jemerove1a38842015-02-10 18:55:12 +010085 for ((summary, items) in breakdownBySummary) {
86 items.forEach {
Dmitry Jemerovebbf2652015-02-10 19:35:27 +010087 to.append(formatCode(formatText(location, languageService.render(it))))
88 it.appendSourceLink(to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +010089 it.appendOverrides(to)
90 it.appendDeprecation(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040091 }
Dmitry Jemerove1a38842015-02-10 18:55:12 +010092 // All items have exactly the same documentation, so we can use any item to render it
93 val item = items.first()
Dmitry Jemerovebbf2652015-02-10 19:35:27 +010094 to.append(formatText(location, item.content.summary))
Dmitry Jemerove1a38842015-02-10 18:55:12 +010095 appendDescription(location, to, item)
Dmitry Jemerovebbf2652015-02-10 19:35:27 +010096 appendLine(to)
97 appendLine(to)
Dmitry Jemerove1a38842015-02-10 18:55:12 +010098 }
99 }
100
101 fun appendDescription(location: Location, to: StringBuilder, node: DocumentationNode) {
102 if (node.content.description != ContentEmpty) {
103 appendHeader(to, "Description", 3)
104 appendLine(to, formatText(location, node.content.description))
105 appendLine(to)
106 }
107 node.content.getSectionsWithSubjects().forEach {
108 appendSectionWithSubject(it.getKey(), location, it.getValue(), to)
109 }
110
111 for (section in node.content.sections.filter { it.subjectName == null }) {
112 appendLine(to, formatStrong(formatText(section.tag)))
113 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400114 }
115 }
116
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100117 fun Content.getSectionsWithSubjects(): Map<String, List<ContentSection>> =
118 sections.filter { it.subjectName != null }.groupBy { it.tag }
119
120 fun appendSectionWithSubject(title: String, location: Location, subjectSections: List<ContentSection>, to: StringBuilder) {
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100121 appendHeader(to, title, 3)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100122 subjectSections.forEach {
123 val subjectName = it.subjectName
124 if (subjectName != null) {
125 to.append(formatCode(subjectName)).append(" - ")
126 to.append(formatText(location, it))
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100127 appendLine(to)
128 }
129 }
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100130 }
131
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100132 private fun DocumentationNode.appendOverrides(to: StringBuilder) {
133 overrides.forEach {
134 to.append("Overrides ")
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100135 val location = locationService.relativePathToLocation(this, it, extension)
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100136 appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location)))
137 }
138 }
139
140 private fun DocumentationNode.appendDeprecation(to: StringBuilder) {
141 if (deprecation != null) {
142 val deprecationParameter = deprecation!!.details(DocumentationNode.Kind.Parameter).firstOrNull()
143 val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull()
144 if (deprecationValue != null) {
145 to.append(formatStrong("Deprecated: "))
146 appendLine(to, formatText(deprecationValue.name.trim("\"")))
147 } else {
148 appendLine(to, formatStrong("Deprecated"))
149 }
150 }
151 }
152
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100153 private fun DocumentationNode.appendSourceLink(to: StringBuilder) {
154 val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull()
155 if (sourceUrl != null) {
Dmitry Jemerovebbf2652015-02-10 19:35:27 +0100156 to.append(" ")
157 appendLine(to, formatLink("(source)", sourceUrl.name))
158 } else {
159 appendLine(to)
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100160 }
161 }
162
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400163 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400164 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400165 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400166 appendHeader(to, formatText(name))
Dmitry Jemerove1a38842015-02-10 18:55:12 +0100167 appendDocumentation(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400168 }
169 }
170
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400171 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400172 if (nodes.any()) {
173 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400174
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400175 val children = nodes.sortBy { it.name }
176 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400177
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400178 appendTable(to) {
179 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400180 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400181 appendTableRow(to) {
182 appendTableCell(to) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100183 to.append(formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400184 }
185 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400186 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400187 for ((summary, items) in breakdownBySummary) {
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100188 val signatureTexts = items map { signature ->
Dmitry Jemerov0c584d02015-01-12 17:23:07 +0100189 val signature = languageService.render(signature, RenderMode.SUMMARY)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100190 val signatureAsCode = ContentCode()
191 signatureAsCode.append(signature)
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100192 formatText(location, signatureAsCode)
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400193 }
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100194 signatureTexts.subList(0, signatureTexts.size()-1).forEach {
195 appendLine(to, it)
196 }
197 to.append(signatureTexts.last())
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400198 if (!summary.isEmpty()) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100199 to.append(summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400200 }
201 }
202 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400203 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400204 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400205 }
206 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400207 }
208 }
209
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400210 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
211 val breakdownByLocation = nodes.groupBy { node ->
Dmitry Jemerovd9bfa022015-02-19 18:59:00 +0100212 formatBreadcrumbs(node.path.filterNot { it.name.isEmpty() }.map { link(node, it) })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400213 }
214
215 for ((breadcrumbs, items) in breakdownByLocation) {
216 appendLine(to, breadcrumbs)
217 appendLine(to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100218 appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400219 }
220
221 for (node in nodes) {
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100222 if (node.kind == DocumentationNode.Kind.ExternalClass) {
223 appendSection(location, "Extensions for ${node.name}", node.members, node, to)
224 continue
225 }
226
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400227 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
228 appendSection(location, "Types", node.members.filter {
229 it.kind in setOf(
230 DocumentationNode.Kind.Class,
231 DocumentationNode.Kind.Interface,
232 DocumentationNode.Kind.Enum,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100233 DocumentationNode.Kind.Object,
234 DocumentationNode.Kind.AnnotationClass)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400235 }, node, to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100236 appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400237 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
238 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
239 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerov2822a3e2015-02-17 12:32:17 +0100240 appendSection(location, "Default Object Properties", node.members(DocumentationNode.Kind.DefaultObjectProperty), node, to)
241 appendSection(location, "Default Object Functions", node.members(DocumentationNode.Kind.DefaultObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400242 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100243 appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400244 appendSection(location, "Other members", node.members.filter {
245 it.kind !in setOf(
246 DocumentationNode.Kind.Class,
247 DocumentationNode.Kind.Interface,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100248 DocumentationNode.Kind.Enum,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400249 DocumentationNode.Kind.Object,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100250 DocumentationNode.Kind.AnnotationClass,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400251 DocumentationNode.Kind.Constructor,
252 DocumentationNode.Kind.Property,
253 DocumentationNode.Kind.Package,
254 DocumentationNode.Kind.Function,
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100255 DocumentationNode.Kind.PropertyAccessor,
Dmitry Jemerov2822a3e2015-02-17 12:32:17 +0100256 DocumentationNode.Kind.DefaultObjectProperty,
257 DocumentationNode.Kind.DefaultObjectFunction,
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100258 DocumentationNode.Kind.ExternalClass,
259 DocumentationNode.Kind.EnumItem
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400260 )
261 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400262 appendSection(location, "Extensions", node.extensions, node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100263 appendSection(location, "Inheritors",
264 node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400265 appendSection(location, "Links", node.links, node, to)
266
267 }
268 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400269}