added support for properties in kdoc

This commit is contained in:
James Strachan
2012-02-24 07:25:07 +00:00
parent d330e4cd77
commit de3d5edb2a
4 changed files with 119 additions and 48 deletions
@@ -241,6 +241,7 @@ DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHO
&nbsp;""")
}
printPropertySummary(klass.properties)
printFunctionSummary(klass.functions)
printFunctionDetail(klass.functions)
@@ -6,6 +6,7 @@ import org.jetbrains.kotlin.model.KPackage
import org.jetbrains.kotlin.template.TextTemplate
import org.jetbrains.kotlin.model.KFunction
import java.util.Collection
import org.jetbrains.kotlin.model.KProperty
abstract class KDocTemplate() : TextTemplate() {
fun rootHref(pkg: KPackage): String {
@@ -23,11 +24,6 @@ abstract class KDocTemplate() : TextTemplate() {
return "${rootHref(c.pkg)}${c.nameAsPath}.html$postfix"
}
fun extensionsHref(pkg: KPackage, c: KClass): String {
// is inside the pkg so no need to use it...
return "${c.nameAsPath}-extensions.html"
}
fun href(f: KFunction): String {
return if (f.owner is KClass) {
"${href(f.owner)}#${f.link}"
@@ -36,6 +32,19 @@ abstract class KDocTemplate() : TextTemplate() {
}
}
fun href(f: KProperty): String {
return if (f.owner is KClass) {
"${href(f.owner)}#${f.link}"
} else {
"package-summary.html#${f.link}"
}
}
fun extensionsHref(pkg: KPackage, c: KClass): String {
// is inside the pkg so no need to use it...
return "${c.nameAsPath}-extensions.html"
}
fun sourceHref(f: KFunction): String {
return if (f.owner is KClass) {
"${rootHref(f.owner.pkg)}src-html/${f.owner.simpleName}.html#line.${f.sourceLine}"
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.model.KPackage
import org.jetbrains.kotlin.model.KClass
import org.jetbrains.kotlin.model.KFunction
import org.jetbrains.kotlin.model.KAnnotation
import org.jetbrains.kotlin.model.KProperty
abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
@@ -19,7 +20,7 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
fun printFunctionSummary(functions: Collection<KFunction>): Unit {
if (functions.notEmpty()) {
println("""<!-- ========== METHOD SUMMARY =========== -->
println("""<!-- ========== FUNCTION SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
@@ -38,27 +39,6 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
}
}
fun printFunctionDetail(functions: Collection<KFunction>): Unit {
if (functions.notEmpty()) {
println("""
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Method Detail</B></FONT></TH>
</TR>
</TABLE>
""")
for (f in functions) {
printFunctionDetail(f)
}
}
}
fun printFunctionSummary(method: KFunction): Unit {
val deprecated = if (method.deprecated) "<B>Deprecated.</B>" else ""
print("""<TR BGCOLOR="white" CLASS="TableRowColor">
@@ -88,19 +68,40 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("</TR>")
}
fun printFunctionDetail(method: KFunction): Unit {
println("<A NAME=\"${method.name}{${method.parameterTypeText}})\"><!-- --></A><A NAME=\"${method.name}(${method.typeParametersText})\"><!-- --></A><H3>")
println("${method.name}</H3>")
fun printFunctionDetail(functions: Collection<KFunction>): Unit {
if (functions.notEmpty()) {
println("""
<!-- ============ FUNCTION DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2">
<B>Method Detail</B></FONT></TH>
</TR>
</TABLE>
""")
for (f in functions) {
printFunctionDetail(f)
}
}
}
fun printFunctionDetail(function: KFunction): Unit {
println("<A NAME=\"${function.name}{${function.parameterTypeText}})\"><!-- --></A><A NAME=\"${function.name}(${function.typeParametersText})\"><!-- --></A><H3>")
println("${function.name}</H3>")
println("<PRE>")
println("<FONT SIZE=\"-1\">")
printAnnotations(method.annotations)
print("</FONT>${method.modifiers.join(" ")} ")
printAnnotations(function.annotations)
print("</FONT>${function.modifiers.join(" ")} ")
printTypeParameters(method)
print(link(method.returnType))
print(" <A HREF=\"${sourceHref(method)}\"><B>${method.name}</B></A>")
printParameters(method)
val exlist = method.exceptions
printTypeParameters(function)
print(link(function.returnType))
print(" <A HREF=\"${sourceHref(function)}\"><B>${function.name}</B></A>")
printParameters(function)
val exlist = function.exceptions
var first = true
if (!exlist.isEmpty()) {
println(" throws ");
@@ -129,6 +130,59 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("<HR>")
}
fun printPropertySummary(properties: Collection<KProperty>): Unit {
if (properties.notEmpty()) {
println("""<!-- ========== PROPERTY SUMMARY =========== -->
<A NAME="method_summary"><!-- --></A>
<TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY="">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2">
<B>Property Summary</B></FONT></TH>
</TR>""")
for (f in properties) {
printPropertySummary(f)
}
println("""</TABLE>
&nbsp;
<P>
""")
}
}
fun printPropertySummary(property: KProperty): Unit {
val deprecated = if (property.deprecated) "<B>Deprecated.</B>" else ""
print("""<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>""")
/*
if (!property.typeParameters.isEmpty()) {
println("""<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" SUMMARY="">
<TR ALIGN="right" VALIGN="">
<TD NOWRAP><FONT SIZE="-1">
<CODE>""")
printTypeParameters(property)
println("<BR>")
print(link(property.returnType))
println("""</CODE></FONT></TD>
</TR>
</TABLE>""")
} else {
print(link(property.returnType))
}
*/
print(link(property.returnType))
println("</CODE></FONT></TD>")
print("<TD><CODE><B><A HREF=\"${href(property)}\">${property.name}</A></B>")
//printParameters(property)
println("</CODE>")
println("")
println("<BR>")
println("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${deprecated}&nbsp;${property.detailedDescription}</TD>")
println("</TR>")
}
fun printTypeParameters(method: KFunction): Unit {
val typeParameters = method.typeParameters
if (!typeParameters.isEmpty()) {
@@ -143,17 +143,21 @@ class KModel(var context: BindingContext, var title: String = "Documentation", v
try {
val descriptors = scope.getAllDescriptors()
for (descriptor in descriptors) {
if (descriptor is CallableDescriptor) {
if (descriptor is PropertyDescriptor) {
if (owner is KClass) {
val name = descriptor.getName()
println("Found Property on owner: $owner with name: $name")
val returnType = getClass(descriptor.getReturnType())
if (returnType != null) {
val property = KProperty(owner, descriptor, name, returnType)
owner.properties.add(property)
}
}
} else if (descriptor is CallableDescriptor) {
val function = createFunction(owner, descriptor)
if (function != null) {
list.add(function)
}
} else if (descriptor is PropertyDescriptor) {
if (owner is KClass) {
val name = descriptor.getName()
val property = KProperty(descriptor, name)
owner.properties.add(property)
}
}
}
} catch (e: Throwable) {
@@ -261,7 +265,9 @@ abstract class KAnnotated {
public open var description: String = ""
public open var detailedDescription: String = ""
get() = if ($detailedDescription.notEmpty()) $detailedDescription else description
get() = if ($detailedDescription.notEmpty()) $detailedDescription else description
public open var deprecated: Boolean = false
}
class KPackage(val model: KModel, val descriptor: NamespaceDescriptor,
@@ -401,7 +407,6 @@ class KClass(val pkg: KPackage, val descriptor: ClassDescriptor,
class KFunction(val owner: KClassOrPackage, val name: String,
var returnType: KClass,
var deprecated: Boolean = false,
var extensionClass: KClass? = null,
var modifiers: List<String> = arrayList<String>(),
var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>(),
@@ -422,7 +427,6 @@ class KFunction(val owner: KClassOrPackage, val name: String,
fun toString() = "fun ($name)"
/** TODO generate a link with the argument type names */
public val link: String = "$name($parameterTypeText)"
/** Returns a list of generic type parameter names kinds like "A, I" */
@@ -440,10 +444,13 @@ class KFunction(val owner: KClassOrPackage, val name: String,
}
}
class KProperty(var descriptor: PropertyDescriptor, var name: String) : KAnnotated(), Comparable<KProperty> {
class KProperty(val owner: KClassOrPackage, val descriptor: PropertyDescriptor, val name: String,
val returnType: KClass) : KAnnotated(), Comparable<KProperty> {
override fun compareTo(other: KProperty): Int = name.compareTo(other.name)
public val link: String = "$name"
fun equals(other: KFunction) = name == other.name
fun toString() = "property $name}"