aboutsummaryrefslogtreecommitdiff
path: root/src/Languages/KotlinLanguageService.kt
diff options
context:
space:
mode:
authorIlya Ryzhenkov <orangy@jetbrains.com>2014-10-03 19:09:31 +0400
committerIlya Ryzhenkov <orangy@jetbrains.com>2014-10-03 19:09:31 +0400
commit7c6da4babd01da31c57c5c6c827eb2957c989b1c (patch)
tree1cc063c24326044b9ee968b2278e0864e8ee7051 /src/Languages/KotlinLanguageService.kt
parenta52e1d543d22fdacf87ec00988b753d2d1107c1d (diff)
downloaddokka-7c6da4babd01da31c57c5c6c827eb2957c989b1c.tar.gz
dokka-7c6da4babd01da31c57c5c6c827eb2957c989b1c.tar.bz2
dokka-7c6da4babd01da31c57c5c6c827eb2957c989b1c.zip
Language Service now formats to ContentNode instead of String.
Diffstat (limited to 'src/Languages/KotlinLanguageService.kt')
-rw-r--r--src/Languages/KotlinLanguageService.kt276
1 files changed, 159 insertions, 117 deletions
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