aboutsummaryrefslogtreecommitdiff
path: root/src/Languages
diff options
context:
space:
mode:
Diffstat (limited to 'src/Languages')
-rw-r--r--src/Languages/JavaLanguageService.kt26
-rw-r--r--src/Languages/KotlinLanguageService.kt276
-rw-r--r--src/Languages/LanguageService.kt14
3 files changed, 174 insertions, 142 deletions
diff --git a/src/Languages/JavaLanguageService.kt b/src/Languages/JavaLanguageService.kt
index f5976ea9..5d2b48f1 100644
--- a/src/Languages/JavaLanguageService.kt
+++ b/src/Languages/JavaLanguageService.kt
@@ -3,8 +3,8 @@ package org.jetbrains.dokka
import org.jetbrains.dokka.DocumentationNode.*
class JavaLanguageService : LanguageService {
- override fun render(node: DocumentationNode): String {
- return when (node.kind) {
+ override fun render(node: DocumentationNode): ContentNode {
+ return ContentText(when (node.kind) {
Kind.Package -> renderPackage(node)
Kind.Class,
Kind.Interface,
@@ -20,7 +20,7 @@ class JavaLanguageService : LanguageService {
Kind.Function -> renderFunction(node)
Kind.Property -> renderProperty(node)
else -> "${node.kind}: ${node.name}"
- }
+ })
}
override fun renderName(node: DocumentationNode): String {
@@ -30,11 +30,11 @@ class JavaLanguageService : LanguageService {
}
}
- override fun renderPackage(node: DocumentationNode): String {
+ fun renderPackage(node: DocumentationNode): String {
return "package ${node.name}"
}
- override fun renderModifier(node: DocumentationNode): String {
+ fun renderModifier(node: DocumentationNode): String {
return when (node.name) {
"open" -> ""
"internal" -> ""
@@ -42,7 +42,7 @@ class JavaLanguageService : LanguageService {
}
}
- override fun renderType(node: DocumentationNode): String {
+ fun renderType(node: DocumentationNode): String {
return when (node.name) {
"Unit" -> "void"
"Int" -> "int"
@@ -56,7 +56,7 @@ class JavaLanguageService : LanguageService {
}
}
- override fun renderTypeParameter(node: DocumentationNode): String {
+ fun renderTypeParameter(node: DocumentationNode): String {
val constraints = node.details(Kind.UpperBound)
return if (constraints.none())
node.name
@@ -65,11 +65,11 @@ class JavaLanguageService : LanguageService {
}
}
- override fun renderParameter(node: DocumentationNode): String {
+ fun renderParameter(node: DocumentationNode): String {
return "${renderType(node.detail(Kind.Type))} ${node.name}"
}
- override fun renderTypeParametersForNode(node: DocumentationNode): String {
+ fun renderTypeParametersForNode(node: DocumentationNode): String {
return StringBuilder {
val typeParameters = node.details(Kind.TypeParameter)
if (typeParameters.any()) {
@@ -80,14 +80,14 @@ class JavaLanguageService : LanguageService {
}.toString()
}
- override fun renderModifiersForNode(node: DocumentationNode): String {
+ fun renderModifiersForNode(node: DocumentationNode): String {
val modifiers = node.details(Kind.Modifier).map { renderModifier(it) }.filter { it != "" }
if (modifiers.none())
return ""
return modifiers.join(" ", postfix = " ")
}
- override fun renderClass(node: DocumentationNode): String {
+ fun renderClass(node: DocumentationNode): String {
return StringBuilder {
when (node.kind) {
Kind.Class -> append("class ")
@@ -103,7 +103,7 @@ class JavaLanguageService : LanguageService {
}.toString()
}
- override fun renderFunction(node: DocumentationNode): String {
+ fun renderFunction(node: DocumentationNode): String {
return StringBuilder {
when (node.kind) {
Kind.Constructor -> append(node.owner?.name)
@@ -127,7 +127,7 @@ class JavaLanguageService : LanguageService {
}.toString()
}
- override fun renderProperty(node: DocumentationNode): String {
+ fun renderProperty(node: DocumentationNode): String {
return StringBuilder {
when (node.kind) {
Kind.Property -> append("val ")
diff --git a/src/Languages/KotlinLanguageService.kt b/src/Languages/KotlinLanguageService.kt
index 7307cffb..851e7fe8 100644
--- a/src/Languages/KotlinLanguageService.kt
+++ b/src/Languages/KotlinLanguageService.kt
@@ -3,24 +3,26 @@ package org.jetbrains.dokka
import org.jetbrains.dokka.DocumentationNode.*
class KotlinLanguageService : LanguageService {
- override fun render(node: DocumentationNode): String {
- return when (node.kind) {
- Kind.Package -> renderPackage(node)
- Kind.Class,
- Kind.Interface,
- Kind.Enum,
- Kind.EnumItem,
- Kind.Object -> renderClass(node)
-
- Kind.TypeParameter -> renderTypeParameter(node)
- Kind.Type,
- Kind.UpperBound -> renderType(node)
-
- Kind.Modifier -> renderModifier(node)
- Kind.Constructor,
- Kind.Function -> renderFunction(node)
- Kind.Property -> renderProperty(node)
- else -> "${node.kind}: ${node.name}"
+ override fun render(node: DocumentationNode): ContentNode {
+ return content {
+ when (node.kind) {
+ Kind.Package -> renderPackage(node)
+ Kind.Class,
+ Kind.Interface,
+ Kind.Enum,
+ Kind.EnumItem,
+ Kind.Object -> renderClass(node)
+
+ Kind.TypeParameter -> renderTypeParameter(node)
+ Kind.Type,
+ Kind.UpperBound -> renderType(node)
+
+ Kind.Modifier -> renderModifier(node)
+ Kind.Constructor,
+ Kind.Function -> renderFunction(node)
+ Kind.Property -> renderProperty(node)
+ else -> ContentText("${node.kind}: ${node.name}")
+ }
}
}
@@ -31,137 +33,177 @@ class KotlinLanguageService : LanguageService {
}
}
- override fun renderPackage(node: DocumentationNode): String {
- return "package ${node.name}"
+ fun ContentNode.renderPackage(node: DocumentationNode) {
+ keyword("package")
+ text(" ")
+ identifier(node.name)
}
- override fun renderType(node: DocumentationNode): String {
- val typeArguments = node.details(Kind.Type)
- val renders = typeArguments.map { renderType(it) }
+ fun ContentNode.renderList(nodes: List<DocumentationNode>, separator: String = ", ", renderItem: (DocumentationNode) -> Unit) {
+ if (nodes.none())
+ return
+ renderItem(nodes.first())
+ nodes.drop(1).forEach {
+ symbol(separator)
+ renderItem(it)
+ }
+ }
+ fun ContentNode.renderType(node: DocumentationNode) {
+ val typeArguments = node.details(Kind.Type)
if (node.name == "Function${typeArguments.count() - 1}") {
// lambda
- return "(${renders.take(renders.size - 1).join()}) -> ${renders.last()}"
+ symbol("(")
+ renderList(typeArguments.take(typeArguments.size - 1)) {
+ renderType(it)
+ }
+ symbol(")")
+ text(" ")
+ symbol("->")
+ text(" ")
+ renderType(typeArguments.last())
+ return
}
if (node.name == "ExtensionFunction${typeArguments.count() - 2}") {
// extension lambda
- return "${renders.first()}.(${renders.drop(1).take(renders.size - 2).join()}) -> ${renders.last()}"
+ renderType(typeArguments.first())
+ symbol(".")
+ symbol("(")
+ renderList(typeArguments.drop(1).take(typeArguments.size - 2)) {
+ renderType(it)
+ }
+ symbol(")")
+ text(" ")
+ symbol("->")
+ text(" ")
+ renderType(typeArguments.last())
+ return
+ }
+ identifier(node.name)
+ if (typeArguments.any()) {
+ symbol("<")
+ renderList(typeArguments) {
+ renderType(it)
+ }
+ symbol(">")
}
- if (typeArguments.none())
- return node.name
- return "${node.name}<${renders.join()}>"
}
- override fun renderModifier(node: DocumentationNode): String {
- return when (node.name) {
- "final" -> ""
- "internal" -> ""
- else -> node.name
+ fun ContentNode.renderModifier(node: DocumentationNode) {
+ when (node.name) {
+ "final", "internal" -> {
+ }
+ else -> keyword(node.name)
}
}
- override fun renderTypeParameter(node: DocumentationNode): String {
+ fun ContentNode.renderTypeParameter(node: DocumentationNode) {
val constraints = node.details(Kind.UpperBound)
- return if (constraints.none())
- node.name
- else {
- node.name + " : " + constraints.map { renderType(node) }.join()
+ identifier(node.name)
+ if (constraints.any()) {
+ symbol(" : ")
+ renderList(constraints) {
+ renderType(it)
+ }
}
}
- override fun renderParameter(node: DocumentationNode): String {
- return node.name + ": " + renderType(node.detail(Kind.Type))
+ fun ContentNode.renderParameter(node: DocumentationNode) {
+ identifier(node.name)
+ symbol(": ")
+ renderType(node.detail(Kind.Type))
}
- override fun renderTypeParametersForNode(node: DocumentationNode): String {
- return StringBuilder {
- val typeParameters = node.details(Kind.TypeParameter)
- if (typeParameters.any()) {
- append("<")
- append(typeParameters.map { renderTypeParameter(it) }.join())
- append("> ")
+ fun ContentNode.renderTypeParametersForNode(node: DocumentationNode) {
+ val typeParameters = node.details(Kind.TypeParameter)
+ if (typeParameters.any()) {
+ symbol("<")
+ renderList(typeParameters) {
+ renderTypeParameter(it)
}
- }.toString()
+ symbol("> ")
+ }
}
- override fun renderModifiersForNode(node: DocumentationNode): String {
- val modifiers = node.details(Kind.Modifier).map { renderModifier(it) }.filter { it != "" }
- if (modifiers.none())
- return ""
- return modifiers.join(" ", postfix = " ")
+ fun ContentNode.renderSupertypesForNode(node: DocumentationNode) {
+ val supertypes = node.details(Kind.Supertype)
+ if (supertypes.any()) {
+ symbol(" : ")
+ renderList(supertypes) {
+ renderTypeParameter(it)
+ }
+ }
}
- override fun renderClass(node: DocumentationNode): String {
- return StringBuilder {
- append(renderModifiersForNode(node))
- when (node.kind) {
- Kind.Class -> append("class ")
- Kind.Interface -> append("trait ")
- Kind.Enum -> append("enum class ")
- Kind.EnumItem -> append("enum val ")
- Kind.Object -> append("object ")
- else -> throw IllegalArgumentException("Node $node is not a class-like object")
- }
+ fun ContentNode.renderModifiersForNode(node: DocumentationNode) {
+ val modifiers = node.details(Kind.Modifier)
+ for (it in modifiers) {
+ if (node.kind == Kind.Interface && it.name == "abstract")
+ continue
+ renderModifier(it)
+ text(" ")
+ }
+ }
+
+ fun ContentNode.renderClass(node: DocumentationNode) {
+ renderModifiersForNode(node)
+ when (node.kind) {
+ Kind.Class -> keyword("class ")
+ Kind.Interface -> keyword("trait ")
+ Kind.Enum -> keyword("enum class ")
+ Kind.EnumItem -> keyword("enum val ")
+ Kind.Object -> keyword("object ")
+ else -> throw IllegalArgumentException("Node $node is not a class-like object")
+ }
- append(node.name)
- append(renderTypeParametersForNode(node))
-
- /*
- val constructors = node.members(Kind.Constructor)
- if (constructors.count() == 1) {
- append("(")
- append(constructors[0].details(Kind.Parameter).map { renderParameter(it) }.join())
- append(")")
- }
- */
- }.toString()
+ identifier(node.name)
+ renderTypeParametersForNode(node)
+ renderSupertypesForNode(node)
}
- override fun renderFunction(node: DocumentationNode): String {
- return StringBuilder {
- append(renderModifiersForNode(node))
- when (node.kind) {
- Kind.Constructor -> append(node.owner!!.name)
- Kind.Function -> append("fun ")
- else -> throw IllegalArgumentException("Node $node is not a function-like object")
- }
- append(renderTypeParametersForNode(node))
- val receiver = node.details(Kind.Receiver).singleOrNull()
- if (receiver != null) {
- append(renderType(receiver.detail(Kind.Type)))
- append(".")
- }
+ fun ContentNode.renderFunction(node: DocumentationNode) {
+ renderModifiersForNode(node)
+ when (node.kind) {
+ Kind.Constructor -> identifier(node.owner!!.name)
+ Kind.Function -> keyword("fun ")
+ else -> throw IllegalArgumentException("Node $node is not a function-like object")
+ }
+ renderTypeParametersForNode(node)
+ val receiver = node.details(Kind.Receiver).singleOrNull()
+ if (receiver != null) {
+ renderType(receiver.detail(Kind.Type))
+ symbol(".")
+ }
- if (node.kind != Kind.Constructor)
- append(node.name)
+ if (node.kind != Kind.Constructor)
+ identifier(node.name)
- append("(")
- append(node.details(Kind.Parameter).map { renderParameter(it) }.join())
- append(")")
- if (node.kind != Kind.Constructor) {
- append(": ")
- append(renderType(node.detail(Kind.Type)))
- }
- }.toString()
+ symbol("(")
+ renderList(node.details(Kind.Parameter)) {
+ renderParameter(it)
+ }
+ symbol(")")
+ if (node.kind != Kind.Constructor) {
+ symbol(": ")
+ renderType(node.detail(Kind.Type))
+ }
}
- override fun renderProperty(node: DocumentationNode): String {
- return StringBuilder {
- append(renderModifiersForNode(node))
- when (node.kind) {
- Kind.Property -> append("val ")
- else -> throw IllegalArgumentException("Node $node is not a property")
- }
- append(renderTypeParametersForNode(node))
- val receiver = node.details(Kind.Receiver).singleOrNull()
- if (receiver != null) {
- append(renderType(receiver.detail(Kind.Type)))
- append(".")
- }
+ fun ContentNode.renderProperty(node: DocumentationNode) {
+ renderModifiersForNode(node)
+ when (node.kind) {
+ Kind.Property -> keyword("val ")
+ else -> throw IllegalArgumentException("Node $node is not a property")
+ }
+ renderTypeParametersForNode(node)
+ val receiver = node.details(Kind.Receiver).singleOrNull()
+ if (receiver != null) {
+ renderType(receiver.detail(Kind.Type))
+ symbol(".")
+ }
- append(node.name)
- append(": ")
- append(renderType(node.detail(Kind.Type)))
- }.toString()
+ identifier(node.name)
+ symbol(": ")
+ renderType(node.detail(Kind.Type))
}
} \ No newline at end of file
diff --git a/src/Languages/LanguageService.kt b/src/Languages/LanguageService.kt
index 728503b0..26217d41 100644
--- a/src/Languages/LanguageService.kt
+++ b/src/Languages/LanguageService.kt
@@ -1,17 +1,7 @@
package org.jetbrains.dokka
trait LanguageService {
- fun render(node: DocumentationNode): String
- fun renderName(node: DocumentationNode): String
- fun renderFunction(node: DocumentationNode): String
- fun renderClass(node: DocumentationNode): String
- fun renderTypeParametersForNode(node: DocumentationNode): String
- fun renderTypeParameter(node: DocumentationNode): String
- fun renderParameter(node: DocumentationNode): String
- fun renderType(node: DocumentationNode): String
- fun renderPackage(node: DocumentationNode): String
- fun renderProperty(node: DocumentationNode): String
- fun renderModifier(node: DocumentationNode): String
- fun renderModifiersForNode(node: DocumentationNode): String
+ fun render(node: DocumentationNode): ContentNode
+ fun renderName(node: DocumentationNode) : String
}