aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dottydoc/src/dotty/tools/dottydoc/core/DocstringPhase.scala4
-rw-r--r--dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala20
-rw-r--r--dottydoc/src/dotty/tools/dottydoc/model/comment/CommentExpander.scala344
-rw-r--r--dottydoc/test/UsecaseTest.scala37
-rw-r--r--src/dotty/tools/dotc/core/Comments.scala374
-rw-r--r--src/dotty/tools/dotc/core/Contexts.scala2
-rw-r--r--src/dotty/tools/dotc/typer/Typer.scala17
-rw-r--r--src/dotty/tools/dotc/util/CommentParsing.scala (renamed from dottydoc/src/dotty/tools/dottydoc/model/comment/CommentUtils.scala)24
8 files changed, 437 insertions, 385 deletions
diff --git a/dottydoc/src/dotty/tools/dottydoc/core/DocstringPhase.scala b/dottydoc/src/dotty/tools/dottydoc/core/DocstringPhase.scala
index 830336ba1..93d51503f 100644
--- a/dottydoc/src/dotty/tools/dottydoc/core/DocstringPhase.scala
+++ b/dottydoc/src/dotty/tools/dottydoc/core/DocstringPhase.scala
@@ -3,14 +3,10 @@ package dottydoc
package core
import dotc.core.Contexts.Context
-import dotc.ast.tpd
-
import transform.DocMiniPhase
import model._
import model.internal._
-import model.factories._
import model.comment._
-import dotty.tools.dotc.core.Symbols.Symbol
import BodyParsers._
class DocstringPhase extends DocMiniPhase with CommentParser with CommentCleaner {
diff --git a/dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala b/dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala
index 4d9c0abbd..8e9e1fd57 100644
--- a/dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala
+++ b/dottydoc/src/dotty/tools/dottydoc/core/UsecasePhase.scala
@@ -11,14 +11,18 @@ import model.factories._
import dotty.tools.dotc.core.Symbols.Symbol
class UsecasePhase extends DocMiniPhase {
- private def defdefToDef(d: tpd.DefDef, sym: Symbol)(implicit ctx: Context) = DefImpl(
- sym,
- d.name.show.split("\\$").head, // UseCase defs get $pos appended to their names
- flags(d), path(d.symbol),
- returnType(d.tpt.tpe),
- typeParams(d.symbol),
- paramLists(d.symbol.info)
- )
+ private def defdefToDef(d: tpd.DefDef, sym: Symbol)(implicit ctx: Context) = {
+ val name = d.name.show.split("\\$").head // UseCase defs get $pos appended to their names
+ DefImpl(
+ sym,
+ name,
+ flags(d),
+ path(d.symbol).init :+ name,
+ returnType(d.tpt.tpe),
+ typeParams(d.symbol),
+ paramLists(d.symbol.info)
+ )
+ }
override def transformDef(implicit ctx: Context) = { case df: DefImpl =>
ctx.docbase.docstring(df.symbol).flatMap(_.usecases.headOption.map(_.tpdCode)).map(defdefToDef(_, df.symbol)).getOrElse(df)
diff --git a/dottydoc/src/dotty/tools/dottydoc/model/comment/CommentExpander.scala b/dottydoc/src/dotty/tools/dottydoc/model/comment/CommentExpander.scala
deleted file mode 100644
index ece0d1018..000000000
--- a/dottydoc/src/dotty/tools/dottydoc/model/comment/CommentExpander.scala
+++ /dev/null
@@ -1,344 +0,0 @@
-/*
- * Port of DocComment.scala from nsc
- * @author Martin Odersky
- * @author Felix Mulder
- */
-
-package dotty.tools
-package dottydoc
-package model
-package comment
-
-import dotc.config.Printers.dottydoc
-import dotc.core.Contexts.Context
-import dotc.core.Symbols._
-import dotc.core.Flags
-import dotc.util.Positions._
-
-import scala.collection.mutable
-
-trait CommentExpander {
- import CommentUtils._
-
- def expand(sym: Symbol, site: Symbol)(implicit ctx: Context): String = {
- val parent = if (site != NoSymbol) site else sym
- defineVariables(parent)
- expandedDocComment(sym, parent)
- }
-
- /** The cooked doc comment of symbol `sym` after variable expansion, or "" if missing.
- *
- * @param sym The symbol for which doc comment is returned
- * @param site The class for which doc comments are generated
- * @throws ExpansionLimitExceeded when more than 10 successive expansions
- * of the same string are done, which is
- * interpreted as a recursive variable definition.
- */
- def expandedDocComment(sym: Symbol, site: Symbol, docStr: String = "")(implicit ctx: Context): String = {
- // when parsing a top level class or module, use the (module-)class itself to look up variable definitions
- val parent = if ((sym.is(Flags.Module) || sym.isClass) && site.is(Flags.Package)) sym
- else site
- expandVariables(cookedDocComment(sym, docStr), sym, parent)
- }
-
- private def template(raw: String): String = {
- val sections = tagIndex(raw)
-
- val defines = sections filter { startsWithTag(raw, _, "@define") }
- val usecases = sections filter { startsWithTag(raw, _, "@usecase") }
-
- val end = startTag(raw, (defines /*::: usecases*/).sortBy(_._1))
-
- if (end == raw.length - 2) raw else raw.substring(0, end) + "*/"
- }
-
- def defines(raw: String): List[String] = {
- val sections = tagIndex(raw)
- val defines = sections filter { startsWithTag(raw, _, "@define") }
- val usecases = sections filter { startsWithTag(raw, _, "@usecase") }
- val end = startTag(raw, (defines ::: usecases).sortBy(_._1))
-
- defines map { case (start, end) => raw.substring(start, end) }
- }
-
- private def replaceInheritDocToInheritdoc(docStr: String): String =
- docStr.replaceAll("""\{@inheritDoc\p{Zs}*\}""", "@inheritdoc")
-
- /** The cooked doc comment of an overridden symbol */
- protected def superComment(sym: Symbol)(implicit ctx: Context): Option[String] =
- allInheritedOverriddenSymbols(sym).iterator map (x => cookedDocComment(x)) find (_ != "")
-
- private val cookedDocComments = mutable.HashMap[Symbol, String]()
-
- /** The raw doc comment of symbol `sym`, minus usecase and define sections, augmented by
- * missing sections of an inherited doc comment.
- * If a symbol does not have a doc comment but some overridden version of it does,
- * the doc comment of the overridden version is copied instead.
- */
- def cookedDocComment(sym: Symbol, docStr: String = "")(implicit ctx: Context): String = cookedDocComments.getOrElseUpdate(sym, {
- var ownComment =
- if (docStr.length == 0) ctx.docbase.docstring(sym).map(c => template(c.raw)).getOrElse("")
- else template(docStr)
- ownComment = replaceInheritDocToInheritdoc(ownComment)
-
- superComment(sym) match {
- case None =>
- // SI-8210 - The warning would be false negative when this symbol is a setter
- if (ownComment.indexOf("@inheritdoc") != -1 && ! sym.isSetter)
- dottydoc.println(s"${sym.pos}: the comment for ${sym} contains @inheritdoc, but no parent comment is available to inherit from.")
- ownComment.replaceAllLiterally("@inheritdoc", "<invalid inheritdoc annotation>")
- case Some(sc) =>
- if (ownComment == "") sc
- else expandInheritdoc(sc, merge(sc, ownComment, sym), sym)
- }
- })
-
- private def isMovable(str: String, sec: (Int, Int)): Boolean =
- startsWithTag(str, sec, "@param") ||
- startsWithTag(str, sec, "@tparam") ||
- startsWithTag(str, sec, "@return")
-
- def merge(src: String, dst: String, sym: Symbol, copyFirstPara: Boolean = false): String = {
- val srcSections = tagIndex(src)
- val dstSections = tagIndex(dst)
- val srcParams = paramDocs(src, "@param", srcSections)
- val dstParams = paramDocs(dst, "@param", dstSections)
- val srcTParams = paramDocs(src, "@tparam", srcSections)
- val dstTParams = paramDocs(dst, "@tparam", dstSections)
- val out = new StringBuilder
- var copied = 0
- var tocopy = startTag(dst, dstSections dropWhile (!isMovable(dst, _)))
-
- if (copyFirstPara) {
- val eop = // end of comment body (first para), which is delimited by blank line, or tag, or end of comment
- (findNext(src, 0)(src.charAt(_) == '\n')) min startTag(src, srcSections)
- out append src.substring(0, eop).trim
- copied = 3
- tocopy = 3
- }
-
- def mergeSection(srcSec: Option[(Int, Int)], dstSec: Option[(Int, Int)]) = dstSec match {
- case Some((start, end)) =>
- if (end > tocopy) tocopy = end
- case None =>
- srcSec match {
- case Some((start1, end1)) => {
- out append dst.substring(copied, tocopy).trim
- out append "\n"
- copied = tocopy
- out append src.substring(start1, end1).trim
- }
- case None =>
- }
- }
-
- //TODO: enable this once you know how to get `sym.paramss`
- /*
- for (params <- sym.paramss; param <- params)
- mergeSection(srcParams get param.name.toString, dstParams get param.name.toString)
- for (tparam <- sym.typeParams)
- mergeSection(srcTParams get tparam.name.toString, dstTParams get tparam.name.toString)
-
- mergeSection(returnDoc(src, srcSections), returnDoc(dst, dstSections))
- mergeSection(groupDoc(src, srcSections), groupDoc(dst, dstSections))
- */
-
- if (out.length == 0) dst
- else {
- out append dst.substring(copied)
- out.toString
- }
- }
-
- /**
- * Expand inheritdoc tags
- * - for the main comment we transform the inheritdoc into the super variable,
- * and the variable expansion can expand it further
- * - for the param, tparam and throws sections we must replace comments on the spot
- *
- * This is done separately, for two reasons:
- * 1. It takes longer to run compared to merge
- * 2. The inheritdoc annotation should not be used very often, as building the comment from pieces severely
- * impacts performance
- *
- * @param parent The source (or parent) comment
- * @param child The child (overriding member or usecase) comment
- * @param sym The child symbol
- * @return The child comment with the inheritdoc sections expanded
- */
- def expandInheritdoc(parent: String, child: String, sym: Symbol): String =
- if (child.indexOf("@inheritdoc") == -1)
- child
- else {
- val parentSections = tagIndex(parent)
- val childSections = tagIndex(child)
- val parentTagMap = sectionTagMap(parent, parentSections)
- val parentNamedParams = Map() +
- ("@param" -> paramDocs(parent, "@param", parentSections)) +
- ("@tparam" -> paramDocs(parent, "@tparam", parentSections)) +
- ("@throws" -> paramDocs(parent, "@throws", parentSections))
-
- val out = new StringBuilder
-
- def replaceInheritdoc(childSection: String, parentSection: => String) =
- if (childSection.indexOf("@inheritdoc") == -1)
- childSection
- else
- childSection.replaceAllLiterally("@inheritdoc", parentSection)
-
- def getParentSection(section: (Int, Int)): String = {
-
- def getSectionHeader = extractSectionTag(child, section) match {
- case param@("@param"|"@tparam"|"@throws") => param + " " + extractSectionParam(child, section)
- case other => other
- }
-
- def sectionString(param: String, paramMap: Map[String, (Int, Int)]): String =
- paramMap.get(param) match {
- case Some(section) =>
- // Cleanup the section tag and parameter
- val sectionTextBounds = extractSectionText(parent, section)
- cleanupSectionText(parent.substring(sectionTextBounds._1, sectionTextBounds._2))
- case None =>
- dottydoc.println(s"""${sym.pos}: the """" + getSectionHeader + "\" annotation of the " + sym +
- " comment contains @inheritdoc, but the corresponding section in the parent is not defined.")
- "<invalid inheritdoc annotation>"
- }
-
- child.substring(section._1, section._1 + 7) match {
- case param@("@param "|"@tparam"|"@throws") =>
- sectionString(extractSectionParam(child, section), parentNamedParams(param.trim))
- case _ =>
- sectionString(extractSectionTag(child, section), parentTagMap)
- }
- }
-
- def mainComment(str: String, sections: List[(Int, Int)]): String =
- if (str.trim.length > 3)
- str.trim.substring(3, startTag(str, sections))
- else
- ""
-
- // Append main comment
- out.append("/**")
- out.append(replaceInheritdoc(mainComment(child, childSections), mainComment(parent, parentSections)))
-
- // Append sections
- for (section <- childSections)
- out.append(replaceInheritdoc(child.substring(section._1, section._2), getParentSection(section)))
-
- out.append("*/")
- out.toString
- }
-
- protected def expandVariables(initialStr: String, sym: Symbol, site: Symbol)(implicit ctx: Context): String = {
- val expandLimit = 10
-
- def expandInternal(str: String, depth: Int): String = {
- if (depth >= expandLimit)
- throw new ExpansionLimitExceeded(str)
-
- val out = new StringBuilder
- var copied, idx = 0
- // excluding variables written as \$foo so we can use them when
- // necessary to document things like Symbol#decode
- def isEscaped = idx > 0 && str.charAt(idx - 1) == '\\'
- while (idx < str.length) {
- if ((str charAt idx) != '$' || isEscaped)
- idx += 1
- else {
- val vstart = idx
- idx = skipVariable(str, idx + 1)
- def replaceWith(repl: String) {
- out append str.substring(copied, vstart)
- out append repl
- copied = idx
- }
- variableName(str.substring(vstart + 1, idx)) match {
- case "super" =>
- superComment(sym) foreach { sc =>
- val superSections = tagIndex(sc)
- replaceWith(sc.substring(3, startTag(sc, superSections)))
- for (sec @ (start, end) <- superSections)
- if (!isMovable(sc, sec)) out append sc.substring(start, end)
- }
- case "" => idx += 1
- case vname =>
- lookupVariable(vname, site) match {
- case Some(replacement) => replaceWith(replacement)
- case None =>
- dottydoc.println(s"Variable $vname undefined in comment for $sym in $site")
- }
- }
- }
- }
- if (out.length == 0) str
- else {
- out append str.substring(copied)
- expandInternal(out.toString, depth + 1)
- }
- }
-
- // We suppressed expanding \$ throughout the recursion, and now we
- // need to replace \$ with $ so it looks as intended.
- expandInternal(initialStr, 0).replaceAllLiterally("""\$""", "$")
- }
-
- def defineVariables(sym: Symbol)(implicit ctx: Context) = {
- val Trim = "(?s)^[\\s&&[^\n\r]]*(.*?)\\s*$".r
-
- val raw = ctx.docbase.docstring(sym).map(_.raw).getOrElse("")
- defs(sym) ++= defines(raw).map {
- str => {
- val start = skipWhitespace(str, "@define".length)
- val (key, value) = str.splitAt(skipVariable(str, start))
- key.drop(start) -> value
- }
- } map {
- case (key, Trim(value)) =>
- variableName(key) -> value.replaceAll("\\s+\\*+$", "")
- }
- }
-
- /** Maps symbols to the variable -> replacement maps that are defined
- * in their doc comments
- */
- private val defs = mutable.HashMap[Symbol, Map[String, String]]() withDefaultValue Map()
-
- /** Lookup definition of variable.
- *
- * @param vble The variable for which a definition is searched
- * @param site The class for which doc comments are generated
- */
- def lookupVariable(vble: String, site: Symbol)(implicit ctx: Context): Option[String] = site match {
- case NoSymbol => None
- case _ =>
- val searchList =
- if (site.flags.is(Flags.Module)) site :: site.info.baseClasses
- else site.info.baseClasses
-
- searchList collectFirst { case x if defs(x) contains vble => defs(x)(vble) } match {
- case Some(str) if str startsWith "$" => lookupVariable(str.tail, site)
- case res => res orElse lookupVariable(vble, site.owner)
- }
- }
-
- /** The position of the raw doc comment of symbol `sym`, or NoPosition if missing
- * If a symbol does not have a doc comment but some overridden version of it does,
- * the position of the doc comment of the overridden version is returned instead.
- */
- def docCommentPos(sym: Symbol)(implicit ctx: Context): Position =
- ctx.docbase.docstring(sym).map(_.pos).getOrElse(NoPosition)
-
- /** A version which doesn't consider self types, as a temporary measure:
- * an infinite loop has broken out between superComment and cookedDocComment
- * since r23926.
- */
- private def allInheritedOverriddenSymbols(sym: Symbol)(implicit ctx: Context): List[Symbol] = {
- if (!sym.owner.isClass) Nil
- else sym.allOverriddenSymbols.toList.filter(_ != NoSymbol) //TODO: could also be `sym.owner.allOverrid..`
- //else sym.owner.ancestors map (sym overriddenSymbol _) filter (_ != NoSymbol)
- }
-
- class ExpansionLimitExceeded(str: String) extends Exception
-}
diff --git a/dottydoc/test/UsecaseTest.scala b/dottydoc/test/UsecaseTest.scala
index 05bc8663f..c7b398d1f 100644
--- a/dottydoc/test/UsecaseTest.scala
+++ b/dottydoc/test/UsecaseTest.scala
@@ -31,6 +31,8 @@ class UsecaseTest extends DottyTest {
case PackageImpl(_, _, List(trt: Trait), _, _) =>
val List(foo: Def) = trt.members
+ assert(foo.comment.isDefined, "Lost comment in transformations")
+
val returnValue = foo.returnValue match {
case ref: TypeReference => ref.title
case _ =>
@@ -184,6 +186,41 @@ class UsecaseTest extends DottyTest {
}
}
+ @Test def checkStripping = {
+ val source = new SourceFile(
+ "CheckStripping.scala",
+ """
+ |package scala
+ |
+ |/** The trait $Coll
+ | *
+ | * @define Coll Iterable
+ | */
+ |trait Iterable[A] {
+ | /** Definition with a "disturbing" signature
+ | *
+ | * @usecase def map[B](f: A => B): $Coll[B]
+ | */
+ | def map[B, M[B]](f: A => B): M[B] = ???
+ |}
+ """.stripMargin
+ )
+
+ checkSources(source :: Nil) { packages =>
+ packages("scala") match {
+ case PackageImpl(_, _, List(trt: Trait), _, _) =>
+ val List(map: Def) = trt.members
+ assert(map.comment.isDefined, "Lost comment in transformations")
+
+ val docstr = ctx.docbase.docstring(map.symbol).get.raw
+ assert(
+ !docstr.contains("@usecase"),
+ s"Comment should not contain usecase after stripping, but was:\n$docstr"
+ )
+ }
+ }
+ }
+
@Test def checkIterator =
checkFiles("./scala-scala/src/library/scala/collection/Iterator.scala" :: Nil) { _ =>
// success if typer throws no errors! :)
diff --git a/src/dotty/tools/dotc/core/Comments.scala b/src/dotty/tools/dotc/core/Comments.scala
index ea5726fa0..3e562baff 100644
--- a/src/dotty/tools/dotc/core/Comments.scala
+++ b/src/dotty/tools/dotc/core/Comments.scala
@@ -9,24 +9,38 @@ import Contexts.Context
import Flags.EmptyFlags
import dotc.util.SourceFile
import dotc.util.Positions._
+import dotc.util.CommentParsing._
import dotc.parsing.Parsers.Parser
-import dotty.tools.dottydoc.model.comment.CommentUtils._
object Comments {
- case class Comment(pos: Position, raw: String)(implicit ctx: Context) {
+ abstract case class Comment(pos: Position, raw: String)(implicit ctx: Context) { self =>
+ def isExpanded: Boolean
+
+ def usecases: List[UseCase]
val isDocComment = raw.startsWith("/**")
- private[this] lazy val sections = tagIndex(raw)
+ def expand(f: String => String): Comment = new Comment(pos, f(raw)) {
+ val isExpanded = true
+ val usecases = self.usecases
+ }
- private def fold[A](z: A)(op: => A) = if (!isDocComment) z else op
+ def withUsecases: Comment = new Comment(pos, stripUsecases) {
+ val isExpanded = self.isExpanded
+ val usecases = parseUsecases
+ }
- lazy val usecases = fold(List.empty[UseCase]) {
- sections
+ private[this] lazy val stripUsecases: String =
+ removeSections(raw, "@usecase", "@define")
+
+ private[this] lazy val parseUsecases: List[UseCase] =
+ if (!raw.startsWith("/**"))
+ List.empty[UseCase]
+ else
+ tagIndex(raw)
.filter { startsWithTag(raw, _, "@usecase") }
.map { case (start, end) => decomposeUseCase(start, end) }
- }
/** Turns a usecase section into a UseCase, with code changed to:
* {{{
@@ -36,7 +50,15 @@ object Comments {
* def foo: A = ???
* }}}
*/
- private def decomposeUseCase(start: Int, end: Int): UseCase = {
+ private[this] def decomposeUseCase(start: Int, end: Int): UseCase = {
+ def subPos(start: Int, end: Int) =
+ if (pos == NoPosition) NoPosition
+ else {
+ val start1 = pos.start + start
+ val end1 = pos.end + end
+ pos withStart start1 withPoint start1 withEnd end1
+ }
+
val codeStart = skipWhitespace(raw, start + "@usecase".length)
val codeEnd = skipToEol(raw, codeStart)
val code = raw.substring(codeStart, codeEnd) + " = ???"
@@ -47,13 +69,13 @@ object Comments {
UseCase(Comment(commentPos, commentStr), code, codePos)
}
+ }
- private def subPos(start: Int, end: Int) =
- if (pos == NoPosition) NoPosition
- else {
- val start1 = pos.start + start
- val end1 = pos.end + end
- pos withStart start1 withPoint start1 withEnd end1
+ object Comment {
+ def apply(pos: Position, raw: String, expanded: Boolean = false, usc: List[UseCase] = Nil)(implicit ctx: Context): Comment =
+ new Comment(pos, raw) {
+ val isExpanded = expanded
+ val usecases = usc
}
}
@@ -74,4 +96,328 @@ object Comments {
}
}
}
+
+ /**
+ * Port of DocComment.scala from nsc
+ * @author Martin Odersky
+ * @author Felix Mulder
+ */
+ class CommentExpander {
+ import dotc.config.Printers.dottydoc
+ import scala.collection.mutable
+
+ def expand(sym: Symbol, site: Symbol)(implicit ctx: Context): String = {
+ val parent = if (site != NoSymbol) site else sym
+ defineVariables(parent)
+ expandedDocComment(sym, parent)
+ }
+
+ /** The cooked doc comment of symbol `sym` after variable expansion, or "" if missing.
+ *
+ * @param sym The symbol for which doc comment is returned
+ * @param site The class for which doc comments are generated
+ * @throws ExpansionLimitExceeded when more than 10 successive expansions
+ * of the same string are done, which is
+ * interpreted as a recursive variable definition.
+ */
+ def expandedDocComment(sym: Symbol, site: Symbol, docStr: String = "")(implicit ctx: Context): String = {
+ // when parsing a top level class or module, use the (module-)class itself to look up variable definitions
+ val parent = if ((sym.is(Flags.Module) || sym.isClass) && site.is(Flags.Package)) sym
+ else site
+ expandVariables(cookedDocComment(sym, docStr), sym, parent)
+ }
+
+ private def template(raw: String): String =
+ removeSections(raw, "@define")
+
+ private def defines(raw: String): List[String] = {
+ val sections = tagIndex(raw)
+ val defines = sections filter { startsWithTag(raw, _, "@define") }
+ val usecases = sections filter { startsWithTag(raw, _, "@usecase") }
+ val end = startTag(raw, (defines ::: usecases).sortBy(_._1))
+
+ defines map { case (start, end) => raw.substring(start, end) }
+ }
+
+ private def replaceInheritDocToInheritdoc(docStr: String): String =
+ docStr.replaceAll("""\{@inheritDoc\p{Zs}*\}""", "@inheritdoc")
+
+ /** The cooked doc comment of an overridden symbol */
+ protected def superComment(sym: Symbol)(implicit ctx: Context): Option[String] =
+ allInheritedOverriddenSymbols(sym).iterator map (x => cookedDocComment(x)) find (_ != "")
+
+ private val cookedDocComments = mutable.HashMap[Symbol, String]()
+
+ /** The raw doc comment of symbol `sym`, minus usecase and define sections, augmented by
+ * missing sections of an inherited doc comment.
+ * If a symbol does not have a doc comment but some overridden version of it does,
+ * the doc comment of the overridden version is copied instead.
+ */
+ def cookedDocComment(sym: Symbol, docStr: String = "")(implicit ctx: Context): String = cookedDocComments.getOrElseUpdate(sym, {
+ var ownComment =
+ if (docStr.length == 0) ctx.docbase.docstring(sym).map(c => template(c.raw)).getOrElse("")
+ else template(docStr)
+ ownComment = replaceInheritDocToInheritdoc(ownComment)
+
+ superComment(sym) match {
+ case None =>
+ // SI-8210 - The warning would be false negative when this symbol is a setter
+ if (ownComment.indexOf("@inheritdoc") != -1 && ! sym.isSetter)
+ dottydoc.println(s"${sym.pos}: the comment for ${sym} contains @inheritdoc, but no parent comment is available to inherit from.")
+ ownComment.replaceAllLiterally("@inheritdoc", "<invalid inheritdoc annotation>")
+ case Some(sc) =>
+ if (ownComment == "") sc
+ else expandInheritdoc(sc, merge(sc, ownComment, sym), sym)
+ }
+ })
+
+ private def isMovable(str: String, sec: (Int, Int)): Boolean =
+ startsWithTag(str, sec, "@param") ||
+ startsWithTag(str, sec, "@tparam") ||
+ startsWithTag(str, sec, "@return")
+
+ def merge(src: String, dst: String, sym: Symbol, copyFirstPara: Boolean = false): String = {
+ val srcSections = tagIndex(src)
+ val dstSections = tagIndex(dst)
+ val srcParams = paramDocs(src, "@param", srcSections)
+ val dstParams = paramDocs(dst, "@param", dstSections)
+ val srcTParams = paramDocs(src, "@tparam", srcSections)
+ val dstTParams = paramDocs(dst, "@tparam", dstSections)
+ val out = new StringBuilder
+ var copied = 0
+ var tocopy = startTag(dst, dstSections dropWhile (!isMovable(dst, _)))
+
+ if (copyFirstPara) {
+ val eop = // end of comment body (first para), which is delimited by blank line, or tag, or end of comment
+ (findNext(src, 0)(src.charAt(_) == '\n')) min startTag(src, srcSections)
+ out append src.substring(0, eop).trim
+ copied = 3
+ tocopy = 3
+ }
+
+ def mergeSection(srcSec: Option[(Int, Int)], dstSec: Option[(Int, Int)]) = dstSec match {
+ case Some((start, end)) =>
+ if (end > tocopy) tocopy = end
+ case None =>
+ srcSec match {
+ case Some((start1, end1)) => {
+ out append dst.substring(copied, tocopy).trim
+ out append "\n"
+ copied = tocopy
+ out append src.substring(start1, end1).trim
+ }
+ case None =>
+ }
+ }
+
+ //TODO: enable this once you know how to get `sym.paramss`
+ /*
+ for (params <- sym.paramss; param <- params)
+ mergeSection(srcParams get param.name.toString, dstParams get param.name.toString)
+ for (tparam <- sym.typeParams)
+ mergeSection(srcTParams get tparam.name.toString, dstTParams get tparam.name.toString)
+
+ mergeSection(returnDoc(src, srcSections), returnDoc(dst, dstSections))
+ mergeSection(groupDoc(src, srcSections), groupDoc(dst, dstSections))
+ */
+
+ if (out.length == 0) dst
+ else {
+ out append dst.substring(copied)
+ out.toString
+ }
+ }
+
+ /**
+ * Expand inheritdoc tags
+ * - for the main comment we transform the inheritdoc into the super variable,
+ * and the variable expansion can expand it further
+ * - for the param, tparam and throws sections we must replace comments on the spot
+ *
+ * This is done separately, for two reasons:
+ * 1. It takes longer to run compared to merge
+ * 2. The inheritdoc annotation should not be used very often, as building the comment from pieces severely
+ * impacts performance
+ *
+ * @param parent The source (or parent) comment
+ * @param child The child (overriding member or usecase) comment
+ * @param sym The child symbol
+ * @return The child comment with the inheritdoc sections expanded
+ */
+ def expandInheritdoc(parent: String, child: String, sym: Symbol): String =
+ if (child.indexOf("@inheritdoc") == -1)
+ child
+ else {
+ val parentSections = tagIndex(parent)
+ val childSections = tagIndex(child)
+ val parentTagMap = sectionTagMap(parent, parentSections)
+ val parentNamedParams = Map() +
+ ("@param" -> paramDocs(parent, "@param", parentSections)) +
+ ("@tparam" -> paramDocs(parent, "@tparam", parentSections)) +
+ ("@throws" -> paramDocs(parent, "@throws", parentSections))
+
+ val out = new StringBuilder
+
+ def replaceInheritdoc(childSection: String, parentSection: => String) =
+ if (childSection.indexOf("@inheritdoc") == -1)
+ childSection
+ else
+ childSection.replaceAllLiterally("@inheritdoc", parentSection)
+
+ def getParentSection(section: (Int, Int)): String = {
+
+ def getSectionHeader = extractSectionTag(child, section) match {
+ case param@("@param"|"@tparam"|"@throws") => param + " " + extractSectionParam(child, section)
+ case other => other
+ }
+
+ def sectionString(param: String, paramMap: Map[String, (Int, Int)]): String =
+ paramMap.get(param) match {
+ case Some(section) =>
+ // Cleanup the section tag and parameter
+ val sectionTextBounds = extractSectionText(parent, section)
+ cleanupSectionText(parent.substring(sectionTextBounds._1, sectionTextBounds._2))
+ case None =>
+ dottydoc.println(s"""${sym.pos}: the """" + getSectionHeader + "\" annotation of the " + sym +
+ " comment contains @inheritdoc, but the corresponding section in the parent is not defined.")
+ "<invalid inheritdoc annotation>"
+ }
+
+ child.substring(section._1, section._1 + 7) match {
+ case param@("@param "|"@tparam"|"@throws") =>
+ sectionString(extractSectionParam(child, section), parentNamedParams(param.trim))
+ case _ =>
+ sectionString(extractSectionTag(child, section), parentTagMap)
+ }
+ }
+
+ def mainComment(str: String, sections: List[(Int, Int)]): String =
+ if (str.trim.length > 3)
+ str.trim.substring(3, startTag(str, sections))
+ else
+ ""
+
+ // Append main comment
+ out.append("/**")
+ out.append(replaceInheritdoc(mainComment(child, childSections), mainComment(parent, parentSections)))
+
+ // Append sections
+ for (section <- childSections)
+ out.append(replaceInheritdoc(child.substring(section._1, section._2), getParentSection(section)))
+
+ out.append("*/")
+ out.toString
+ }
+
+ protected def expandVariables(initialStr: String, sym: Symbol, site: Symbol)(implicit ctx: Context): String = {
+ val expandLimit = 10
+
+ def expandInternal(str: String, depth: Int): String = {
+ if (depth >= expandLimit)
+ throw new ExpansionLimitExceeded(str)
+
+ val out = new StringBuilder
+ var copied, idx = 0
+ // excluding variables written as \$foo so we can use them when
+ // necessary to document things like Symbol#decode
+ def isEscaped = idx > 0 && str.charAt(idx - 1) == '\\'
+ while (idx < str.length) {
+ if ((str charAt idx) != '$' || isEscaped)
+ idx += 1
+ else {
+ val vstart = idx
+ idx = skipVariable(str, idx + 1)
+ def replaceWith(repl: String) = {
+ out append str.substring(copied, vstart)
+ out append repl
+ copied = idx
+ }
+ variableName(str.substring(vstart + 1, idx)) match {
+ case "super" =>
+ superComment(sym) foreach { sc =>
+ val superSections = tagIndex(sc)
+ replaceWith(sc.substring(3, startTag(sc, superSections)))
+ for (sec @ (start, end) <- superSections)
+ if (!isMovable(sc, sec)) out append sc.substring(start, end)
+ }
+ case "" => idx += 1
+ case vname =>
+ lookupVariable(vname, site) match {
+ case Some(replacement) => replaceWith(replacement)
+ case None =>
+ dottydoc.println(s"Variable $vname undefined in comment for $sym in $site")
+ }
+ }
+ }
+ }
+ if (out.length == 0) str
+ else {
+ out append str.substring(copied)
+ expandInternal(out.toString, depth + 1)
+ }
+ }
+
+ // We suppressed expanding \$ throughout the recursion, and now we
+ // need to replace \$ with $ so it looks as intended.
+ expandInternal(initialStr, 0).replaceAllLiterally("""\$""", "$")
+ }
+
+ def defineVariables(sym: Symbol)(implicit ctx: Context) = {
+ val Trim = "(?s)^[\\s&&[^\n\r]]*(.*?)\\s*$".r
+
+ val raw = ctx.docbase.docstring(sym).map(_.raw).getOrElse("")
+ defs(sym) ++= defines(raw).map {
+ str => {
+ val start = skipWhitespace(str, "@define".length)
+ val (key, value) = str.splitAt(skipVariable(str, start))
+ key.drop(start) -> value
+ }
+ } map {
+ case (key, Trim(value)) =>
+ variableName(key) -> value.replaceAll("\\s+\\*+$", "")
+ }
+ }
+
+ /** Maps symbols to the variable -> replacement maps that are defined
+ * in their doc comments
+ */
+ private val defs = mutable.HashMap[Symbol, Map[String, String]]() withDefaultValue Map()
+
+ /** Lookup definition of variable.
+ *
+ * @param vble The variable for which a definition is searched
+ * @param site The class for which doc comments are generated
+ */
+ def lookupVariable(vble: String, site: Symbol)(implicit ctx: Context): Option[String] = site match {
+ case NoSymbol => None
+ case _ =>
+ val searchList =
+ if (site.flags.is(Flags.Module)) site :: site.info.baseClasses
+ else site.info.baseClasses
+
+ searchList collectFirst { case x if defs(x) contains vble => defs(x)(vble) } match {
+ case Some(str) if str startsWith "$" => lookupVariable(str.tail, site)
+ case res => res orElse lookupVariable(vble, site.owner)
+ }
+ }
+
+ /** The position of the raw doc comment of symbol `sym`, or NoPosition if missing
+ * If a symbol does not have a doc comment but some overridden version of it does,
+ * the position of the doc comment of the overridden version is returned instead.
+ */
+ def docCommentPos(sym: Symbol)(implicit ctx: Context): Position =
+ ctx.docbase.docstring(sym).map(_.pos).getOrElse(NoPosition)
+
+ /** A version which doesn't consider self types, as a temporary measure:
+ * an infinite loop has broken out between superComment and cookedDocComment
+ * since r23926.
+ */
+ private def allInheritedOverriddenSymbols(sym: Symbol)(implicit ctx: Context): List[Symbol] = {
+ if (!sym.owner.isClass) Nil
+ else sym.allOverriddenSymbols.toList.filter(_ != NoSymbol) //TODO: could also be `sym.owner.allOverrid..`
+ //else sym.owner.ancestors map (sym overriddenSymbol _) filter (_ != NoSymbol)
+ }
+
+ class ExpansionLimitExceeded(str: String) extends Exception
+ }
}
diff --git a/src/dotty/tools/dotc/core/Contexts.scala b/src/dotty/tools/dotc/core/Contexts.scala
index a37e367fa..3378f0790 100644
--- a/src/dotty/tools/dotc/core/Contexts.scala
+++ b/src/dotty/tools/dotc/core/Contexts.scala
@@ -582,6 +582,8 @@ object Contexts {
private[this] val _docstrings: mutable.Map[Symbol, Comment] =
mutable.Map.empty
+ val templateExpander = new CommentExpander
+
def docstrings: Map[Symbol, Comment] = _docstrings.toMap
def docstring(sym: Symbol): Option[Comment] = _docstrings.get(sym)
diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala
index 2a8e7da9a..b24fc6237 100644
--- a/src/dotty/tools/dotc/typer/Typer.scala
+++ b/src/dotty/tools/dotc/typer/Typer.scala
@@ -1538,7 +1538,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
tpd.cpy.DefDef(mdef)(rhs = Inliner.bodyToInline(mdef.symbol)) ::
Inliner.removeInlineAccessors(mdef.symbol)
- def typedUsecases(syms: List[Symbol], owner: Symbol)(implicit ctx: Context): Unit = {
+ private def typedUsecases(syms: List[Symbol], owner: Symbol)(implicit ctx: Context): Unit = {
val relevantSyms = syms.filter(ctx.docbase.docstring(_).isDefined)
relevantSyms.foreach { sym =>
expandParentDocs(sym)
@@ -1555,13 +1555,16 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}
}
- import dotty.tools.dottydoc.model.comment.CommentExpander
- val expander = new CommentExpander {}
- def expandParentDocs(sym: Symbol)(implicit ctx: Context): Unit =
+ private def expandParentDocs(sym: Symbol)(implicit ctx: Context): Unit =
ctx.docbase.docstring(sym).foreach { cmt =>
- def expandDoc(owner: Symbol): Unit = {
- expander.defineVariables(sym)
- val newCmt = Comment(cmt.pos, expander.expandedDocComment(sym, owner, cmt.raw))
+ def expandDoc(owner: Symbol): Unit = if (!cmt.isExpanded) {
+ val tplExp = ctx.docbase.templateExpander
+ tplExp.defineVariables(sym)
+
+ val newCmt = cmt
+ .expand(tplExp.expandedDocComment(sym, owner, _))
+ .withUsecases
+
ctx.docbase.addDocstring(sym, Some(newCmt))
}
diff --git a/dottydoc/src/dotty/tools/dottydoc/model/comment/CommentUtils.scala b/src/dotty/tools/dotc/util/CommentParsing.scala
index e5307bd3c..077776b5d 100644
--- a/dottydoc/src/dotty/tools/dottydoc/model/comment/CommentUtils.scala
+++ b/src/dotty/tools/dotc/util/CommentParsing.scala
@@ -3,15 +3,10 @@
* @author Martin Odersky
* @author Felix Mulder
*/
+package dotty.tools.dotc.util
-package dotty.tools
-package dottydoc
-package model
-package comment
-
-import scala.reflect.internal.Chars._
-
-object CommentUtils {
+object CommentParsing {
+ import scala.reflect.internal.Chars._
/** Returns index of string `str` following `start` skipping longest
* sequence of whitespace characters characters (but no newlines)
@@ -221,4 +216,17 @@ object CommentUtils {
result
}
+
+ def removeSections(raw: String, xs: String*): String = {
+ val sections = tagIndex(raw)
+
+ val toBeRemoved = for {
+ section <- xs
+ lines = sections filter { startsWithTag(raw, _, section) }
+ } yield lines
+
+ val end = startTag(raw, toBeRemoved.flatten.sortBy(_._1).toList)
+
+ if (end == raw.length - 2) raw else raw.substring(0, end) + "*/"
+ }
}