@@ -40,14 +40,21 @@ object Comments {
4040 *
4141 * The `Comment` contains functionality to create versions of itself without
4242 * `@usecase ` sections as well as functionality to map the `raw` docstring
43+ *
44+ * @param pos The position of this `Comment`.
45+ * @param raw The raw comment, as seen in the source code, without any expansion.
4346 */
4447 abstract case class Comment (pos : Position , raw : String ) { self =>
48+
49+ /** The expansion of this comment */
50+ def expanded : Option [String ]
51+
4552 /** Has this comment been cooked or expanded? */
46- def isExpanded : Boolean
53+ final def isExpanded : Boolean = expanded.isDefined
4754
48- /** The body of this comment, without the `@usecase ` and `@define ` sections. */
49- lazy val body : String =
50- removeSections(raw , " @usecase" , " @define" )
55+ /** The body of this comment, without the `@usecase ` and `@define ` sections, after expansion . */
56+ lazy val expandedBody : Option [ String ] =
57+ expanded.map( removeSections(_ , " @usecase" , " @define" ) )
5158
5259 /**
5360 * The `@usecase ` sections of this comment.
@@ -57,23 +64,26 @@ object Comments {
5764
5865 val isDocComment = raw.startsWith(" /**" )
5966
60- def expand (f : String => String ): Comment = new Comment (pos, f( raw) ) {
61- val isExpanded = true
67+ def expand (f : String => String ): Comment = new Comment (pos, raw) {
68+ val expanded = Some (f(raw))
6269 val usecases = self.usecases
6370 }
6471
6572 def withUsecases (implicit ctx : Context ): Comment = new Comment (pos, raw) {
66- val isExpanded = self.isExpanded
73+ assert(self.isExpanded)
74+ val expanded = self.expanded
6775 val usecases = parseUsecases
6876 }
6977
7078 private [this ] def parseUsecases (implicit ctx : Context ): List [UseCase ] =
7179 if (! isDocComment) {
7280 Nil
7381 } else {
74- tagIndex(raw)
75- .filter { startsWithTag(raw, _, " @usecase" ) }
76- .map { case (start, end) => decomposeUseCase(start, end) }
82+ expanded.map { body =>
83+ tagIndex(body)
84+ .filter { startsWithTag(body, _, " @usecase" ) }
85+ .map { case (start, end) => decomposeUseCase(body, start, end) }
86+ }.getOrElse(Nil )
7787 }
7888
7989 /** Turns a usecase section into a UseCase, with code changed to:
@@ -84,7 +94,7 @@ object Comments {
8494 * def foo: A = ???
8595 * }}}
8696 */
87- private [this ] def decomposeUseCase (start : Int , end : Int )(implicit ctx : Context ): UseCase = {
97+ private [this ] def decomposeUseCase (body : String , start : Int , end : Int )(implicit ctx : Context ): UseCase = {
8898 def subPos (start : Int , end : Int ) =
8999 if (pos == NoPosition ) NoPosition
90100 else {
@@ -93,19 +103,19 @@ object Comments {
93103 pos withStart start1 withPoint start1 withEnd end1
94104 }
95105
96- val codeStart = skipWhitespace(raw , start + " @usecase" .length)
97- val codeEnd = skipToEol(raw , codeStart)
98- val code = raw .substring(codeStart, codeEnd) + " = ???"
99- val codePos = subPos(codeStart, codeEnd)
106+ val codeStart = skipWhitespace(body , start + " @usecase" .length)
107+ val codeEnd = skipToEol(body , codeStart)
108+ val code = body .substring(codeStart, codeEnd) + " = ???"
109+ val codePos = subPos(codeStart, codeEnd)
100110
101111 UseCase (code, codePos)
102112 }
103113 }
104114
105115 object Comment {
106- def apply (pos : Position , raw : String , expanded : Boolean = false , usc : List [UseCase ] = Nil ): Comment =
116+ def apply (pos : Position , raw : String , expandedComment : Option [ String ] = None , usc : List [UseCase ] = Nil ): Comment =
107117 new Comment (pos, raw) {
108- val isExpanded = expanded
118+ val expanded = expandedComment
109119 val usecases = usc
110120 }
111121 }
0 commit comments