@@ -11,7 +11,7 @@ import util.Property.Key
11
11
import parsing .Parsers .Parser
12
12
13
13
object Comments {
14
- val ContextDoc = new Key [ContextDocstrings ]
14
+ val ContextDoc = new Key [ContextDocstrings ]
15
15
16
16
/** Decorator for getting docbase out of context */
17
17
implicit class CommentsContext (val ctx : Context ) extends AnyVal {
@@ -37,7 +37,12 @@ object Comments {
37
37
doc.map(d => _docstrings += (sym -> d))
38
38
}
39
39
40
- abstract case class Comment (pos : Position , raw : String )(implicit ctx : Context ) { self =>
40
+ /** A `Comment` contains the unformatted docstring as well as a position
41
+ *
42
+ * The `Comment` contains functionality to create versions of itself without
43
+ * `@usecase ` sections as well as functionality to map the `raw` docstring
44
+ */
45
+ abstract case class Comment (pos : Position , raw : String ) { self =>
41
46
def isExpanded : Boolean
42
47
43
48
def usecases : List [UseCase ]
@@ -49,15 +54,15 @@ object Comments {
49
54
val usecases = self.usecases
50
55
}
51
56
52
- def withUsecases : Comment = new Comment (pos, stripUsecases) {
57
+ def withUsecases ( implicit ctx : Context ) : Comment = new Comment (pos, stripUsecases) {
53
58
val isExpanded = self.isExpanded
54
59
val usecases = parseUsecases
55
60
}
56
61
57
62
private [this ] lazy val stripUsecases : String =
58
63
removeSections(raw, " @usecase" , " @define" )
59
64
60
- private [this ] lazy val parseUsecases : List [UseCase ] =
65
+ private [this ] def parseUsecases ( implicit ctx : Context ) : List [UseCase ] =
61
66
if (! raw.startsWith(" /**" ))
62
67
List .empty[UseCase ]
63
68
else
@@ -73,7 +78,7 @@ object Comments {
73
78
* def foo: A = ???
74
79
* }}}
75
80
*/
76
- private [this ] def decomposeUseCase (start : Int , end : Int ): UseCase = {
81
+ private [this ] def decomposeUseCase (start : Int , end : Int )( implicit ctx : Context ) : UseCase = {
77
82
def subPos (start : Int , end : Int ) =
78
83
if (pos == NoPosition ) NoPosition
79
84
else {
@@ -102,22 +107,29 @@ object Comments {
102
107
}
103
108
}
104
109
105
- case class UseCase (comment : Comment , code : String , codePos : Position )( implicit ctx : Context ) {
110
+ abstract case class UseCase (comment : Comment , code : String , codePos : Position ) {
106
111
/** Set by typer */
107
112
var tpdCode : tpd.DefDef = _
108
113
109
- lazy val untpdCode : untpd.Tree = {
110
- val tree = new Parser ( new SourceFile ( " <usecase> " , code)).localDef(codePos.start, EmptyFlags )
114
+ def untpdCode : untpd.Tree
115
+ }
111
116
112
- tree match {
113
- case tree : untpd.DefDef =>
114
- val newName = (tree.name.show + " $" + codePos + " $doc" ).toTermName
115
- untpd.DefDef (newName, tree.tparams, tree.vparamss, tree.tpt, tree.rhs)
116
- case _ =>
117
- ctx.error(" proper definition was not found in `@usecase`" , codePos)
118
- tree
117
+ object UseCase {
118
+ def apply (comment : Comment , code : String , codePos : Position )(implicit ctx : Context ) =
119
+ new UseCase (comment, code, codePos) {
120
+ val untpdCode = {
121
+ val tree = new Parser (new SourceFile (" <usecase>" , code)).localDef(codePos.start, EmptyFlags )
122
+
123
+ tree match {
124
+ case tree : untpd.DefDef =>
125
+ val newName = (tree.name.show + " $" + codePos + " $doc" ).toTermName
126
+ untpd.DefDef (newName, tree.tparams, tree.vparamss, tree.tpt, tree.rhs)
127
+ case _ =>
128
+ ctx.error(" proper definition was not found in `@usecase`" , codePos)
129
+ tree
130
+ }
131
+ }
119
132
}
120
- }
121
133
}
122
134
123
135
/**
0 commit comments