Skip to content

Commit a02f8ad

Browse files
committed
Small refactorings in Comments
1 parent 79ffffd commit a02f8ad

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

compiler/src/dotty/tools/dotc/core/Comments.scala

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,17 @@ object Comments {
4242
* `@usecase` sections as well as functionality to map the `raw` docstring
4343
*/
4444
abstract case class Comment(pos: Position, raw: String) { self =>
45+
/** Has this comment been cooked or expanded? */
4546
def isExpanded: Boolean
4647

48+
/** The body of this comment, without the `@usecase` and `@define` sections. */
49+
lazy val body: String =
50+
removeSections(raw, "@usecase", "@define")
51+
52+
/**
53+
* The `@usecase` sections of this comment.
54+
* This is populated by calling `withUsecases` on this object.
55+
*/
4756
def usecases: List[UseCase]
4857

4958
val isDocComment = raw.startsWith("/**")
@@ -53,21 +62,19 @@ object Comments {
5362
val usecases = self.usecases
5463
}
5564

56-
def withUsecases(implicit ctx: Context): Comment = new Comment(pos, stripUsecases) {
65+
def withUsecases(implicit ctx: Context): Comment = new Comment(pos, raw) {
5766
val isExpanded = self.isExpanded
5867
val usecases = parseUsecases
5968
}
6069

61-
private[this] lazy val stripUsecases: String =
62-
removeSections(raw, "@usecase", "@define")
63-
6470
private[this] def parseUsecases(implicit ctx: Context): List[UseCase] =
65-
if (!raw.startsWith("/**"))
66-
List.empty[UseCase]
67-
else
71+
if (!isDocComment) {
72+
Nil
73+
} else {
6874
tagIndex(raw)
69-
.filter { startsWithTag(raw, _, "@usecase") }
70-
.map { case (start, end) => decomposeUseCase(start, end) }
75+
.filter { startsWithTag(raw, _, "@usecase") }
76+
.map { case (start, end) => decomposeUseCase(start, end) }
77+
}
7178

7279
/** Turns a usecase section into a UseCase, with code changed to:
7380
* {{{

doc-tool/src/dotty/tools/dottydoc/core/DocstringPhase.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class DocstringPhase extends DocMiniPhase with CommentParser with CommentCleaner
2929

3030
private def parsedComment(ent: Entity)(implicit ctx: Context): Option[Comment] =
3131
getComment(ent.symbol).map { cmt =>
32-
val parsed = parse(ent, ctx.docbase.packages, clean(cmt.raw), cmt.raw, cmt.pos)
32+
val text = cmt.body
33+
val parsed = parse(ent, ctx.docbase.packages, clean(text), text, cmt.pos)
3334

3435
if (ctx.settings.wikiSyntax.value)
3536
WikiComment(ent, parsed, cmt.pos).comment

doc-tool/test/UsecaseTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ abstract class UsecaseBase extends DottyDocTest {
228228
val List(map: Def) = trt.members
229229
assert(map.comment.isDefined, "Lost comment in transformations")
230230

231-
val docstr = ctx.docbase.docstring(map.symbol).get.raw
231+
val docstr = ctx.docbase.docstring(map.symbol).get.body
232232
assert(
233233
!docstr.contains("@usecase"),
234234
s"Comment should not contain usecase after stripping, but was:\n$docstr"

0 commit comments

Comments
 (0)