Skip to content

Commit b489a0b

Browse files
committed
More refactorings to comment cooking
1 parent 5ba2d88 commit b489a0b

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package core
44

55
import ast.{ untpd, tpd }
6-
import Decorators._, Symbols._, Contexts._, Flags.EmptyFlags
6+
import Decorators._, Symbols._, Contexts._
77
import util.SourceFile
88
import util.Positions._
99
import util.CommentParsing._
@@ -33,7 +33,7 @@ object Comments {
3333
def docstring(sym: Symbol): Option[Comment] = _docstrings.get(sym)
3434

3535
def addDocstring(sym: Symbol, doc: Option[Comment]): Unit =
36-
doc.map(d => _docstrings.update(sym, d))
36+
doc.foreach(d => _docstrings.update(sym, d))
3737
}
3838

3939
/**
@@ -112,29 +112,25 @@ object Comments {
112112
}
113113
}
114114

115-
abstract case class UseCase(code: String, codePos: Position) {
116-
/** Set by typer */
117-
var tpdCode: tpd.DefDef = _
118-
119-
def untpdCode: untpd.Tree
115+
final case class UseCase(code: String, codePos: Position, untpdCode: untpd.Tree, tpdCode: Option[tpd.DefDef]) {
116+
def typed(tpdCode: tpd.DefDef): UseCase = copy(tpdCode = Some(tpdCode))
120117
}
121118

122119
object UseCase {
123-
def apply(code: String, codePos: Position)(implicit ctx: Context) =
124-
new UseCase(code, codePos) {
125-
val untpdCode = {
126-
val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start)
127-
128-
tree match {
129-
case tree: untpd.DefDef =>
130-
val newName = ctx.freshNames.newName(tree.name, NameKinds.DocArtifactName)
131-
untpd.DefDef(newName, tree.tparams, tree.vparamss, tree.tpt, tree.rhs)
132-
case _ =>
133-
ctx.error(ProperDefinitionNotFound(), codePos)
134-
tree
135-
}
120+
def apply(code: String, codePos: Position)(implicit ctx: Context): UseCase = {
121+
val tree = {
122+
val tree = new Parser(new SourceFile("<usecase>", code)).localDef(codePos.start)
123+
tree match {
124+
case tree: untpd.DefDef =>
125+
val newName = ctx.freshNames.newName(tree.name, NameKinds.DocArtifactName)
126+
tree.copy(name = newName)
127+
case _ =>
128+
ctx.error(ProperDefinitionNotFound(), codePos)
129+
tree
136130
}
137131
}
132+
UseCase(code, codePos, tree, None)
133+
}
138134
}
139135

140136
/**

compiler/src/dotty/tools/dotc/typer/Docstrings.scala

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package typer
44

55
import core._
66
import Contexts._, Symbols._, Decorators._, Comments._
7-
import util.Positions._
87
import ast.tpd
98

109
trait Docstrings { self: Typer =>
@@ -21,39 +20,51 @@ trait Docstrings { self: Typer =>
2120
* @param sym The symbol for which the comment is being cooked.
2221
* @param owner The class for which comments are being cooked.
2322
*/
24-
def cookComment(sym: Symbol, owner: Symbol)(implicit ctx: Context): Unit = {
25-
for {
26-
docbase <- ctx.docCtx
27-
comment <- docbase.docstring(sym).filter(!_.isExpanded)
28-
} {
29-
expandParentDocs(sym)
30-
docbase.docstring(sym).get.usecases.foreach { usecase =>
31-
enterSymbol(createSymbol(usecase.untpdCode))
32-
typedStats(usecase.untpdCode :: Nil, owner) match {
33-
case List(df: tpd.DefDef) => usecase.tpdCode = df
34-
case _ => ctx.error("`@usecase` was not a valid definition", usecase.codePos)
35-
}
36-
}
23+
def cookComment(sym: Symbol, owner: Symbol)(implicit ctx: Context): Option[Comment] = {
24+
ctx.docCtx.flatMap { docCtx =>
25+
expand(sym, owner)(ctx, docCtx)
3726
}
3827
}
3928

40-
private def expandParentDocs(sym: Symbol)(implicit ctx: Context): Unit =
41-
ctx.docCtx.foreach { docCtx =>
42-
docCtx.docstring(sym).foreach { cmt =>
43-
def expandDoc(owner: Symbol): Unit = if (!cmt.isExpanded) {
44-
val tplExp = docCtx.templateExpander
45-
tplExp.defineVariables(sym)
46-
47-
val newCmt = cmt
48-
.expand(tplExp.expandedDocComment(sym, owner, _))
29+
private def expand(sym: Symbol, owner: Symbol)(implicit ctx: Context, docCtx: ContextDocstrings): Option[Comment] = {
30+
docCtx.docstring(sym).flatMap {
31+
case cmt if cmt.isExpanded =>
32+
Some(cmt)
33+
case _ =>
34+
expandComment(sym).map { expanded =>
35+
val typedUsecases = expanded.usecases.map { usecase =>
36+
enterSymbol(createSymbol(usecase.untpdCode))
37+
typedStats(usecase.untpdCode :: Nil, owner) match {
38+
case List(df: tpd.DefDef) =>
39+
usecase.typed(df)
40+
case _ =>
41+
ctx.error("`@usecase` was not a valid definition", usecase.codePos)
42+
usecase
43+
}
44+
}
4945

50-
docCtx.addDocstring(sym, Some(newCmt))
46+
val commentWithUsecases = expanded.copy(usecases = typedUsecases)
47+
docCtx.addDocstring(sym, Some(commentWithUsecases))
48+
commentWithUsecases
5149
}
50+
}
51+
}
5252

53-
if (sym ne NoSymbol) {
54-
expandParentDocs(sym.owner)
55-
expandDoc(sym.owner)
56-
}
57-
}
53+
private def expandComment(sym: Symbol, owner: Symbol, comment: Comment)(implicit ctx: Context, docCtx: ContextDocstrings): Comment = {
54+
val tplExp = docCtx.templateExpander
55+
tplExp.defineVariables(sym)
56+
val newComment = comment.expand(tplExp.expandedDocComment(sym, owner, _))
57+
docCtx.addDocstring(sym, Some(newComment))
58+
newComment
59+
}
60+
61+
private def expandComment(sym: Symbol)(implicit ctx: Context, docCtx: ContextDocstrings): Option[Comment] = {
62+
if (sym eq NoSymbol) None
63+
else {
64+
for {
65+
cmt <- docCtx.docstring(sym) if !cmt.isExpanded
66+
_ = expandComment(sym.owner)
67+
} yield expandComment(sym, sym.owner, cmt)
5868
}
69+
}
5970
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class UsecasePhase extends DocMiniPhase {
2929
override def transformDef(implicit ctx: Context) = { case df: DefImpl =>
3030
val defdefs =
3131
ctx.docbase.docstring(df.symbol)
32-
.map(_.usecases.map(_.tpdCode))
32+
.map(_.usecases.flatMap(_.tpdCode))
3333
.getOrElse(Nil)
3434

3535
if (defdefs.isEmpty) df :: Nil

0 commit comments

Comments
 (0)