improved the kdoc format so it looks a bit more like kotlin code

This commit is contained in:
James Strachan
2012-03-30 12:35:26 +01:00
parent 8b45b26785
commit 68d8e7fba0
4 changed files with 57 additions and 25 deletions
@@ -98,7 +98,7 @@ class KDocConfig() {
} }
} }
protected class LongestFirstStringComparator() : Comparator<String> { private class LongestFirstStringComparator() : Comparator<String> {
override fun compare(s1: String?, s2: String?): Int { override fun compare(s1: String?, s2: String?): Int {
return compareBy(s1, s2, { (s: String) -> s.length() }, { (s: String) -> s }) return compareBy(s1, s2, { (s: String) -> s.length() }, { (s: String) -> s })
} }
@@ -38,6 +38,7 @@ import org.jetbrains.jet.lang.descriptors.ClassKind
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile import com.intellij.psi.PsiFile
import com.intellij.psi.PsiDirectory import com.intellij.psi.PsiDirectory
import org.jetbrains.jet.lang.descriptors.Visibilities
/** /**
@@ -254,7 +255,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) {
} }
} }
protected fun addFunctions(owner: KClassOrPackage, scope: JetScope): Unit { fun addFunctions(owner: KClassOrPackage, scope: JetScope): Unit {
try { try {
val descriptors = scope.getAllDescriptors() val descriptors = scope.getAllDescriptors()
for (descriptor in descriptors) { for (descriptor in descriptors) {
@@ -303,14 +304,16 @@ class KModel(var context: BindingContext, val config: KDocConfig) {
configureComments(function, descriptor) configureComments(function, descriptor)
val receiver = descriptor.getReceiverParameter() val receiver = descriptor.getReceiverParameter()
if (receiver is ExtensionReceiver) { if (receiver is ExtensionReceiver) {
function.extensionClass = getType(receiver.getType())?.klass val receiverType = getType(receiver.getType())
function.receiverType = receiverType
function.extensionClass = receiverType?.klass
} }
return function return function
} }
return null return null
} }
protected fun addTypeParameters(answer: List<KTypeParameter>, descriptors: List<TypeParameterDescriptor?>): Unit { fun addTypeParameters(answer: List<KTypeParameter>, descriptors: List<TypeParameterDescriptor?>): Unit {
for (typeParam in descriptors) { for (typeParam in descriptors) {
if (typeParam != null) { if (typeParam != null) {
val p = createTypeParameter(typeParam) val p = createTypeParameter(typeParam)
@@ -340,7 +343,7 @@ class KModel(var context: BindingContext, val config: KDocConfig) {
} }
protected fun fileFor(descriptor: DeclarationDescriptor): String? { fun fileFor(descriptor: DeclarationDescriptor): String? {
val psiElement = getPsiElement(descriptor) val psiElement = getPsiElement(descriptor)
return psiElement?.getContainingFile()?.getName() return psiElement?.getContainingFile()?.getName()
} }
@@ -844,7 +847,7 @@ class KClass(val pkg: KPackage, val descriptor: ClassDescriptor,
fun isApi(): Boolean { fun isApi(): Boolean {
val visibility = descriptor.getVisibility() val visibility = descriptor.getVisibility()
return visibility.isAPI() return visibility.isPublicAPI()
} }
val kind: String val kind: String
@@ -870,9 +873,9 @@ class KClass(val pkg: KPackage, val descriptor: ClassDescriptor,
val visibility: String val visibility: String
get() { get() {
val v = descriptor.getVisibility() val v = descriptor.getVisibility()
return if (v == Visibility.PUBLIC) "public" return if (v == Visibilities.PUBLIC) "public"
else if (v == Visibility.PROTECTED) "protected" else if (v == Visibilities.PROTECTED) "protected"
else if (v == Visibility.PRIVATE) "private" else if (v == Visibilities.PRIVATE) "private"
else "" else ""
} }
@@ -911,6 +914,7 @@ class KClass(val pkg: KPackage, val descriptor: ClassDescriptor,
class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage, val name: String, class KFunction(val descriptor: CallableDescriptor, val owner: KClassOrPackage, val name: String,
var returnType: KType, var returnType: KType,
var parameters: List<KParameter>, var parameters: List<KParameter>,
var receiverType: KType? = null,
var extensionClass: KClass? = null, var extensionClass: KClass? = null,
var modifiers: List<String> = arrayList<String>(), var modifiers: List<String> = arrayList<String>(),
var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>(), var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>(),
@@ -954,6 +958,8 @@ class KProperty(val owner: KClassOrPackage, val descriptor: PropertyDescriptor,
fun equals(other: KFunction) = name == other.name fun equals(other: KFunction) = name == other.name
fun isVar(): Boolean = descriptor.isVar()
fun toString() = "property $name" fun toString() = "property $name"
} }
@@ -95,9 +95,9 @@ function windowTitle()
</TR> </TR>
<TR> <TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;<A HREF="#nested_class_summary">NESTED</A>&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD> SUMMARY:&nbsp;<A HREF="#nested_class_summary">NESTED</A>&nbsp;|&nbsp;PROPERTY&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">FUNCTION</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD> DETAIL:&nbsp;PROPERTY&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">FUNCTION</A></FONT></TD>
</TR> </TR>
</TABLE> </TABLE>
<A NAME="skip-navbar_top"></A> <A NAME="skip-navbar_top"></A>
@@ -151,9 +151,9 @@ DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHO
</TR> </TR>
<TR> <TR>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
SUMMARY:&nbsp;<A HREF="#nested_class_summary">NESTED</A>&nbsp;|&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">METHOD</A></FONT></TD> SUMMARY:&nbsp;<A HREF="#nested_class_summary">NESTED</A>&nbsp;|&nbsp;PROPERTY&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_summary">FUNCTION</A></FONT></TD>
<TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2"> <TD VALIGN="top" CLASS="NavBarCell3"><FONT SIZE="-2">
DETAIL:&nbsp;FIELD&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">METHOD</A></FONT></TD> DETAIL:&nbsp;PROPERTY&nbsp;|&nbsp;CONSTR&nbsp;|&nbsp;<A HREF="#method_detail">FUNCTION</A></FONT></TD>
</TR> </TR>
</TABLE> </TABLE>
<A NAME="skip-navbar_bottom"></A> <A NAME="skip-navbar_bottom"></A>
@@ -18,6 +18,11 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
protected override fun relativePrefix(): String = pkg.nameAsRelativePath protected override fun relativePrefix(): String = pkg.nameAsRelativePath
val funKeyword = keyword("fun")
val valKeyword = keyword("val")
val varKeyword = keyword("var")
protected fun keyword(name: String): String = "<B>$name</B>"
fun printFunctionSummary(functions: Collection<KFunction>): Unit { fun printFunctionSummary(functions: Collection<KFunction>): Unit {
if (functions.notEmpty()) { if (functions.notEmpty()) {
@@ -40,6 +45,7 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
} }
} }
fun printFunctionSummary(function: KFunction): Unit { fun printFunctionSummary(function: KFunction): Unit {
val deprecated = if (function.deprecated) "<B>Deprecated.</B>" else "" val deprecated = if (function.deprecated) "<B>Deprecated.</B>" else ""
print("""<TR BGCOLOR="white" CLASS="TableRowColor"> print("""<TR BGCOLOR="white" CLASS="TableRowColor">
@@ -50,18 +56,22 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
<TR ALIGN="right" VALIGN=""> <TR ALIGN="right" VALIGN="">
<TD NOWRAP><FONT SIZE="-1"> <TD NOWRAP><FONT SIZE="-1">
<CODE>""") <CODE>""")
print("$funKeyword ")
printTypeParameters(function) printTypeParameters(function)
println("""<BR>""") printReceiverType(function, "<BR>")
print(link(function.returnType))
println("""</CODE></FONT></TD> println("""</CODE></FONT></TD>
</TR> </TR>
</TABLE>""") </TABLE>""")
} else { } else {
print(link(function.returnType)) print(funKeyword)
printReceiverType(function)
} }
// print receiver type
println("""</CODE></FONT></TD>""") println("""</CODE></FONT></TD>""")
print("""<TD><CODE><B><A HREF="${href(function)}">${function.name}</A></B>""") print("""<TD><CODE><B><A HREF="${href(function)}">${function.name}</A></B>""")
printParameters(function) printParameters(function)
print(": ")
print(link(function.returnType))
println("""</CODE>""") println("""</CODE>""")
println("") println("")
if (true) { if (true) {
@@ -75,6 +85,18 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("""</TR>""") println("""</TR>""")
} }
fun printReceiverType(function: KFunction, prefix: String = " ", postfix: String = "", none: String = ""): Unit {
val receiverType = function.receiverType
if (receiverType != null) {
print(prefix)
print(link(receiverType))
print(postfix)
} else {
print(none)
}
}
fun printFunctionDetail(functions: Collection<KFunction>): Unit { fun printFunctionDetail(functions: Collection<KFunction>): Unit {
if (functions.notEmpty()) { if (functions.notEmpty()) {
println(""" println("""
@@ -102,13 +124,15 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("""<PRE>""") println("""<PRE>""")
println("""<FONT SIZE="-1">""") println("""<FONT SIZE="-1">""")
printAnnotations(function.annotations) printAnnotations(function.annotations)
print("""</FONT>${function.modifiers.join(" ")} """) print("""</FONT>${function.modifiers.join(" ")} $funKeyword""")
printTypeParameters(function) printTypeParameters(function, " ")
printReceiverType(function, " ", ".", " ")
print("""<A HREF="${sourceHref(function)}"><B>${function.name}</B></A>""")
printParameters(function)
print(": ")
print(link(function.returnType)) print(link(function.returnType))
print(""" <A HREF="${sourceHref(function)}"><B>${function.name}</B></A>""")
printParameters(function)
val exlist = function.exceptions val exlist = function.exceptions
var first = true var first = true
if (!exlist.isEmpty()) { if (!exlist.isEmpty()) {
@@ -163,8 +187,9 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
fun printPropertySummary(property: KProperty): Unit { fun printPropertySummary(property: KProperty): Unit {
val deprecated = if (property.deprecated) "<B>Deprecated.</B>" else "" val deprecated = if (property.deprecated) "<B>Deprecated.</B>" else ""
print("""<TR BGCOLOR="white" CLASS="TableRowColor"> print("""<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"> <TD ALIGN="right" WIDTH="1%"><FONT SIZE="-1">
<CODE>""") <CODE>""")
print(if (property.isVar()) varKeyword else valKeyword)
/* /*
if (!property.typeParameters.isEmpty()) { if (!property.typeParameters.isEmpty()) {
println("""<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" SUMMARY=""> println("""<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" SUMMARY="">
@@ -181,9 +206,9 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
print(link(property.returnType)) print(link(property.returnType))
} }
*/ */
print(link(property.returnType))
println("""</CODE></FONT></TD>""") println("""</CODE></FONT></TD>""")
print("""<TD><CODE><B><A HREF="${href(property)}">${property.name}</A></B>""") print("""<TD><CODE><B><A HREF="${href(property)}">${property.name}</A></B>: """)
print(link(property.returnType))
//printParameters(property) //printParameters(property)
println("""</CODE>""") println("""</CODE>""")
println("""""") println("""""")
@@ -192,9 +217,10 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
println("""</TR>""") println("""</TR>""")
} }
fun printTypeParameters(method: KFunction): Unit { fun printTypeParameters(method: KFunction, separator: String = ""): Unit {
val typeParameters = method.typeParameters val typeParameters = method.typeParameters
if (!typeParameters.isEmpty()) { if (!typeParameters.isEmpty()) {
print(separator)
print("&lt") print("&lt")
var separator = "" var separator = ""
for (t in typeParameters) { for (t in typeParameters) {
@@ -212,7 +238,7 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
} }
} }
} }
print("&gt;&nbsp;") print("&gt")
} }
} }