aboutsummaryrefslogtreecommitdiff
path: root/plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt
diff options
context:
space:
mode:
authorKamil Doległo <kamilok1965@interia.pl>2020-03-01 21:26:13 +0100
committerKamil Doległo <kamilok1965@users.noreply.github.com>2020-03-04 18:00:19 +0100
commite4044ec67ad90041b02bb84c4b966ffeac537617 (patch)
treea06e82ad218737771b7a6381537a98ba00c94eaf /plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt
parentcf0c5043887b1dd38808b0fc12bd8700c9f3b6ba (diff)
downloaddokka-e4044ec67ad90041b02bb84c4b966ffeac537617.tar.gz
dokka-e4044ec67ad90041b02bb84c4b966ffeac537617.tar.bz2
dokka-e4044ec67ad90041b02bb84c4b966ffeac537617.zip
Add initial version of Kotlin as Java plugin
Diffstat (limited to 'plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt')
-rw-r--r--plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt112
1 files changed, 112 insertions, 0 deletions
diff --git a/plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt b/plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt
new file mode 100644
index 00000000..526546f2
--- /dev/null
+++ b/plugins/kotlin-as-java/src/main/kotlin/JavaSignatureProvider.kt
@@ -0,0 +1,112 @@
+package org.jetbrains.dokka.kotlinAsJava
+
+import org.jetbrains.dokka.base.signatures.SignatureProvider
+import org.jetbrains.dokka.base.transformers.pages.comments.CommentsToContentConverter
+import org.jetbrains.dokka.base.translators.documentables.PageContentBuilder
+import org.jetbrains.dokka.links.DRI
+import org.jetbrains.dokka.links.sureClassNames
+import org.jetbrains.dokka.model.*
+import org.jetbrains.dokka.model.Annotation
+import org.jetbrains.dokka.model.Enum
+import org.jetbrains.dokka.model.Function
+import org.jetbrains.dokka.pages.ContentKind
+import org.jetbrains.dokka.pages.ContentNode
+import org.jetbrains.dokka.pages.PlatformData
+import org.jetbrains.dokka.utilities.DokkaLogger
+
+class JavaSignatureProvider(ctcc: CommentsToContentConverter, logger: DokkaLogger) : SignatureProvider {
+ private val contentBuilder = PageContentBuilder(ctcc, logger)
+
+ override fun signature(documentable: Documentable): List<ContentNode> = when (documentable) {
+ is Function -> signature(documentable)
+ is Classlike -> signature(documentable)
+ else -> throw NotImplementedError(
+ "Cannot generate signature for ${documentable::class.qualifiedName} ${documentable.name}"
+ )
+ }
+
+ private fun signature(f: Function) = f.platformData.map { signature(f, it) }.distinct()
+
+ private fun signature(c: Classlike) = c.platformData.map { signature(c, it) }.distinct()
+
+ private fun signature(c: Classlike, platform: PlatformData) = contentBuilder.contentFor(c, ContentKind.Symbol) {
+ text(c.visibility[platform]?.externalDisplayName ?: "")
+ if (c is Class) {
+ text(c.modifier.toString())
+ }
+ when (c) {
+ is Class -> text(" class ")
+ is Interface -> text(" interface ")
+ is Enum -> text(" enum ")
+ is Object -> text(" class ")
+ is Annotation -> text(" @interface ")
+ }
+ text(c.name!!)
+ if (c is WithSupertypes) {
+ list(c.supertypes.getValue(platform), prefix = " : ") {
+ link(it.sureClassNames, it)
+ }
+ }
+ }
+
+ private fun signature(f: Function, platform: PlatformData) = contentBuilder.contentFor(f, ContentKind.Symbol) {
+ text(f.visibility[platform]?.externalDisplayName ?: "")
+ text(f.modifier.toString())
+ val returnType = f.type
+ if (!f.isConstructor && returnType.constructorFqName != Unit::class.qualifiedName) {
+ text(": ")
+ type(returnType)
+ }
+ text(" ")
+ link(f.name, f.dri)
+ val generics = f.generics.filterOnPlatform(platform)
+ if (generics.isNotEmpty()) {
+ text("<")
+ generics.forEach {
+ signature(it)
+ }
+ text(">")
+ }
+ text("(")
+ list(listOfNotNull(f.receiver) + f.parameters.filterOnPlatform(platform)) {
+ type(it.type)
+ text(" ")
+ link(it.name!!, it.dri)
+ text(", ")
+ }
+ text(")")
+ }
+
+ private fun signature(t: TypeParameter) = contentBuilder.contentFor(t, ContentKind.Symbol) {
+ link(t.name, t.dri)
+ if (t.bounds.isNotEmpty()) {
+ text("<")
+ t.bounds.forEach {
+ signature(it, t.dri, t.platformData)
+ }
+ text(">")
+ }
+ }
+
+ private fun signature(p: Projection, dri: DRI, platforms: List<PlatformData>): List<ContentNode> = when (p) {
+ is OtherParameter -> contentBuilder.contentFor(dri, platforms.toSet()) { text(p.name) }.children
+
+ is TypeConstructor -> contentBuilder.contentFor(dri, platforms.toSet()) {
+ link(p.dri.classNames.orEmpty(), p.dri)
+ }.children + p.projections.flatMap { signature(it, dri, platforms) }
+
+ is Variance -> contentBuilder.contentFor(dri, platforms.toSet()) {
+ text(p.kind.toString() + " ")
+ }.children + signature(p.inner, dri, platforms)
+
+ is Star -> contentBuilder.contentFor(dri, platforms.toSet()) { text("*") }.children
+
+ is Nullable -> signature(p.inner, dri, platforms) + contentBuilder.contentFor(
+ dri,
+ platforms.toSet()
+ ) { text("?") }.children
+ }
+
+ private fun <T : Documentable> Collection<T>.filterOnPlatform(platformData: PlatformData) =
+ this.filter { it.platformData.contains(platformData) }
+} \ No newline at end of file