blob: 6ec75379b94a3bc751d36607a64ca332a2f5b6f9 [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
6public data class FormatLink(val text: String, val location: Location)
7
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 Ryzhenkovaa59acb2014-07-15 20:05:55 +040028 public abstract fun formatLink(text: String, location: Location): String
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040029 public abstract fun formatLink(text: String, href: String): String
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +040030 public open fun formatLink(link: FormatLink): String = formatLink(formatText(link.text), link.location)
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040031 public abstract fun formatStrong(text: String): String
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010032 public abstract fun formatStrikethrough(text: String): String
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040033 public abstract fun formatEmphasis(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040034 public abstract fun formatCode(code: String): String
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020035 public abstract fun formatList(text: String): String
36 public abstract fun formatListItem(text: String): String
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040037 public abstract fun formatBreadcrumbs(items: Iterable<FormatLink>): String
38
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040039 open fun formatText(location: Location, nodes: Iterable<ContentNode>): String {
40 return nodes.map { formatText(location, it) }.join("")
Ilya Ryzhenkov778e2b32014-09-29 20:54:59 +040041 }
42
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040043 open fun formatText(location: Location, content: ContentNode): String {
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030044 return StringBuilder {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040045 when (content) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010046 is ContentText -> append(formatText(content.text))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040047 is ContentSymbol -> append(formatSymbol(content.text))
48 is ContentKeyword -> append(formatKeyword(content.text))
49 is ContentIdentifier -> append(formatIdentifier(content.text))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040050 is ContentStrong -> append(formatStrong(formatText(location, content.children)))
Dmitry Jemerove17eaa52015-01-09 20:59:58 +010051 is ContentStrikethrough -> append(formatStrikethrough(formatText(location, content.children)))
Ilya Ryzhenkov9f0ff552014-10-13 13:38:40 +040052 is ContentCode -> append(formatCode(formatText(location, content.children)))
53 is ContentEmphasis -> append(formatEmphasis(formatText(location, content.children)))
Ilya Ryzhenkov18399492014-12-22 09:50:17 +020054 is ContentList -> append(formatList(formatText(location, content.children)))
55 is ContentListItem -> append(formatListItem(formatText(location, content.children)))
56
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040057 is ContentNodeLink -> {
58 val linkTo = locationService.relativeLocation(location, content.node, extension)
59 val linkText = formatText(location, content.children)
60 append(formatLink(linkText, linkTo))
61 }
Ilya Ryzhenkov71cd87e2014-10-03 22:51:44 +040062 is ContentExternalLink -> {
63 val linkText = formatText(location, content.children)
64 append(formatLink(linkText, content.href))
65 }
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040066 is ContentParagraph -> {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +010067 appendParagraph(this, formatText(location, content.children))
Ilya Ryzhenkovad14ea92014-10-13 18:22:02 +040068 }
Ilya Ryzhenkov1cb3af92014-10-13 20:14:45 +040069 is ContentBlockCode -> {
70 appendBlockCode(this, formatText(location, content.children))
71 }
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040072 else -> append(formatText(location, content.children))
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +030073 }
74 }.toString()
75 }
76
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040077 open public fun link(from: DocumentationNode, to: DocumentationNode): FormatLink = link(from, to, extension)
78
79 open public fun link(from: DocumentationNode, to: DocumentationNode, extension: String): FormatLink {
80 return FormatLink(to.name, locationService.relativeLocation(from, to, extension))
81 }
82
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040083 fun appendDescription(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +010084 val described = nodes.filter { it.hasDescriptionOrTags() }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040085 if (described.any()) {
Dmitry Jemerovc43a4372014-12-29 20:22:43 +010086 val single = described.size() == 1
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +010087 if (described.any { it.content.description != ContentEmpty }) {
88 appendHeader(to, "Description", 3)
89 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040090 for (node in described) {
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040091 if (!single) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +040092 appendBlockCode(to, formatText(location, languageService.render(node)))
Ilya Ryzhenkovfb41c692014-07-15 18:23:15 +040093 }
Ilya Ryzhenkov49077362014-10-14 19:53:13 +040094 appendLine(to, formatText(location, node.content.description))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +040095 appendLine(to)
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +010096
97 node.content.getSectionsWithSubjects().forEach {
98 appendSectionWithSubject(it.getKey(), location, it.getValue(), to)
99 }
100
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100101 for (section in node.content.sections.filter { it.subjectName == null }) {
102 appendLine(to, formatStrong(formatText(section.tag)))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400103 appendLine(to, formatText(location, section))
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400104 }
105 }
106 }
107 }
108
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100109 fun Content.getSectionsWithSubjects(): Map<String, List<ContentSection>> =
110 sections.filter { it.subjectName != null }.groupBy { it.tag }
111
112 fun appendSectionWithSubject(title: String, location: Location, subjectSections: List<ContentSection>, to: StringBuilder) {
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100113 appendHeader(to, title, 3)
Dmitry Jemerov0fac1d92015-01-30 19:01:40 +0100114 subjectSections.forEach {
115 val subjectName = it.subjectName
116 if (subjectName != null) {
117 to.append(formatCode(subjectName)).append(" - ")
118 to.append(formatText(location, it))
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100119 appendLine(to)
120 }
121 }
Dmitry Jemerovc43a4372014-12-29 20:22:43 +0100122 }
123
Dmitry Jemerovbfd9ffd2015-01-30 17:59:15 +0100124 private fun DocumentationNode.hasDescriptionOrTags() =
125 content.description != ContentEmpty || !content.sections.isEmpty()
126
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400127 fun appendSummary(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400128 val breakdownBySummary = nodes.groupByTo(LinkedHashMap()) { node ->
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400129 formatText(location, node.summary)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400130 }
131
132 for ((summary, items) in breakdownBySummary) {
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400133 items.forEach {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400134 appendBlockCode(to, formatText(location, languageService.render(it)))
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100135 it.appendOverrides(to)
136 it.appendDeprecation(to)
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100137 it.appendSourceLink(to)
Ilya Ryzhenkov7c6da4b2014-10-03 19:09:31 +0400138 }
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400139 appendLine(to, summary)
Ilya Ryzhenkov455d74a2014-09-19 22:25:27 +0300140 appendLine(to)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400141 }
142 }
143
Dmitry Jemerov0dd5ea32015-01-14 13:30:43 +0100144 private fun DocumentationNode.appendOverrides(to: StringBuilder) {
145 overrides.forEach {
146 to.append("Overrides ")
147 val location = locationService.relativeLocation(this, it, extension)
148 appendLine(to, formatLink(FormatLink(it.owner!!.name + "." + it.name, location)))
149 }
150 }
151
152 private fun DocumentationNode.appendDeprecation(to: StringBuilder) {
153 if (deprecation != null) {
154 val deprecationParameter = deprecation!!.details(DocumentationNode.Kind.Parameter).firstOrNull()
155 val deprecationValue = deprecationParameter?.details(DocumentationNode.Kind.Value)?.firstOrNull()
156 if (deprecationValue != null) {
157 to.append(formatStrong("Deprecated: "))
158 appendLine(to, formatText(deprecationValue.name.trim("\"")))
159 } else {
160 appendLine(to, formatStrong("Deprecated"))
161 }
162 }
163 }
164
Dmitry Jemerov6146fa82015-01-14 18:46:36 +0100165 private fun DocumentationNode.appendSourceLink(to: StringBuilder) {
166 val sourceUrl = details(DocumentationNode.Kind.SourceUrl).firstOrNull()
167 if (sourceUrl != null) {
168 appendLine(to, formatLink("Source", sourceUrl.name))
169 }
170 }
171
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400172 fun appendLocation(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
Ilya Ryzhenkovbd494a82014-08-21 19:53:36 +0400173 val breakdownByName = nodes.groupBy { node -> node.name }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400174 for ((name, items) in breakdownByName) {
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400175 appendHeader(to, formatText(name))
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400176 appendSummary(location, to, items)
177 appendDescription(location, to, items)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400178 }
179 }
180
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400181 private fun StructuredFormatService.appendSection(location: Location, caption: String, nodes: List<DocumentationNode>, node: DocumentationNode, to: StringBuilder) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400182 if (nodes.any()) {
183 appendHeader(to, caption, 3)
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400184
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400185 val children = nodes.sortBy { it.name }
186 val membersMap = children.groupBy { link(node, it) }
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400187
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400188 appendTable(to) {
189 appendTableBody(to) {
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400190 for ((memberLocation, members) in membersMap) {
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400191 appendTableRow(to) {
192 appendTableCell(to) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100193 to.append(formatLink(memberLocation))
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400194 }
195 appendTableCell(to) {
Ilya Ryzhenkov280dc292014-10-14 16:08:10 +0400196 val breakdownBySummary = members.groupBy { formatText(location, it.summary) }
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400197 for ((summary, items) in breakdownBySummary) {
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100198 val signatureTexts = items map { signature ->
Dmitry Jemerov0c584d02015-01-12 17:23:07 +0100199 val signature = languageService.render(signature, RenderMode.SUMMARY)
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100200 val signatureAsCode = ContentCode()
201 signatureAsCode.append(signature)
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100202 formatText(location, signatureAsCode)
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400203 }
Dmitry Jemerov3fc3e332014-12-30 15:35:00 +0100204 signatureTexts.subList(0, signatureTexts.size()-1).forEach {
205 appendLine(to, it)
206 }
207 to.append(signatureTexts.last())
Ilya Ryzhenkova52e1d52014-10-03 15:57:16 +0400208 if (!summary.isEmpty()) {
Dmitry Jemerov8ef68182014-12-30 12:36:14 +0100209 to.append(summary)
Ilya Ryzhenkovaa59acb2014-07-15 20:05:55 +0400210 }
211 }
212 }
Ilya Ryzhenkove8447fd2014-07-15 16:37:50 +0400213 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400214 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400215 }
216 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400217 }
218 }
219
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400220 override fun appendNodes(location: Location, to: StringBuilder, nodes: Iterable<DocumentationNode>) {
221 val breakdownByLocation = nodes.groupBy { node ->
222 formatBreadcrumbs(node.path.map { link(node, it) })
223 }
224
225 for ((breadcrumbs, items) in breakdownByLocation) {
226 appendLine(to, breadcrumbs)
227 appendLine(to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100228 appendLocation(location, to, items.filter { it.kind != DocumentationNode.Kind.ExternalClass })
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400229 }
230
231 for (node in nodes) {
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100232 if (node.kind == DocumentationNode.Kind.ExternalClass) {
233 appendSection(location, "Extensions for ${node.name}", node.members, node, to)
234 continue
235 }
236
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400237 appendSection(location, "Packages", node.members(DocumentationNode.Kind.Package), node, to)
238 appendSection(location, "Types", node.members.filter {
239 it.kind in setOf(
240 DocumentationNode.Kind.Class,
241 DocumentationNode.Kind.Interface,
242 DocumentationNode.Kind.Enum,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100243 DocumentationNode.Kind.Object,
244 DocumentationNode.Kind.AnnotationClass)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400245 }, node, to)
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100246 appendSection(location, "Extensions for External Classes", node.members(DocumentationNode.Kind.ExternalClass), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400247 appendSection(location, "Constructors", node.members(DocumentationNode.Kind.Constructor), node, to)
248 appendSection(location, "Properties", node.members(DocumentationNode.Kind.Property), node, to)
249 appendSection(location, "Functions", node.members(DocumentationNode.Kind.Function), node, to)
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100250 appendSection(location, "Class Object Properties", node.members(DocumentationNode.Kind.ClassObjectProperty), node, to)
251 appendSection(location, "Class Object Functions", node.members(DocumentationNode.Kind.ClassObjectFunction), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400252 appendSection(location, "Accessors", node.members(DocumentationNode.Kind.PropertyAccessor), node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100253 appendSection(location, "Enum Values", node.members(DocumentationNode.Kind.EnumItem), node, to)
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400254 appendSection(location, "Other members", node.members.filter {
255 it.kind !in setOf(
256 DocumentationNode.Kind.Class,
257 DocumentationNode.Kind.Interface,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100258 DocumentationNode.Kind.Enum,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400259 DocumentationNode.Kind.Object,
Dmitry Jemerov716483c2014-12-30 17:41:14 +0100260 DocumentationNode.Kind.AnnotationClass,
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400261 DocumentationNode.Kind.Constructor,
262 DocumentationNode.Kind.Property,
263 DocumentationNode.Kind.Package,
264 DocumentationNode.Kind.Function,
Dmitry Jemerovcedaeb42014-12-29 20:50:26 +0100265 DocumentationNode.Kind.PropertyAccessor,
266 DocumentationNode.Kind.ClassObjectProperty,
Dmitry Jemerov4b0dcee2015-01-09 19:48:44 +0100267 DocumentationNode.Kind.ClassObjectFunction,
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100268 DocumentationNode.Kind.ExternalClass,
269 DocumentationNode.Kind.EnumItem
Ilya Ryzhenkov49077362014-10-14 19:53:13 +0400270 )
271 }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400272 appendSection(location, "Extensions", node.extensions, node, to)
Dmitry Jemerovc4f40a02015-01-12 16:32:30 +0100273 appendSection(location, "Inheritors",
274 node.inheritors.filter { it.kind != DocumentationNode.Kind.EnumItem }, node, to)
Ilya Ryzhenkovd6fd0452014-10-03 20:20:02 +0400275 appendSection(location, "Links", node.links, node, to)
276
277 }
278 }
Ilya Ryzhenkov62cb5092014-07-15 15:54:05 +0400279}