Skip to content

Commit

Permalink
Adds Dokka receiver docs, and richer type signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
cjbrooks12 committed Oct 11, 2018
1 parent ae841fa commit 0dbc632
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ interface KotlinClasslike : KotlinDocElement

interface KotlinMemberlike : KotlinDocElement {
val modifiers: List<String>
}

interface KotlinType : KotlinDocElement {
val type: String
val qualifiedType: String
val signature: List<SignatureComponent>
val simpleSignature: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ data class KotlinMethod(
override val comment: String,
override val summaryPos: Int,
override val modifiers: List<String>,

val parameters: List<KotlinParameter>,
val returnValue: KotlinReturnValue,
val receiver: KotlinReceiverType? = null,
val returnValue: KotlinReturnType,
val signature: List<SignatureComponent>,
val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinMemberlike {
Expand All @@ -126,12 +128,12 @@ data class KotlinField(
override val comment: String,
override val summaryPos: Int,
override val modifiers: List<String>,
val type: String,
val qualifiedType: String,
val nullable: Boolean,
val signature: List<SignatureComponent>,
val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinMemberlike {

override val type: String,
override val qualifiedType: String,
override val signature: List<SignatureComponent>,
override val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinMemberlike, KotlinType {
override val kind = "Field"
}

Expand All @@ -147,35 +149,62 @@ data class KotlinParameter(
override val qualifiedName: String,
override val comment: String,
override val summaryPos: Int,
val type: String,
val qualifiedType: String,
val nullable: Boolean,
val defaultValue: String?
) : KotlinDocElement {

override val type: String,
override val qualifiedType: String,
val defaultValue: String?,
override val signature: List<SignatureComponent>,
override val simpleSignature: String = signature.map { it.name }.joinToString("")

) : KotlinDocElement, KotlinType {
override val kind = "Parameter"
}

/**
* The docs for a parameter of a constructor or method
* The docs for a method return type
*/
@Serializable
data class KotlinReturnValue(
data class KotlinReturnType(
@Transient
val node: Any? = null,

override val name: String,
override val qualifiedName: String,
override val comment: String,
override val summaryPos: Int,
val type: String,
val nullable: Boolean
) : KotlinDocElement {
override val kind = "ReturnValue"

override val type: String,
override val qualifiedType: String,
override val signature: List<SignatureComponent>,
override val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinDocElement, KotlinType {
override val kind = "ReturnType"
}

/**
* The docs for a method receiver type
*/
@Serializable
data class KotlinReceiverType(
@Transient
val node: Any? = null,

override val name: String,
override val qualifiedName: String,
override val comment: String,
override val summaryPos: Int,

override val type: String,
override val qualifiedType: String,
override val signature: List<SignatureComponent>,
override val simpleSignature: String = signature.map { it.name }.joinToString("")
) : KotlinDocElement, KotlinType {
override val kind = "ReceiverType"
}

/**
* A component to the rich signature. The complete signature can be created by joining all components together,
* optionally generating
* optionally generating links for individual components of the signature.
*/
@Serializable
data class SignatureComponent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fun DocumentationNode.constructorSignature(
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()

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

return signatureComponents
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ val DocumentationNode.isField: Boolean get() = this.kind == NodeKind.Field || th
fun DocumentationNode.toField(): KotlinField {
assert(this.isField) { "node must be a Field or Property" }
val modifiers = this.modifiers
val nullable = this.nullable
return KotlinField(
this,
this.simpleName,
Expand All @@ -20,7 +19,6 @@ fun DocumentationNode.toField(): KotlinField {
modifiers,
this.simpleType,
this.qualifiedType,
nullable,
this.fieldSignature(
modifiers,
this.asType()
Expand All @@ -34,10 +32,10 @@ fun DocumentationNode.fieldSignature(
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()

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

return signatureComponents
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ val DocumentationNode.nullable: Boolean
return asType().details(NodeKind.NullabilityModifier).singleOrNull() != null
}

fun MutableList<SignatureComponent>.appendModifierList(modifiers: List<String>) {
for (modifier in modifiers) {
add(SignatureComponent("modifier", "$modifier ", ""))
}
fun List<String>.toModifierListSignature(): List<SignatureComponent> {
return this.map { SignatureComponent("modifier", "$it ", "") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.copperleaf.dokka.json.generator.formatter

import com.copperleaf.dokka.json.models.KotlinMethod
import com.copperleaf.dokka.json.models.KotlinParameter
import com.copperleaf.dokka.json.models.KotlinReturnValue
import com.copperleaf.dokka.json.models.KotlinReceiverType
import com.copperleaf.dokka.json.models.KotlinReturnType
import com.copperleaf.dokka.json.models.SignatureComponent
import org.jetbrains.dokka.DocumentationNode
import org.jetbrains.dokka.NodeKind
Expand All @@ -13,7 +14,8 @@ fun DocumentationNode.toMethod(): KotlinMethod {
assert(this.isMethod) { "node must be a Function" }
val modifiers = this.modifiers
val parameters = this.parameters
val returnValue = this.returnValue
val receiverType = this.receiverType
val returnType = this.returnType
return KotlinMethod(
this,
this.simpleName,
Expand All @@ -22,47 +24,74 @@ fun DocumentationNode.toMethod(): KotlinMethod {
this.summary.textLength,
modifiers,
parameters,
returnValue,
receiverType,
returnType,
this.methodSignature(
modifiers,
parameters,
returnValue
receiverType,
returnType
)
)
}

fun DocumentationNode.methodSignature(
modifiers: List<String>,
parameters: List<KotlinParameter>,
returnValue: KotlinReturnValue
receiverType: KotlinReceiverType?,
returnType: KotlinReturnType
): List<SignatureComponent> {
val signatureComponents = mutableListOf<SignatureComponent>()

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

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

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

if (returnValue.name != "Unit") {
if (returnType.name != "Unit") {
signatureComponents.add(SignatureComponent("punctuation", ": ", ""))
val node = returnValue.node as DocumentationNode
val nodeType = node.asType()
signatureComponents.appendParameterType(nodeType)
signatureComponents.addAll(returnType.signature)
}

return signatureComponents
}

val DocumentationNode.returnValue: KotlinReturnValue
val DocumentationNode.returnType: KotlinReturnType
get() {
val it = this.detail(NodeKind.Type)
return KotlinReturnValue(
return KotlinReturnType(
it,
it.simpleName,
it.qualifiedName,
it.contentText,
it.summary.textLength,
it.simpleType,
it.nullable
it.qualifiedType,
it.asType().toTypeSignature()
)
}
}

val DocumentationNode.receiverType: KotlinReceiverType?
get() {
val it = this.detailOrNull(NodeKind.Receiver)
return if (it == null)
null
else
KotlinReceiverType(
it,
it.simpleName,
it.qualifiedName,
it.contentText,
it.summary.textLength,
it.simpleType,
it.qualifiedType,
it.asType().toTypeSignature()
)
}

Loading

0 comments on commit 0dbc632

Please sign in to comment.