@@ -36,54 +36,54 @@ object Comments {
3636 doc.map(d => _docstrings.update(sym, d))
3737 }
3838
39- /** A `Comment` contains the unformatted docstring as well as a position
40- *
41- * The `Comment` contains functionality to create versions of itself without
42- * `@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.
46- */
47- abstract case class Comment (pos : Position , raw : String ) { self =>
48-
49- /** The expansion of this comment */
50- def expanded : Option [String ]
39+ /**
40+ * A `Comment` contains the unformatted docstring, it's position and potentially more
41+ * information that is populated when the comment is "cooked".
42+ *
43+ * @param pos The position of this `Comment`.
44+ * @param raw The raw comment, as seen in the source code, without any expansion.
45+ * @param expanded If this comment has been expanded, it's expansion, otherwise `None`.
46+ * @param usecases The usecases for this comment.
47+ */
48+ final case class Comment (pos : Position , raw : String , expanded : Option [String ], usecases : List [UseCase ]) {
5149
5250 /** Has this comment been cooked or expanded? */
53- final def isExpanded : Boolean = expanded.isDefined
51+ def isExpanded : Boolean = expanded.isDefined
5452
5553 /** The body of this comment, without the `@usecase ` and `@define ` sections, after expansion. */
5654 lazy val expandedBody : Option [String ] =
5755 expanded.map(removeSections(_, " @usecase" , " @define" ))
5856
57+ val isDocComment = Comment .isDocComment(raw)
58+
5959 /**
60- * The `@usecase ` sections of this comment.
61- * This is populated by calling `withUsecases` on this object.
60+ * Expands this comment by giving its content to `f`, and then parsing the `@usecase ` sections.
61+ * Typically, `f` will take care of expanding the variables.
62+ *
63+ * @param f The expansion function.
64+ * @return The expanded comment, with the `usecases` populated.
6265 */
63- def usecases : List [UseCase ]
66+ def expand (f : String => String )(implicit ctx : Context ): Comment = {
67+ val expandedComment = f(raw)
68+ val useCases = Comment .parseUsecases(expandedComment, pos)
69+ Comment (pos, raw, Some (expandedComment), useCases)
70+ }
71+ }
6472
65- val isDocComment = raw.startsWith( " /** " )
73+ object Comment {
6674
67- def expand (f : String => String ): Comment = new Comment (pos, raw) {
68- val expanded = Some (f(raw))
69- val usecases = self.usecases
70- }
75+ def isDocComment (comment : String ): Boolean = comment.startsWith(" /**" )
7176
72- def withUsecases (implicit ctx : Context ): Comment = new Comment (pos, raw) {
73- assert(self.isExpanded)
74- val expanded = self.expanded
75- val usecases = parseUsecases
76- }
77+ def apply (pos : Position , raw : String ): Comment =
78+ Comment (pos, raw, None , Nil )
7779
78- private [ this ] def parseUsecases (implicit ctx : Context ): List [UseCase ] =
79- if (! isDocComment) {
80+ private def parseUsecases ( expandedComment : String , pos : Position ) (implicit ctx : Context ): List [UseCase ] =
81+ if (! isDocComment(expandedComment) ) {
8082 Nil
8183 } else {
82- expanded.map { body =>
83- tagIndex(body)
84- .filter { startsWithTag(body, _, " @usecase" ) }
85- .map { case (start, end) => decomposeUseCase(body, start, end) }
86- }.getOrElse(Nil )
84+ tagIndex(expandedComment)
85+ .filter { startsWithTag(expandedComment, _, " @usecase" ) }
86+ .map { case (start, end) => decomposeUseCase(expandedComment, pos, start, end) }
8787 }
8888
8989 /** Turns a usecase section into a UseCase, with code changed to:
@@ -94,7 +94,7 @@ object Comments {
9494 * def foo: A = ???
9595 * }}}
9696 */
97- private [this ] def decomposeUseCase (body : String , start : Int , end : Int )(implicit ctx : Context ): UseCase = {
97+ private [this ] def decomposeUseCase (body : String , pos : Position , start : Int , end : Int )(implicit ctx : Context ): UseCase = {
9898 def subPos (start : Int , end : Int ) =
9999 if (pos == NoPosition ) NoPosition
100100 else {
@@ -112,14 +112,6 @@ object Comments {
112112 }
113113 }
114114
115- object Comment {
116- def apply (pos : Position , raw : String , expandedComment : Option [String ] = None , usc : List [UseCase ] = Nil ): Comment =
117- new Comment (pos, raw) {
118- val expanded = expandedComment
119- val usecases = usc
120- }
121- }
122-
123115 abstract case class UseCase (code : String , codePos : Position ) {
124116 /** Set by typer */
125117 var tpdCode : tpd.DefDef = _
0 commit comments