Skip to content

Commit

Permalink
Starts trying to add Dokka class signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrooks12 committed Oct 17, 2018
1 parent f1e6ece commit f166bfa
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 49 deletions.
18 changes: 18 additions & 0 deletions dokka-json-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,21 @@ test {
}
}
test.outputs.upToDateWhen {false}



configurations { dokka }
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
dokka 'org.jetbrains.dokka:dokka-fatjar:0.9.17'
}
task runDefaultDokka(type: JavaExec) {
main = "org.jetbrains.dokka.MainKt"
classpath = configurations.dokka
args = [
"-src", "${project(':dokka-json').projectDir}/src/test/kotlin",
"-output", "${project(':dokka-json').buildDir}/dokka/default"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ data class KotlinClassDoc(
override val qualifiedName: String,
override val comment: String,
override val summaryPos: Int,
val modifiers: List<String>,
val constructors: List<KotlinConstructor>,
val methods: List<KotlinMethod>,
val fields: List<KotlinField>,
val extensions: List<KotlinMethod>
val extensions: List<KotlinMethod>,
val signature: List<SignatureComponent>,
val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinClasslike {
companion object {
fun fromJson(json: String): KotlinClassDoc {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.copperleaf.dokka.json.generator.formatter

import com.copperleaf.dokka.json.models.KotlinClassDoc
import com.copperleaf.dokka.json.models.SignatureComponent
import org.jetbrains.dokka.DocumentationNode
import org.jetbrains.dokka.NodeKind
import org.jetbrains.dokka.path
Expand All @@ -10,6 +11,8 @@ val DocumentationNode.classLike: Boolean get() = NodeKind.classLike.contains(thi
fun DocumentationNode.toClassDoc(deep: Boolean = false): KotlinClassDoc {
assert(this.classLike) { "node must be a Class-like" }

val modifiers = this.modifiers

return KotlinClassDoc(
this,
this.path.map { it.name }.filterNot { it.isEmpty() }.first(),
Expand All @@ -18,9 +21,34 @@ fun DocumentationNode.toClassDoc(deep: Boolean = false): KotlinClassDoc {
this.qualifiedName,
this.contentText,
this.summary.textLength,
modifiers,
if (deep) this.members.filter { it.isConstructor }.map { it.toConstructor() } else emptyList(),
if (deep) this.members.filter { it.isMethod }.map { it.toMethod() } else emptyList(),
if (deep) this.members.filter { it.isField }.map { it.toField() } else emptyList(),
if (deep) this.extensions.filter { it.isMethod }.map { it.toMethod() } else emptyList()
if (deep) this.extensions.filter { it.isMethod }.map { it.toMethod() } else emptyList(),
this.classSignature(
modifiers
)
)
}

fun DocumentationNode.classSignature(
modifiers: List<String>
): List<SignatureComponent> {
val list = mutableListOf<SignatureComponent>()

list.addAll(modifiers.toModifierListSignature())
list.add(SignatureComponent("keyword", "class ", ""))
list.add(SignatureComponent("type", this.simpleName, this.qualifiedName))
// list.addAll(this.toTypeParameterDeclarationSignature())
// list.addAll(this.toSuperclassDeclarationSignature())

return list
}

fun DocumentationNode.toSuperclassDeclarationSignature(): List<SignatureComponent> {
return this.details(NodeKind.Supertype).toListSignature(
childMapper = { it.toTypeSignature() },
prefix = listOf(SignatureComponent("punctuation", ": ", ""))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ fun DocumentationNode.constructorSignature(
modifiers: List<String>,
parameters: List<KotlinParameter>
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()
val list = mutableListOf<SignatureComponent>()

signatureComponents.addAll(modifiers.toModifierListSignature())
signatureComponents.add(SignatureComponent("keyword", "constructor", ""))
signatureComponents.addAll(parameters.toParameterListSignature())
list.addAll(modifiers.toModifierListSignature())
list.add(SignatureComponent("keyword", "constructor", ""))
list.addAll(parameters.toParameterListSignature())

return signatureComponents
return list
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ fun DocumentationNode.fieldSignature(
modifiers: List<String>,
type: DocumentationNode
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()
val list = mutableListOf<SignatureComponent>()

signatureComponents.addAll(modifiers.toModifierListSignature())
signatureComponents.add(SignatureComponent("name", this.simpleName, ""))
signatureComponents.add(SignatureComponent("punctuation", ": ", ""))
signatureComponents.addAll(type.asType().toTypeSignature())
list.addAll(modifiers.toModifierListSignature())
list.add(SignatureComponent("name", this.simpleName, ""))
list.add(SignatureComponent("punctuation", ": ", ""))
list.addAll(type.asType().toTypeSignature())

return signatureComponents
return list
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import org.jetbrains.dokka.NodeKind
import org.jetbrains.dokka.path
import org.jetbrains.dokka.qualifiedNameFromType

val DocumentationNode.modifiers: List<String> get() = this.details(NodeKind.Modifier)
.map { it.name }
.filter { !arrayOf("public", "final").contains(it) }
val DocumentationNode.modifiers: List<String>
get() = this.details(NodeKind.Modifier)
.map { it.name }
.filter { !arrayOf("public", "final").contains(it) }

val DocumentationNode.simpleName: String
get() {
Expand All @@ -17,7 +18,7 @@ val DocumentationNode.simpleName: String

val DocumentationNode.qualifiedName: String
get() {
return if(kind == NodeKind.Type) {
return if (kind == NodeKind.Type) {
this.qualifiedNameFromType()
}
else {
Expand All @@ -26,8 +27,11 @@ val DocumentationNode.qualifiedName: String
}

fun DocumentationNode.asType(): DocumentationNode {
return if (kind == NodeKind.Type) this else this.detail(NodeKind.Type)
}
return if (kind == NodeKind.Type) this else this.detailOrNull(NodeKind.Type) ?: {
println("other node requesting type: ${this.kind} ${this.name}")
this
}()
}

val DocumentationNode.simpleType: String
get() {
Expand All @@ -47,3 +51,25 @@ val DocumentationNode.nullable: Boolean
fun List<String>.toModifierListSignature(): List<SignatureComponent> {
return this.map { SignatureComponent("modifier", "$it ", "") }
}

fun List<DocumentationNode>.toListSignature(
childMapper: (DocumentationNode) -> List<SignatureComponent>,
prefix: List<SignatureComponent> = emptyList(),
postfix: List<SignatureComponent> = emptyList(),
separator: List<SignatureComponent> = listOf(SignatureComponent("punctuation", ", ", ""))
): List<SignatureComponent> {
val list = mutableListOf<SignatureComponent>()

if (this.isNotEmpty()) {
list.addAll(prefix)
this.forEachIndexed { index, superclass ->
list.addAll(childMapper(superclass))
if (index < this.size - 1) {
list.addAll(separator)
}
}
list.addAll(postfix)
}

return list
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,27 @@ fun DocumentationNode.methodSignature(
receiverType: KotlinReceiverType?,
returnType: KotlinReturnType
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()
val list = mutableListOf<SignatureComponent>()

signatureComponents.addAll(modifiers.toModifierListSignature())
signatureComponents.add(SignatureComponent("keyword", "fun ", ""))
list.addAll(modifiers.toModifierListSignature())
list.add(SignatureComponent("keyword", "fun ", ""))

list.addAll(this.toTypeParameterDeclarationSignature())

if (receiverType != null) {
signatureComponents.addAll(receiverType.signature)
signatureComponents.add(SignatureComponent("punctuation", ".", ""))
list.addAll(receiverType.signature)
list.add(SignatureComponent("punctuation", ".", ""))
}

signatureComponents.add(SignatureComponent("name", this.simpleName, ""))
signatureComponents.addAll(parameters.toParameterListSignature())
list.add(SignatureComponent("name", this.simpleName, ""))
list.addAll(parameters.toParameterListSignature())

if (returnType.name != "Unit") {
signatureComponents.add(SignatureComponent("punctuation", ": ", ""))
signatureComponents.addAll(returnType.signature)
list.add(SignatureComponent("punctuation", ": ", ""))
list.addAll(returnType.signature)
}

return signatureComponents
return list
}

val DocumentationNode.returnType: KotlinReturnType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,11 @@ fun DocumentationNode.toNonFunctionalTypeSignature(): List<SignatureComponent> {

list.add(SignatureComponent("type", this.simpleType, this.qualifiedType))

val typeArguments = this.details(NodeKind.Type)
if (typeArguments.isNotEmpty()) {
list.add(SignatureComponent("punctuation", "<", ""))
typeArguments.forEachIndexed { index, parameter ->
list.addAll(parameter.toTypeSignature())
if (index < typeArguments.size - 1) {
list.add(SignatureComponent("punctuation", ", ", ""))
}
}
list.add(SignatureComponent("punctuation", ">", ""))
}
this.details(NodeKind.Type).toListSignature(
childMapper = { it.toTypeSignature() },
prefix = listOf(SignatureComponent("punctuation", "<", "")),
postfix = listOf(SignatureComponent("punctuation", ">", ""))
)

if (this.nullable) {
list.add(SignatureComponent("punctuation", "?", ""))
Expand Down Expand Up @@ -123,3 +117,32 @@ fun DocumentationNode.toFunctionalTypeSignature(): List<SignatureComponent> {

return list
}


// Type Params
//----------------------------------------------------------------------------------------------------------------------

fun DocumentationNode.toTypeParameterDeclarationSignature(): List<SignatureComponent> {
val list = mutableListOf<SignatureComponent>()

val typeArguments = this.details(NodeKind.TypeParameter)
if (typeArguments.isNotEmpty()) {
list.add(SignatureComponent("punctuation", "<", ""))
typeArguments.forEachIndexed { index, typeParameter ->
list.add(SignatureComponent("name", typeParameter.name, ""))

val upperBound = typeParameter.detailOrNull(NodeKind.UpperBound)
if (upperBound != null) {
list.add(SignatureComponent("punctuation", " : ", ""))
list.addAll(upperBound.asType().toTypeSignature())
}

if (index < typeArguments.size - 1) {
list.add(SignatureComponent("punctuation", ", ", ""))
}
}
list.add(SignatureComponent("punctuation", "> ", ""))
}

return list
}
Loading

0 comments on commit f166bfa

Please sign in to comment.