added type parameter support

This commit is contained in:
James Strachan
2012-03-01 07:02:53 +00:00
parent 52dcbb565f
commit f50ae6be6f
2 changed files with 59 additions and 21 deletions
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.model.KClass
import org.jetbrains.kotlin.model.KFunction
import org.jetbrains.kotlin.model.KAnnotation
import org.jetbrains.kotlin.model.KProperty
import org.jetbrains.kotlin.model.KTypeParameter
abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
@@ -39,32 +40,33 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
}
}
fun printFunctionSummary(method: KFunction): Unit {
val deprecated = if (method.deprecated) "<B>Deprecated.</B>" else ""
fun printFunctionSummary(function: KFunction): Unit {
val deprecated = if (function.deprecated) "<B>Deprecated.</B>" else ""
print("""<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>""")
if (!method.typeParameters.isEmpty()) {
if (!function.typeParameters.isEmpty()) {
println("""<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" SUMMARY="">
<TR ALIGN="right" VALIGN="">
<TD NOWRAP><FONT SIZE="-1">
<CODE>""")
printTypeParameters(method)
printTypeParameters(function)
println("""<BR>""")
print(link(method.returnType))
print(link(function.returnType))
printTypeParameterNames(function.returnType.typeParameters)
println("""</CODE></FONT></TD>
</TR>
</TABLE>""")
} else {
print(link(method.returnType))
print(link(function.returnType))
}
println("""</CODE></FONT></TD>""")
print("""<TD><CODE><B><A HREF="${href(method)}">${method.name}</A></B>""")
printParameters(method)
print("""<TD><CODE><B><A HREF="${href(function)}">${function.name}</A></B>""")
printParameters(function)
println("""</CODE>""")
println("""""")
println("""<BR>""")
println("""&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${deprecated}&nbsp;${method.detailedDescription}</TD>""")
println("""&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${deprecated}&nbsp;${function.detailedDescription}</TD>""")
println("""</TR>""")
}
@@ -99,6 +101,8 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
printTypeParameters(function)
print(link(function.returnType))
printTypeParameterNames(function.returnType.typeParameters)
print(""" <A HREF="${sourceHref(function)}"><B>${function.name}</B></A>""")
printParameters(function)
val exlist = function.exceptions
@@ -204,7 +208,7 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
}
}
}
print("&gt")
print("&gt;&nbsp;")
}
}
@@ -215,10 +219,23 @@ abstract class PackageTemplateSupport(open val pkg: KPackage) : KDocTemplate() {
if (first) first = false else print(", ")
print("${p.name}:&nbsp;")
print(link(p.klass))
printTypeParameterNames(method.typeParameters)
}
print(")")
}
fun printTypeParameterNames(typeParameters: List<KTypeParameter>): Unit {
if (!typeParameters.isEmpty()) {
print("&lt")
var separator = ""
for (t in typeParameters) {
print(separator)
separator = ", "
print(t.name)
}
print("&gt;")
}
}
fun printAnnotations(annotations: Collection<KAnnotation>): Unit {
for (a in annotations) {
println(link(a))
@@ -20,6 +20,7 @@ import org.jetbrains.jet.lang.resolve.scopes.receivers.ExtensionReceiver
import org.jetbrains.jet.lang.descriptors.ValueParameterDescriptor
import org.jetbrains.jet.lang.psi.JetFile
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
fun containerName(descriptor: DeclarationDescriptor): String = qualifiedName(descriptor.containingDeclaration)
@@ -212,6 +213,7 @@ class KModel(var context: BindingContext, var title: String = "Documentation", v
val returnType = getClass(descriptor.getReturnType())
if (returnType != null) {
val function = KFunction(owner, descriptor.getName() ?: "null", returnType)
addTypeParameters(function.typeParameters, descriptor.getTypeParameters())
function.description = commentsFor(descriptor)
val params = descriptor.getValueParameters()
for (param in params) {
@@ -231,6 +233,24 @@ class KModel(var context: BindingContext, var title: String = "Documentation", v
return null
}
protected fun addTypeParameters(answer: List<KTypeParameter>, descriptors: List<TypeParameterDescriptor?>): Unit {
for (typeParam in descriptors) {
if (typeParam != null) {
val p = createTypeParameter(typeParam)
if (p != null){
answer.add(p)
}
}
}
}
protected fun createTypeParameter(descriptor: TypeParameterDescriptor): KTypeParameter? {
val name = descriptor.getName()
val answer = KTypeParameter(name, descriptor)
answer.description = commentsFor(descriptor)
return answer
}
protected fun createParameter(descriptor: ValueParameterDescriptor): KParameter? {
val returnType = getClass(descriptor.getReturnType())
if (returnType != null) {
@@ -322,23 +342,25 @@ class KPackage(val model: KModel, val descriptor: NamespaceDescriptor,
fun toString() = "KPackage($name)"
fun getClass(name: String, classElement: ClassDescriptor): KClass {
fun getClass(name: String, descriptor: ClassDescriptor): KClass {
var created = false
val klass = classMap.getOrPut(name) {
created = true
KClass(this, classElement, name)
KClass(this, descriptor, name)
}
if (created) {
local = true
klass.description = model.commentsFor(classElement)
val superTypes = classElement.getTypeConstructor().getSupertypes()
klass.description = model.commentsFor(descriptor)
val typeConstructor = descriptor.getTypeConstructor()
val superTypes = typeConstructor.getSupertypes()
for (st in superTypes) {
val sc = model.getClass(st)
if (sc != null) {
klass.baseClasses.add(sc)
}
}
model.addFunctions(klass, classElement.getDefaultType().getMemberScope())
model.addFunctions(klass, descriptor.getDefaultType().getMemberScope())
model.addTypeParameters(klass.typeParameters, typeConstructor.getParameters())
}
return klass
}
@@ -404,6 +426,7 @@ class KClass(val pkg: KPackage, val descriptor: ClassDescriptor,
val simpleName: String,
var kind: String = "class", var group: String = "Other",
var annotations: List<KAnnotation> = arrayList<KAnnotation>(),
var typeParameters: List<KTypeParameter> = arrayList<KTypeParameter>(),
var since: String = "",
var authors: List<String> = arrayList<String>(),
var baseClasses: List<KClass> = arrayList<KClass>(),
@@ -477,7 +500,7 @@ class KFunction(val owner: KClassOrPackage, val name: String,
public val parameterTypeText: String
get() {
if (_parameterTypeText == null) {
_parameterTypeText = typeParameters.map{ it.klass.name }.join(", ")
_parameterTypeText = typeParameters.map{ it.name }.join(", ")
}
return _parameterTypeText.sure()
}
@@ -502,12 +525,10 @@ class KParameter(val name: String,
}
class KTypeParameter(val name: String,
var klass: KClass,
var extends: List<KClass> = arrayList<KClass>()) {
val descriptor: TypeParameterDescriptor,
var extends: List<KClass> = arrayList<KClass>()) : KAnnotated() {
fun toString() = if (extends.isEmpty()) name else {
"$name extends ${extends.map{it.name}.join(" & ")}"
}
fun toString() = "$name"
}
class KAnnotation(var klass: KClass) : KAnnotated() {