Skip to content

Commit 5a6927e

Browse files
committed
Add UsecasePhase to dottydoc
1 parent 45872eb commit 5a6927e

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

dottydoc/src/dotty/tools/dottydoc/DottyDoc.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class DocCompiler extends Compiler {
3131
List(new DocFrontEnd),
3232
List(new DocImplicitsPhase),
3333
List(new DocASTPhase),
34-
List(DocMiniTransformations(new LinkReturnTypes,
34+
List(DocMiniTransformations(new UsecasePhase,
35+
new LinkReturnTypes,
3536
new LinkParamListTypes,
3637
new LinkImplicitlyAddedTypes,
3738
new LinkSuperTypes,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package dotty.tools
2+
package dottydoc
3+
package core
4+
5+
import dotc.core.Contexts.Context
6+
import dotc.ast.tpd
7+
8+
import transform.DocMiniPhase
9+
import model.internal._
10+
import model.factories._
11+
import dotty.tools.dotc.core.Symbols.Symbol
12+
13+
class UsecasePhase extends DocMiniPhase {
14+
private def defdefToDef(d: tpd.DefDef, sym: Symbol)(implicit ctx: Context) = DefImpl(
15+
sym,
16+
d.name.decode.toString,
17+
flags(d), path(d.symbol),
18+
returnType(d.tpt.tpe),
19+
typeParams(d.symbol),
20+
paramLists(d.symbol.info)
21+
)
22+
23+
override def transformDef(implicit ctx: Context) = { case df: DefImpl =>
24+
ctx.docbase.docstring(df.symbol).flatMap(_.usecases.headOption.map(_.tpdCode)).map(defdefToDef(_, df.symbol)).getOrElse(df)
25+
}
26+
}

dottydoc/test/UsecaseTest.scala

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package dotty.tools
2+
package dottydoc
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
7+
import dotc.util.SourceFile
8+
import model._
9+
import model.internal._
10+
import model.references._
11+
12+
class UsecaseTest extends DottyTest {
13+
@Test def simpleUsecase = {
14+
val source = new SourceFile(
15+
"DefWithUseCase.scala",
16+
"""
17+
|package scala
18+
|
19+
|trait Test[A] {
20+
| /** Definition with a "disturbing" signature
21+
| *
22+
| * @usecase def foo: A
23+
| */
24+
| def foo[B]: A => B
25+
|}
26+
""".stripMargin
27+
)
28+
29+
checkSources(source :: Nil) { packages =>
30+
packages("scala") match {
31+
case PackageImpl(_, _, List(trt: Trait), _, _) =>
32+
val List(map: Def) = trt.members
33+
34+
val returnValue = map.returnValue match {
35+
case ref: TypeReference => ref.title
36+
case _ =>
37+
assert(
38+
false,
39+
"Incorrect return value after usecase transformation"
40+
)
41+
""
42+
}
43+
44+
assert(
45+
map.typeParams.isEmpty,
46+
"Type parameters were not stripped by usecase"
47+
)
48+
assert(returnValue == "A", "Incorrect return type after usecase")
49+
}
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)