@@ -4,7 +4,6 @@ package typer
44
55import core ._
66import Contexts ._ , Symbols ._ , Decorators ._ , Comments ._
7- import util .Positions ._
87import ast .tpd
98
109trait 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}
0 commit comments