Skip to content

Commit 383265f

Browse files
committed
Show implicit modifier in implicit function types
1 parent 256bb3d commit 383265f

File tree

7 files changed

+61
-48
lines changed

7 files changed

+61
-48
lines changed

doc-tool/src/dotty/tools/dottydoc/core/TypeLinkingPhases.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ trait TypeLinker extends MemberLookup {
106106
ref.copy(left = linkReference(ent, left, packs), right = linkReference(ent, right, packs))
107107
case ref @ NamedReference(_, rf, _, _) =>
108108
ref.copy(ref = linkRef(rf))
109-
case ref @ FunctionReference(args, rv) =>
109+
case ref @ FunctionReference(args, rv, _) =>
110110
ref.copy(args = args.map(linkReference(ent, _, packs)), returnValue = linkReference(ent, rv, packs))
111111
case ref @ TupleReference(args) =>
112112
ref.copy(args = args.map(linkRef))

doc-tool/src/dotty/tools/dottydoc/model/JavaConverters.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ object JavaConverters {
7474
"scala" -> ref
7575
).asJava
7676

77-
case FunctionReference(args, returnValue) => Map(
77+
case FunctionReference(args, returnValue, isImplicit) => Map(
7878
"kind" -> "FunctionReference",
7979
"args" -> args.map(_.asJava).asJava,
8080
"returnValue" -> returnValue.asJava,
81+
"isImplicit" -> isImplicit,
8182
"scala" -> ref
8283
).asJava
8384

@@ -273,15 +274,15 @@ object JavaConverters {
273274
e.paramLists.map { paramList =>
274275
Map(
275276
"isImplicit" -> paramList.isImplicit,
276-
"list" -> paramList.list.map(_.showReference).asJava
277+
"list" -> paramList.list.map(_.asJava).asJava
277278
).asJava
278279
}
279280
.asJava
280281
}
281282
)
282283

283284
def returnValue(e: ReturnValue) =
284-
Map("returnValue" -> e.returnValue.showReference)
285+
Map("returnValue" -> e.returnValue.asJava)
285286

286287
entity(e) ++ (e match {
287288
case e: Package => members(e)

doc-tool/src/dotty/tools/dottydoc/model/factories.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ object factories {
6464
val cls = tycon.typeSymbol
6565

6666
if (defn.isFunctionClass(cls))
67-
FunctionReference(args.init.map(expandTpe(_, Nil)), expandTpe(args.last))
67+
FunctionReference(args.init.map(expandTpe(_, Nil)), expandTpe(args.last), defn.isImplicitFunctionClass(cls))
6868
else if (defn.isTupleClass(cls))
6969
TupleReference(args.map(expandTpe(_, Nil)))
7070
else {

doc-tool/src/dotty/tools/dottydoc/model/references.scala

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object references {
66
final case class TypeReference(title: String, tpeLink: MaterializableLink, paramLinks: List[Reference]) extends Reference
77
final case class OrTypeReference(left: Reference, right: Reference) extends Reference
88
final case class AndTypeReference(left: Reference, right: Reference) extends Reference
9-
final case class FunctionReference(args: List[Reference], returnValue: Reference) extends Reference
9+
final case class FunctionReference(args: List[Reference], returnValue: Reference, isImplicit: Boolean) extends Reference
1010
final case class TupleReference(args: List[Reference]) extends Reference
1111
final case class BoundsReference(low: Reference, high: Reference) extends Reference
1212
final case class NamedReference(title: String, ref: Reference, isByName: Boolean = false, isRepeated: Boolean = false) extends Reference
@@ -27,43 +27,4 @@ object references {
2727
})
2828
}
2929
final case class NoLink(title: String, target: String) extends MaterializableLink
30-
31-
implicit class ReferenceShower(val ref: Reference) extends AnyVal {
32-
def showReference: String = ref match {
33-
case TypeReference(title, _, tparams) =>
34-
title + {
35-
if (tparams.nonEmpty) tparams.map(_.showReference).mkString("[", ",", "]")
36-
else ""
37-
}
38-
39-
case OrTypeReference(left, right) =>
40-
left.showReference + " | " + right.showReference
41-
case AndTypeReference(left, right) =>
42-
left.showReference + " & " + right.showReference
43-
44-
case FunctionReference(args, ret) =>
45-
if (args.isEmpty)
46-
"() => " + ret.showReference
47-
else if (args.tail.isEmpty)
48-
args.head.showReference + " => " + ret.showReference
49-
else
50-
args.mkString("(", ",", s") => ${ret.showReference}")
51-
52-
case TupleReference(xs) =>
53-
xs.mkString("(", ",", ")")
54-
55-
case BoundsReference(lo, hi) =>
56-
lo.showReference + "<: " + hi.showReference
57-
58-
case NamedReference(title, ref, isByName, isRepeated) =>
59-
val byName = if (isByName) "=> " else ""
60-
val repeated = if (isRepeated) "*" else ""
61-
s"$title: $byName${ref.showReference}$repeated"
62-
63-
case ConstantReference(title) => title
64-
case EmptyReference =>
65-
assert(false, "unexpected empty reference")
66-
"<empty reference>"
67-
}
68-
}
6930
}

doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ object tags {
8585
case AndTypeReference(left, right) =>
8686
s"""${renderReference(left)}<span class="and-or-separator"> &amp; </span>${renderReference(right)}"""
8787

88-
case FunctionReference(args, returnValue) => {
88+
case FunctionReference(args, returnValue, isImplicit) => {
89+
val implicitPrefix = if (isImplicit) "implicit " else ""
8990
val params =
9091
if (args.isEmpty) "<span>() =&gt; </span>"
9192
else if (args.tail.isEmpty) renderReference(args.head) + """<span class="right-arrow"> =&gt; </span>"""
9293
else args.map(renderReference).mkString("<span>(</span>", "<span>, </span>", "<span>) =&gt; </span>")
9394

94-
params + renderReference(returnValue)
95+
implicitPrefix + params + renderReference(returnValue)
9596
}
9697

9798
case TupleReference(args) =>

doc-tool/test/dotty/tools/dottydoc/JavaConverterTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ class JavaConverterTest {
186186
case AndTypeReference(left, right) =>
187187
assertSerializedCorrectly(left, actual.get("left"))
188188
assertSerializedCorrectly(right, actual.get("right"))
189-
case FunctionReference(args, returnValue) =>
189+
case FunctionReference(args, returnValue, isImplicit) =>
190+
assertEquals(isImplicit, actual.get("isImplicit"))
190191
assertEach(args, actual.get("args")) { (exp, act) =>
191192
assertSerializedCorrectly(exp, act)
192193
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dotty.tools
2+
package dottydoc
3+
4+
import dotty.tools.dotc.util.SourceFile
5+
import dotty.tools.dottydoc.model._
6+
import dotty.tools.dottydoc.model.internal._
7+
import dotty.tools.dottydoc.model.references._
8+
9+
import org.junit.Test
10+
import org.junit.Assert.{assertTrue, fail}
11+
12+
class TypeRenderingTest extends DottyDocTest {
13+
@Test def renderImplicitFunctionType = {
14+
val source = new SourceFile(
15+
"ImplicitFunctionType.scala",
16+
"""
17+
|package scala
18+
|
19+
|trait Test {
20+
| def a: implicit Int => Int = ???
21+
| def b(x: implicit Int => Int) = ???
22+
| type c = implicit Int => Int
23+
|}
24+
""".stripMargin
25+
)
26+
27+
def checkImplicitFunctionType(ref: Reference) = ref match {
28+
case FunctionReference(_, _, isImplicit) =>
29+
assertTrue("Should be an implicit function type", isImplicit)
30+
case _ =>
31+
fail("Unexpected: " + ref)
32+
}
33+
34+
checkSources(source :: Nil) { packages =>
35+
packages("scala") match {
36+
case PackageImpl(_, _, _, List(trt: Trait), _, _, _, _) =>
37+
val List(a: Def, b: Def, c: TypeAlias) = trt.members.sortBy(_.name)
38+
checkImplicitFunctionType(a.returnValue)
39+
b.paramLists.head.list.head match {
40+
case NamedReference("x", ref, _, _) =>
41+
checkImplicitFunctionType(ref)
42+
case _ =>
43+
fail("Unexpected: " + b.paramLists.head.list.head)
44+
}
45+
checkImplicitFunctionType(c.alias.get)
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)