Skip to content

Commit 5e37d73

Browse files
committed
More refactorings to comment cooking
1 parent 1b5fa87 commit 5e37d73

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
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: 32 additions & 26 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,46 @@ 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 = {
23+
def cookComment(sym: Symbol, owner: Symbol)(implicit ctx: Context): Option[Comment] = {
2524
for {
26-
docbase <- ctx.docCtx
27-
comment <- docbase.docstring(sym).filter(!_.isExpanded)
28-
} {
29-
expandParentDocs(sym)
30-
docbase.docstring(sym).get.usecases.foreach { usecase =>
25+
docCtx <- ctx.docCtx
26+
comment <- expandParentDocs(sym)(ctx, docCtx)
27+
} yield {
28+
val typedUsecases = comment.usecases.map { usecase =>
3129
enterSymbol(createSymbol(usecase.untpdCode))
3230
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)
31+
case List(df: tpd.DefDef) =>
32+
usecase.typed(df)
33+
case _ =>
34+
ctx.error("`@usecase` was not a valid definition", usecase.codePos)
35+
usecase
3536
}
3637
}
38+
val newComment = comment.copy(usecases = typedUsecases)
39+
docCtx.addDocstring(sym, Some(newComment))
40+
newComment
3741
}
3842
}
3943

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, _))
49-
50-
docCtx.addDocstring(sym, Some(newCmt))
51-
}
44+
private def expandDoc(sym: Symbol, owner: Symbol, comment: Comment)(implicit ctx: Context, docCtx: ContextDocstrings): Comment = {
45+
if (!comment.isExpanded) {
46+
val tplExp = docCtx.templateExpander
47+
tplExp.defineVariables(sym)
48+
val newComment = comment.expand(tplExp.expandedDocComment(sym, owner, _))
49+
docCtx.addDocstring(sym, Some(newComment))
50+
newComment
51+
} else {
52+
comment
53+
}
54+
}
5255

53-
if (sym ne NoSymbol) {
54-
expandParentDocs(sym.owner)
55-
expandDoc(sym.owner)
56-
}
57-
}
56+
private def expandParentDocs(sym: Symbol)(implicit ctx: Context, docCtx: ContextDocstrings): Option[Comment] = {
57+
if (sym eq NoSymbol) None
58+
else {
59+
for {
60+
cmt <- docCtx.docstring(sym) if !cmt.isExpanded
61+
_ = expandParentDocs(sym.owner)
62+
} yield expandDoc(sym, sym.owner, cmt)
5863
}
64+
}
5965
}

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)