Skip to content

Commit 79092cb

Browse files
committed
Scala3doc: cook comments
Resolve @define-s and expand @inheritdoc-s.
1 parent 1a4106f commit 79092cb

File tree

4 files changed

+437
-7
lines changed

4 files changed

+437
-7
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ object Comments {
4444
* @param expanded If this comment has been expanded, it's expansion, otherwise `None`.
4545
* @param usecases The usecases for this comment.
4646
*/
47-
final case class Comment(span: Span, raw: String, expanded: Option[String], usecases: List[UseCase]) {
47+
final case class Comment(
48+
span: Span,
49+
raw: String,
50+
expanded: Option[String],
51+
usecases: List[UseCase],
52+
variables: Map[String, String],
53+
) {
4854

4955
/** Has this comment been cooked or expanded? */
5056
def isExpanded: Boolean = expanded.isDefined
@@ -65,7 +71,7 @@ object Comments {
6571
def expand(f: String => String)(using Context): Comment = {
6672
val expandedComment = f(raw)
6773
val useCases = Comment.parseUsecases(expandedComment, span)
68-
Comment(span, raw, Some(expandedComment), useCases)
74+
Comment(span, raw, Some(expandedComment), useCases, Map.empty)
6975
}
7076
}
7177

@@ -74,7 +80,7 @@ object Comments {
7480
def isDocComment(comment: String): Boolean = comment.startsWith("/**")
7581

7682
def apply(span: Span, raw: String): Comment =
77-
Comment(span, raw, None, Nil)
83+
Comment(span, raw, None, Nil, Map.empty)
7884

7985
private def parseUsecases(expandedComment: String, span: Span)(using Context): List[UseCase] =
8086
if (!isDocComment(expandedComment))

scala3doc-testcases/src/tests/tests.scala

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package tests
1010
* This is an *important* _test_ class.
1111
* Important enough to get multiple sentences in its summary.
1212
*
13+
* Here is foo: $foo
14+
*
1315
* And `this` is inline code.
1416
*
1517
* And this is the **strong** __emphasis__ test.
@@ -56,17 +58,25 @@ package tests
5658
* @version 1.0.0
5759
* @result A class doesn't actually have a result.
5860
* @constructor A class has a constructor, and this one is important.
61+
*
62+
* @define foo Foo expanded.
5963
*/
6064
class A {
6165

62-
/** This is a method.
66+
/** This is my method.
6367
*
6468
* This is a link: [[AA]].
6569
*
6670
* This is another link: [[AA$]].
6771
*
6872
* And yet another: [[B]].
6973
*/
74+
final def myMethod(s: String): String = s
75+
76+
/** This is a method.
77+
*
78+
* This is foo: $foo
79+
*/
7080
def method(s: String): String = s
7181

7282
class AA
@@ -85,6 +95,8 @@ object A
8595
*
8696
* This is an ''important'' '''test''' __class__. And `this` is inline code.
8797
*
98+
* Here is foo: $foo
99+
*
88100
* While
89101
* {{{
90102
* this.is("a code block")
@@ -104,9 +116,15 @@ object A
104116
*
105117
* And this is his companion: [[tests.A$]].
106118
* @syntax wiki
119+
* @define foo Bar, actually.
107120
*/
108121
class B extends A {
109-
/** This is a method. */
122+
/** @inheritdoc */ override def method(s: String): String = s
123+
124+
/** This is a method.
125+
*
126+
* And this is my foo: $foo
127+
*/
110128
def otherMethod(s: String): String = s
111129

112130
class BB

scala3doc/src/dotty/dokka/tasty/ScalaDocSupport.scala

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,27 @@ trait ScaladocSupport { self: TastyParser =>
1212
import qctx.reflect._
1313

1414
def parseComment(
15-
commentNode: Documentation,
15+
commentPre: Documentation,
1616
tree: Tree
1717
): dkkd.DocumentationNode = {
18+
val commentNode =
19+
if tree.symbol.isClassDef || tree.symbol.owner.isClassDef then
20+
import dotty.tools.dotc
21+
given ctx as dotc.core.Contexts.Context = qctx.asInstanceOf[scala.quoted.runtime.impl.QuotesImpl].ctx
22+
23+
// println(s"Expanding comment for sym: ${tree.symbol.show}")
24+
val sym = tree.symbol.asInstanceOf[dotc.core.Symbols.Symbol]
25+
val owner =
26+
if tree.symbol.isClassDef then sym else sym.owner
27+
28+
comments.CommentExpander.cookComment(sym, owner)(using ctx)
29+
.get.asInstanceOf[Documentation]
30+
else
31+
commentPre
32+
33+
val commentString = commentNode.expanded getOrElse commentNode.raw
1834
val preparsed =
19-
comments.Preparser.preparse(comments.Cleaner.clean(commentNode.raw))
35+
comments.Preparser.preparse(comments.Cleaner.clean(commentString))
2036

2137
val commentSyntax =
2238
preparsed.syntax.headOption match {

0 commit comments

Comments
 (0)