@@ -36,54 +36,54 @@ object Comments {
36
36
doc.map(d => _docstrings.update(sym, d))
37
37
}
38
38
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 ]) {
51
49
52
50
/** Has this comment been cooked or expanded? */
53
- final def isExpanded : Boolean = expanded.isDefined
51
+ def isExpanded : Boolean = expanded.isDefined
54
52
55
53
/** The body of this comment, without the `@usecase ` and `@define ` sections, after expansion. */
56
54
lazy val expandedBody : Option [String ] =
57
55
expanded.map(removeSections(_, " @usecase" , " @define" ))
58
56
57
+ val isDocComment = Comment .isDocComment(raw)
58
+
59
59
/**
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.
62
65
*/
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
+ }
64
72
65
- val isDocComment = raw.startsWith( " /** " )
73
+ object Comment {
66
74
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(" /**" )
71
76
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 )
77
79
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) ) {
80
82
Nil
81
83
} 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) }
87
87
}
88
88
89
89
/** Turns a usecase section into a UseCase, with code changed to:
@@ -94,7 +94,7 @@ object Comments {
94
94
* def foo: A = ???
95
95
* }}}
96
96
*/
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 = {
98
98
def subPos (start : Int , end : Int ) =
99
99
if (pos == NoPosition ) NoPosition
100
100
else {
@@ -112,14 +112,6 @@ object Comments {
112
112
}
113
113
}
114
114
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
-
123
115
abstract case class UseCase (code : String , codePos : Position ) {
124
116
/** Set by typer */
125
117
var tpdCode : tpd.DefDef = _
0 commit comments