Skip to content

Commit fe1ff8f

Browse files
committed
Fix #28: render functions passed as arguments
1 parent a284cad commit fe1ff8f

File tree

5 files changed

+85
-21
lines changed

5 files changed

+85
-21
lines changed

dottydoc/js/src/html/Member.scala

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ trait MemberLayout {
119119
"[",
120120
tref
121121
.paramLinks
122-
.map(linkToAnchor)
122+
.map(referenceToLinks)
123123
.flatMap(link => Seq(link, span(cls := "type-separator no-left-margin", ",").render))
124124
.toList.dropRight(1),
125125
"]"
@@ -141,6 +141,30 @@ trait MemberLayout {
141141
span(cls := "type-separator", "&"),
142142
referenceToLinks(right)
143143
).render
144+
145+
case "BoundsReference" =>
146+
val (low, high) = (ref.asInstanceOf[BoundsReference].low, ref.asInstanceOf[BoundsReference].high)
147+
span(
148+
referenceToLinks(low),
149+
span(cls := "type-separator", "<:"),
150+
referenceToLinks(high)
151+
).render
152+
153+
case "FunctionReference" => {
154+
val func = ref.asInstanceOf[FunctionReference]
155+
span(
156+
cls := "no-left-margin",
157+
if (func.args.length > 1) "(" else "",
158+
if (func.args.isEmpty)
159+
span("()")
160+
else func
161+
.args
162+
.map(referenceToLinks)
163+
.flatMap(link => Seq(link, span(cls := "type-separator no-left-margin", ",").render)).init.toList,
164+
if (func.args.length > 1) ") => " else " => ",
165+
referenceToLinks(func.returnValue)
166+
).render
167+
}
144168
}
145169
}
146170

@@ -183,7 +207,7 @@ trait MemberLayout {
183207
returnValue,
184208
"[",
185209
trv.paramLinks
186-
.map(decodeTpeLink)
210+
.map(link)
187211
.flatMap { sp =>
188212
Seq(sp, span(cls := "type-separator no-left-margin", ",").render)
189213
}
@@ -208,6 +232,28 @@ trait MemberLayout {
208232
span(cls := "type-separator", "&"),
209233
link(right)
210234
).render
235+
236+
case "BoundsReference" =>
237+
val (low, high) = (rv.asInstanceOf[BoundsReference].low, rv.asInstanceOf[BoundsReference].high)
238+
span(
239+
link(low),
240+
span(cls := "type-separator", "<:"),
241+
link(high)
242+
).render
243+
case "FunctionReference" =>
244+
val func = rv.asInstanceOf[FunctionReference]
245+
span(
246+
cls := "no-left-margin",
247+
if (func.args.length > 1) "(" else "",
248+
if (func.args.isEmpty)
249+
span("()")
250+
else func
251+
.args
252+
.map(link)
253+
.flatMap(link => Seq(link, span(cls := "type-separator no-left-margin", ",").render)).init.toList,
254+
if (func.args.length > 1) ") => " else " => ",
255+
link(func.returnValue)
256+
).render
211257
}
212258
}
213259

dottydoc/js/src/model/references.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ trait AndTypeReference extends Reference {
3131
val right: Reference
3232
}
3333

34+
@ScalaJSDefined
35+
trait BoundsReference extends Reference {
36+
val low: Reference
37+
val high: Reference
38+
}
39+
3440
@ScalaJSDefined
3541
trait NamedReference extends Reference {
3642
val title: String
@@ -43,6 +49,12 @@ trait ConstantReference extends Reference {
4349
val title: String
4450
}
4551

52+
@ScalaJSDefined
53+
trait FunctionReference extends Reference {
54+
val args: sjs.Array[Reference]
55+
val returnValue: Reference
56+
}
57+
4658
/** Materializable links */
4759
@ScalaJSDefined
4860
sealed trait MaterializableLink extends sjs.Object {

dottydoc/jvm/src/dotty/tools/dottydoc/model/json.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ object json {
4646
s"""{"left":${refToJson(ref.left)},"right":${refToJson(ref.right)},"kind":"AndTypeReference"}"""
4747
case ref: OrTypeReference =>
4848
s"""{"left":${refToJson(ref.left)},"right":${refToJson(ref.right)},"kind":"OrTypeReference"}"""
49+
case ref: BoundsReference =>
50+
s"""{"low":${refToJson(ref.low)},"high":${refToJson(ref.high)},"kind":"BoundsReference"}"""
4951
case ref: NamedReference =>
5052
s"""{"title":${ref.title.json},"ref":${refToJson(ref.ref)},"isByName":${ref.isByName.json},"kind":"NamedReference"}"""
5153
case ref: ConstantReference =>
5254
s"""{"title":${ref.title.json},"kind": "ConstantReference"}"""
55+
case ref: FunctionReference =>
56+
s"""{"args":${ref.args.map(refToJson).mkString("[",",","]")},"returnValue":${refToJson(ref.returnValue)},"kind": "FunctionReference"}"""
5357
}
5458
implicit class ReferenceJson(val ref: Reference) extends AnyVal { def json: String = refToJson(ref) }
5559

dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/comment/BodyEntities.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,7 @@ sealed trait Reference
103103
final case class TypeReference(title: String, tpeLink: MaterializableLink, paramLinks: List[Reference]) extends Reference
104104
final case class OrTypeReference(left: Reference, right: Reference) extends Reference
105105
final case class AndTypeReference(left: Reference, right: Reference) extends Reference
106+
final case class FunctionReference(args: List[Reference], returnValue: Reference) extends Reference
107+
final case class BoundsReference(low: Reference, high: Reference) extends Reference
106108
final case class NamedReference(title: String, ref: Reference, isByName: Boolean = false) extends Reference
107109
final case class ConstantReference(title: String) extends Reference

dottydoc/shared/src/main/scala/dotty/tools/dottydoc/model/factories.scala

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,23 @@ object factories {
5555
TypeReference(name, UnsetLink(name, realQuery), params)
5656
}
5757

58-
def expandTpe(t: Type, params: List[MaterializableLink] = Nil): Reference = t match {
59-
case ref @ RefinedType(parent, rn, info) => {
60-
val paramName = (info match {
61-
case ta: TypeAlias if ta.alias.isInstanceOf[NamedType] =>
62-
ta.alias.asInstanceOf[NamedType].name.show
63-
case _ => rn.show
64-
}).split("\\$").last
65-
val param = UnsetLink(paramName, paramName)
66-
expandTpe(parent, param :: params)
67-
}
68-
case HKApply(tycon, args) =>
69-
def paramName: Type => String = { tpe =>
70-
(tpe match {
71-
case ta: TypeAlias if ta.alias.isInstanceOf[NamedType] =>
72-
ta.alias.asInstanceOf[NamedType].name.show
73-
case _ => tpe.show
74-
}).split("\\$").last
75-
}
76-
expandTpe(tycon, args.map(paramName).map(x => UnsetLink(x,x)))
58+
def expandTpe(t: Type, params: List[Reference] = Nil): Reference = t match {
59+
case tl: TypeLambda =>
60+
//FIXME: should be handled correctly
61+
// example, in `Option`:
62+
//
63+
// {{{
64+
// def companion: GenericCompanion[collection.Iterable]
65+
// }}}
66+
//
67+
// Becomes: def companion: [+X0] -> collection.Iterable[X0]
68+
typeRef(tl.show + " (not handled)")
69+
case AppliedType(tycon, args) =>
70+
FunctionReference(args.init.map(expandTpe(_, Nil)), expandTpe(args.last))
71+
case ref @ RefinedType(parent, rn, info) =>
72+
expandTpe(parent) //FIXME: will be a refined HK, aka class Foo[X] { def bar: List[X] } or similar
73+
case ref @ HKApply(tycon, args) =>
74+
expandTpe(tycon, args.map(expandTpe(_, params)))
7775
case TypeRef(_, n) =>
7876
val name = n.decode.toString.split("\\$").last
7977
typeRef(name, params = params)
@@ -83,6 +81,8 @@ object factories {
8381
OrTypeReference(expandTpe(left), expandTpe(right))
8482
case AndType(left, right) =>
8583
AndTypeReference(expandTpe(left), expandTpe(right))
84+
case tb @ TypeBounds(lo, hi) =>
85+
BoundsReference(expandTpe(lo), expandTpe(hi))
8686
case AnnotatedType(tpe, _) =>
8787
expandTpe(tpe)
8888
case ExprType(tpe) =>

0 commit comments

Comments
 (0)