Skip to content

Commit bfb46c1

Browse files
committed
Print arguments of concrete annotations
1 parent 9a877cb commit bfb46c1

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import StdNames._
77
import dotty.tools.dotc.ast.tpd
88
import scala.util.Try
99
import util.Spans.Span
10+
import printing.{Showable, Printer, Texts}
11+
import Texts.{Text, Str}
1012

1113
object Annotations {
1214

1315
def annotClass(tree: Tree)(using Context) =
1416
if (tree.symbol.isConstructor) tree.symbol.owner
1517
else tree.tpe.typeSymbol
1618

17-
abstract class Annotation {
19+
abstract class Annotation extends Showable {
1820
def tree(using Context): Tree
1921

2022
def symbol(using Context): Symbol = annotClass(tree)
@@ -44,6 +46,13 @@ object Annotations {
4446
/** The tree evaluation has finished. */
4547
def isEvaluated: Boolean = true
4648

49+
/** A string representation of the annotation.
50+
*/
51+
final def toText(printer: Printer): Text = printer.toText(this)
52+
53+
/** A text representing thge arguments of this annotation. Overridden in ConcreteAnnotation */
54+
def argsText(printer: Printer): Text = ""
55+
4756
def ensureCompleted(using Context): Unit = tree
4857

4958
def sameAnnotation(that: Annotation)(using Context): Boolean =
@@ -52,6 +61,19 @@ object Annotations {
5261

5362
case class ConcreteAnnotation(t: Tree) extends Annotation {
5463
def tree(using Context): Tree = t
64+
override def argsText(printer: Printer): Text =
65+
def toTextArg(arg: Tree): Text = arg match
66+
case Typed(SeqLiteral(elems, _), _) => printer.toTextGlobal(elems, ", ")
67+
case _ => printer.toText(arg)
68+
def recur(tree: Tree): Text = tree match
69+
case TypeApply(fn, args) =>
70+
recur(fn) ~ "[" ~ printer.toTextGlobal(args, ", ") ~ "]"
71+
case Apply(fn, args) =>
72+
recur(fn)
73+
~ (Str("(") ~ Text(args.map(toTextArg), ", ") ~ ")" provided args.nonEmpty)
74+
case _ =>
75+
""
76+
recur(t)
5577
}
5678

5779
abstract class LazyAnnotation extends Annotation {

compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,8 @@ class PlainPrinter(_ctx: Context) extends Printer {
553553
case _ => literalText(String.valueOf(const.value))
554554
}
555555

556-
def toText(annot: Annotation): Text = s"@${annot.symbol.name}" // for now
556+
def toText(annot: Annotation): Text =
557+
Str(s"@${annot.symbol.name}") ~ annot.argsText(this)
557558

558559
def toText(param: LambdaParam): Text =
559560
varianceSign(param.paramVariance)

0 commit comments

Comments
 (0)