Skip to content

Commit 86c9209

Browse files
committed
Remove Context reference in Comment and UseCase
1 parent a51b55f commit 86c9209

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import util.Property.Key
1111
import parsing.Parsers.Parser
1212

1313
object Comments {
14-
val ContextDoc = new Key[ContextDocstrings]
14+
val ContextDoc = new Key[ContextDocstrings]
1515

1616
/** Decorator for getting docbase out of context */
1717
implicit class CommentsContext(val ctx: Context) extends AnyVal {
@@ -37,7 +37,12 @@ object Comments {
3737
doc.map(d => _docstrings += (sym -> d))
3838
}
3939

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 =>
4146
def isExpanded: Boolean
4247

4348
def usecases: List[UseCase]
@@ -49,15 +54,15 @@ object Comments {
4954
val usecases = self.usecases
5055
}
5156

52-
def withUsecases: Comment = new Comment(pos, stripUsecases) {
57+
def withUsecases(implicit ctx: Context): Comment = new Comment(pos, stripUsecases) {
5358
val isExpanded = self.isExpanded
5459
val usecases = parseUsecases
5560
}
5661

5762
private[this] lazy val stripUsecases: String =
5863
removeSections(raw, "@usecase", "@define")
5964

60-
private[this] lazy val parseUsecases: List[UseCase] =
65+
private[this] def parseUsecases(implicit ctx: Context): List[UseCase] =
6166
if (!raw.startsWith("/**"))
6267
List.empty[UseCase]
6368
else
@@ -73,7 +78,7 @@ object Comments {
7378
* def foo: A = ???
7479
* }}}
7580
*/
76-
private[this] def decomposeUseCase(start: Int, end: Int): UseCase = {
81+
private[this] def decomposeUseCase(start: Int, end: Int)(implicit ctx: Context): UseCase = {
7782
def subPos(start: Int, end: Int) =
7883
if (pos == NoPosition) NoPosition
7984
else {
@@ -102,22 +107,29 @@ object Comments {
102107
}
103108
}
104109

105-
case class UseCase(comment: Comment, code: String, codePos: Position)(implicit ctx: Context) {
110+
abstract case class UseCase(comment: Comment, code: String, codePos: Position) {
106111
/** Set by typer */
107112
var tpdCode: tpd.DefDef = _
108113

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+
}
111116

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+
}
119132
}
120-
}
121133
}
122134

123135
/**

0 commit comments

Comments
 (0)