Skip to content

Commit 0473b15

Browse files
committed
Tastydoc: update tasty.reflect usage
1 parent 88519e7 commit 0473b15

File tree

4 files changed

+65
-47
lines changed

4 files changed

+65
-47
lines changed

tastydoc/src/dotty/tastydoc/DocPrinter.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ class DocPrinter(mutablePackagesMap: scala.collection.mutable.HashMap[String, Em
128128
(if(c.authors.nonEmpty) Md.bold(Md.italics("authors")) + " " + c.authors.mkString(", ") + "\n" else "") +
129129
(if(c.see.nonEmpty) Md.bold(Md.italics("see")) + " " + c.see.mkString(", ") + "\n" else "") +
130130
(if(c.result.isDefined) Md.bold(Md.italics("return")) + " " + c.result.get + "\n" else "") +
131-
(if(c.throws.nonEmpty) c.throws.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") else "") +
132-
(if(c.valueParams.nonEmpty) c.valueParams.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") + "\n" else "") +
133-
(if(c.typeParams.nonEmpty) c.typeParams.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") + "\n" else "") +
131+
(if(c.throws.nonEmpty) c.throws.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") else "") +
132+
(if(c.valueParams.nonEmpty) c.valueParams.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") + "\n" else "") +
133+
(if(c.typeParams.nonEmpty) c.typeParams.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") + "\n" else "") +
134134
(if(c.version.isDefined) Md.bold(Md.italics("version")) + " " + c.version.get + "\n" else "") +
135135
(if(c.since.isDefined) Md.bold(Md.italics("since")) + " " + c.since.get + "\n" else "") +
136136
(if(c.todo.nonEmpty) Md.bold(Md.italics("TODO")) + " " + c.todo.mkString(", ") + "\n" else "") +
@@ -139,9 +139,9 @@ class DocPrinter(mutablePackagesMap: scala.collection.mutable.HashMap[String, Em
139139
(if(c.example.nonEmpty) Md.bold(Md.italics("Example")) + " " + c.example.mkString("\n") + "\n" else "") +
140140
(if(c.constructor.isDefined) Md.bold(Md.italics("Constructor")) + " " + c.constructor.get + "\n" else "") +
141141
(if(c.group.isDefined) Md.bold(Md.italics("Group")) + " " + c.group.get + "\n" else "") +
142-
(if(c.groupDesc.nonEmpty) c.groupDesc.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") else "") +
143-
(if(c.groupNames.nonEmpty) c.groupNames.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") + "\n" else "") +
144-
(if(c.groupPrio.nonEmpty) c.groupPrio.map((x, y) => Md.bold(Md.italics(x)) + " " + y).mkString("\n") + "\n" else "") +
142+
(if(c.groupDesc.nonEmpty) c.groupDesc.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") else "") +
143+
(if(c.groupNames.nonEmpty) c.groupNames.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") + "\n" else "") +
144+
(if(c.groupPrio.nonEmpty) c.groupPrio.map { case (x, y) => Md.bold(Md.italics(x)) + " " + y }.mkString("\n") + "\n" else "") +
145145
(if(c.hideImplicitConversions.nonEmpty) Md.bold(Md.italics("Hide Implicit Conversions")) + " " + c.hideImplicitConversions.mkString(", ") + "\n" else "")
146146
case None => ""
147147
}
@@ -434,4 +434,4 @@ class DocPrinter(mutablePackagesMap: scala.collection.mutable.HashMap[String, Em
434434

435435
case _ =>
436436
}
437-
}
437+
}

tastydoc/src/dotty/tastydoc/TastyExtractor.scala

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import dotty.tastydoc.representations._
88
/** A trait containing useful methods for extracting information from the reflect API */
99
trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentCleaner{
1010
def extractPath(reflect: Reflection)(symbol: reflect.Symbol) : List[String] = {
11-
import reflect._
11+
import reflect.{given, _}
1212

13-
val pathArray = symbol.show(implicitly[reflect.Context].withoutColors).split("\\.")
13+
val pathArray = symbol.show.split("\\.") // NOTE: this should print w/o colors, inspect afterwards
1414
pathArray.view(0, pathArray.length - 1).toList
1515
}
1616

1717
def extractModifiers(reflect: Reflection)(flags: reflect.Flags, privateWithin: Option[reflect.Type], protectedWithin: Option[reflect.Type]) : (List[String], Option[Reference], Option[Reference]) = {
18-
import reflect._
18+
import reflect.{given, _}
1919

2020
(((if(flags.is(Flags.Override)) "override" else "") ::
2121
(if(flags.is(Flags.Private)) "private" else "")::
@@ -40,7 +40,7 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
4040
}
4141

4242
def extractComments(reflect: Reflection)(comment: Option[reflect.Comment], rep: Representation) : (Map[String, EmulatedPackageRepresentation], String) => Option[Comment] = {
43-
import reflect._
43+
import reflect.{given, _}
4444

4545
comment match {
4646
case Some(com) =>
@@ -58,8 +58,8 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
5858
}
5959
}
6060

61-
def extractClassMembers(reflect: Reflection)(body: List[reflect.Statement], symbol: reflect.ClassDefSymbol, parentRepresentation: Some[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) : List[Representation with Modifiers] = {
62-
import reflect._
61+
def extractClassMembers(reflect: Reflection)(body: List[reflect.Statement], symbol: reflect.Symbol, parentRepresentation: Some[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) : List[Representation with Modifiers] = {
62+
import reflect.{given, _}
6363

6464
/** Filter fields which shouldn't be displayed in the doc
6565
*/
@@ -74,14 +74,22 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
7474
!(symbol.owner.name == "Any" && ownerPath == List("scala"))
7575
}
7676

77-
(body.flatMap{
77+
(body.flatMap {
7878
case IsDefDef(_) => None //No definitions, they are appended with symbol.methods below
7979
case IsValDef(_) => None //No val/var, they are appended with symbol.fields below
8080
case IsInlined(_) => None //Inlined aren't desirable members
8181
case x => Some(x)
8282
}.filter(x => filterSymbol(x.symbol)).map(convertToRepresentation(reflect)(_, parentRepresentation)) ++
8383
symbol.methods.filter(x => filterSymbol(x)).map{x => convertToRepresentation(reflect)(x.tree, parentRepresentation)} ++
84-
symbol.fields.filter(x => filterSymbol(x)).flatMap{case reflect.IsValDefSymbol(x) => Some(x) case _ => None}.map{x => convertToRepresentation(reflect)(x.tree, parentRepresentation)}
84+
symbol.fields.filter { x =>
85+
filterSymbol(x)
86+
}.flatMap {
87+
case x if x.isValDef => Some(x)
88+
// case reflect.IsValDefSymbol(x) => Some(x)
89+
case _ => None
90+
}.map { x =>
91+
convertToRepresentation(reflect)(x.tree, parentRepresentation)
92+
}
8593
)
8694
.flatMap{
8795
case r: Representation with Modifiers => Some(r)
@@ -91,7 +99,7 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
9199
}
92100

93101
def extractParents(reflect: Reflection)(parents: List[reflect.Tree]): List[Reference] = {
94-
import reflect._
102+
import reflect.{given, _}
95103

96104
val parentsReferences = parents.map{
97105
case reflect.IsTypeTree(c) => convertTypeToReference(reflect)(c.tpe)
@@ -110,7 +118,7 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
110118
* @return (is case, is a trait, is an object, the kind as a String)
111119
*/
112120
def extractKind(reflect: Reflection)(flags: reflect.Flags): (Boolean, Boolean, Boolean, String) = {
113-
import reflect._
121+
import reflect.{given, _}
114122

115123
val isCase = flags.is(reflect.Flags.Case)
116124
val isTrait = flags.is(reflect.Flags.Trait)
@@ -134,8 +142,8 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
134142
(isCase, isTrait, isObject, kind)
135143
}
136144

137-
def extractCompanion(reflect: Reflection)(companionModule: Option[reflect.ValDefSymbol], companionClass: Option[reflect.ClassDefSymbol], companionIsObject: Boolean): Option[CompanionReference] = {
138-
import reflect._
145+
def extractCompanion(reflect: Reflection)(companionModule: Option[reflect.Symbol], companionClass: Option[reflect.Symbol], companionIsObject: Boolean): Option[CompanionReference] = {
146+
import reflect.{given, _}
139147

140148
if(companionIsObject){
141149
companionModule match {
@@ -157,7 +165,7 @@ trait TastyExtractor extends TastyTypeConverter with CommentParser with CommentC
157165
}
158166

159167
def extractAnnotations(reflect: Reflection)(annots: List[reflect.Term]): List[TypeReference] = {
160-
import reflect._
168+
import reflect.{given, _}
161169

162170
def keepAnnot(label: String, link: String): Boolean = {
163171
!(label == "SourceFile" && link == "/internal") &&

tastydoc/src/dotty/tastydoc/TastyTypeConverter.scala

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dotty.tastydoc.references._
77
trait TastyTypeConverter {
88

99
def convertTypeOrBoundsToReference(reflect: Reflection)(typeOrBounds: reflect.TypeOrBounds): Reference = {
10-
import reflect._
10+
import reflect.{given, _}
1111

1212
def anyOrNothing(reference: Reference): Boolean = reference match {
1313
case TypeReference("Any", "/scala", _, _) => true
@@ -30,18 +30,18 @@ trait TastyTypeConverter {
3030
}
3131

3232
def convertTypeToReference(reflect: Reflection)(tp: reflect.Type): Reference = {
33-
import reflect._
33+
import reflect.{given, _}
3434

3535
//Inner method to avoid passing the reflection each time
3636
def inner(tp: reflect.Type): Reference = tp match {
37-
case reflect.Type.IsOrType(reflect.Type.OrType(left, right)) => OrTypeReference(inner(left), inner(right))
38-
case reflect.Type.IsAndType(reflect.Type.AndType(left, right)) => AndTypeReference(inner(left), inner(right))
39-
case reflect.Type.IsByNameType(reflect.Type.ByNameType(tpe)) => ByNameReference(inner(tpe))
40-
case reflect.Type.IsConstantType(reflect.Type.ConstantType(constant)) => ConstantReference(constant.value.toString)
41-
case reflect.Type.IsThisType(reflect.Type.ThisType(tpe)) => inner(tpe)
42-
case reflect.Type.IsAnnotatedType(reflect.Type.AnnotatedType(tpe, _)) => inner(tpe)
43-
case reflect.Type.IsTypeLambda(reflect.Type.TypeLambda(paramNames, paramTypes, resType)) => ConstantReference(tp.show(implicitly[reflect.Context].withoutColors)) //TOFIX
44-
case reflect.Type.IsRefinement(reflect.Type.Refinement(parent, name, info)) =>
37+
case OrType(left, right) => OrTypeReference(inner(left), inner(right))
38+
case AndType(left, right) => AndTypeReference(inner(left), inner(right))
39+
case ByNameType(tpe) => ByNameReference(inner(tpe))
40+
case ConstantType(constant) => ConstantReference(constant.value.toString)
41+
case ThisType(tpe) => inner(tpe)
42+
case AnnotatedType(tpe, _) => inner(tpe)
43+
case TypeLambda(paramNames, paramTypes, resType) => ConstantReference(tp.show) //TOFIX
44+
case Refinement(parent, name, info) =>
4545
val tuple = convertTypeOrBoundsToReference(reflect)(info) match {
4646
case r if (info match {case reflect.IsTypeBounds(info) => true case _ => false}) => ("type", name, r)
4747
case r@TypeReference(_, _, _, _) => ("val", name, r)
@@ -53,7 +53,7 @@ trait TastyTypeConverter {
5353
RefinedReference(p, ls:+tuple)
5454
case t => RefinedReference(t, List(tuple))
5555
}
56-
case reflect.Type.IsAppliedType(reflect.Type.AppliedType(tpe, typeOrBoundsList)) =>
56+
case AppliedType(tpe, typeOrBoundsList) =>
5757
inner(tpe) match {
5858
case TypeReference(label, link, _, hasOwnFile) =>
5959
if(link == "/scala"){
@@ -70,26 +70,32 @@ trait TastyTypeConverter {
7070
}
7171
case _ => throw Exception("Match error in AppliedType. This should not happen, please open an issue. " + tp)
7272
}
73-
case reflect.Type.IsTypeRef(reflect.Type.TypeRef(typeName, qual)) =>
73+
case TypeRef(qual, typeName) =>
7474
convertTypeOrBoundsToReference(reflect)(qual) match {
7575
case TypeReference(label, link, xs, _) => TypeReference(typeName, link + "/" + label, xs, true)
7676
case EmptyReference => TypeReference(typeName, "", Nil, true)
7777
case _ => throw Exception("Match error in TypeRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
7878
}
79-
case reflect.Type.IsTermRef(reflect.Type.TermRef(typeName, qual)) =>
79+
case TermRef(qual, typeName) =>
8080
convertTypeOrBoundsToReference(reflect)(qual) match {
8181
case TypeReference(label, link, xs, _) => TypeReference(typeName + "$", link + "/" + label, xs)
8282
case EmptyReference => TypeReference(typeName, "", Nil)
8383
case _ => throw Exception("Match error in TermRef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(qual))
8484
}
85-
case reflect.Type.IsSymRef(reflect.Type.SymRef(symbol, typeOrBounds)) => symbol match {
85+
86+
// NOTE: old SymRefs are now either TypeRefs or TermRefs - the logic here needs to be moved into above branches
87+
// NOTE: _.symbol on *Ref returns its symbol
88+
case SymRef(symbol, typeOrBounds) => symbol match {
89+
// NOTE: Only TypeRefs can reference ClassDefSymbols
8690
case reflect.IsClassDefSymbol(_) => //Need to be split because these types have their own file
8791
convertTypeOrBoundsToReference(reflect)(typeOrBounds) match {
8892
case TypeReference(label, link, xs, _) => TypeReference(symbol.name, link + "/" + label, xs, true)
8993
case EmptyReference if symbol.name == "<root>" | symbol.name == "_root_" => EmptyReference
9094
case EmptyReference => TypeReference(symbol.name, "", Nil, true)
9195
case _ => throw Exception("Match error in SymRef/TypeOrBounds/ClassDef. This should not happen, please open an issue. " + convertTypeOrBoundsToReference(reflect)(typeOrBounds))
9296
}
97+
98+
// NOTE: This branch handles packages, which are now TypeRefs
9399
case reflect.IsTermSymbol(_) | reflect.IsTypeDefSymbol(_) =>
94100
convertTypeOrBoundsToReference(reflect)(typeOrBounds) match {
95101
case TypeReference(label, link, xs, _) => TypeReference(symbol.name, link + "/" + label, xs)

tastydoc/src/dotty/tastydoc/representations.scala

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,31 @@ object representations extends TastyExtractor {
8383
}
8484

8585
class PackageRepresentation(reflect: Reflection, internal: reflect.PackageClause, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation with Members {
86-
import reflect._
86+
import reflect.{given, _}
8787

88-
override val (name, path) = extractPackageNameAndPath(internal.pid.show(implicitly[reflect.Context].withoutColors))
88+
override val (name, path) = extractPackageNameAndPath(internal.pid.show)
8989
override val members = internal.stats.map(convertToRepresentation(reflect)(_, Some(this)))
9090
override val annotations = extractAnnotations(reflect)(internal.symbol.annots)
9191

9292
override def comments(packages: Map[String, EmulatedPackageRepresentation], userDocSyntax: String) = extractComments(reflect)(internal.symbol.comment, this)(packages, userDocSyntax)
9393
}
9494

9595
class ImportRepresentation(reflect: Reflection, internal: reflect.Import, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation {
96-
import reflect._
96+
import reflect.{given, _}
9797

9898
override val name = if (internal.selectors.size > 1){
9999
internal.selectors.map(_.toString).mkString("{", ", ", "}")
100100
} else {
101101
internal.selectors.head.toString
102102
}
103-
override val path = internal.expr.symbol.show(implicitly[reflect.Context].withoutColors).split("\\.").toList
103+
override val path = internal.expr.symbol.show.split("\\.").toList
104104
override val annotations = extractAnnotations(reflect)(internal.symbol.annots)
105105

106106
override def comments(packages: Map[String, EmulatedPackageRepresentation], userDocSyntax: String) = extractComments(reflect)(internal.symbol.comment, this)(packages, userDocSyntax)
107107
}
108108

109109
class ClassRepresentation(reflect: Reflection, internal: reflect.ClassDef, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation with Members with Parents with Modifiers with Companion with Constructors with TypeParams {
110-
import reflect._
110+
import reflect.{given, _}
111111

112112
override val path = extractPath(reflect)(internal.symbol)
113113
override val parents = extractParents(reflect)(internal.parents)
@@ -124,27 +124,31 @@ object representations extends TastyExtractor {
124124
case _ => None
125125
}
126126
}
127-
override val typeParams = internal.constructor.typeParams.map(x => x.show(implicitly[reflect.Context].withoutColors).stripPrefix("type "))
127+
override val typeParams = internal.constructor.typeParams.map(x => x.show.stripPrefix("type "))
128128
override val annotations = extractAnnotations(reflect)(internal.symbol.annots)
129129
var knownSubclasses: List[Reference] = Nil
130130

131131
val (isCase, isTrait, isObject, kind) = extractKind(reflect)(internal.symbol.flags)
132132

133133
override val name = internal.name
134134

135-
override val companion = extractCompanion(reflect)(internal.symbol.companionModule, internal.symbol.companionClass, !isObject)
135+
override val companion = extractCompanion(reflect)(
136+
Some(internal.symbol.companionModule).filter(_.exists), // TODO: refactor later, there is now a NoSymbol
137+
Some(internal.symbol.companionClass).filter(_.exists), // TODO: refactor later, there is now a NoSymbol
138+
!isObject
139+
)
136140
override val members: List[Representation with Modifiers] = extractClassMembers(reflect)(internal.body, internal.symbol, Some(this))
137141

138142
override def comments(packages: Map[String, EmulatedPackageRepresentation], userDocSyntax: String) = extractComments(reflect)(internal.symbol.comment, this)(packages, userDocSyntax)
139143
}
140144

141145
class DefRepresentation(reflect: Reflection, internal: reflect.DefDef, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation with Modifiers with TypeParams with MultipleParamList with ReturnValue {
142-
import reflect._
146+
import reflect.{given, _}
143147

144148
override val name = internal.name
145149
override val path = extractPath(reflect)(internal.symbol)
146150
override val (modifiers, privateWithin, protectedWithin) = extractModifiers(reflect)(internal.symbol.flags, internal.symbol.privateWithin, internal.symbol.protectedWithin)
147-
override val typeParams = internal.typeParams.map(x => x.show(implicitly[reflect.Context].withoutColors).stripPrefix("type "))
151+
override val typeParams = internal.typeParams.map(x => x.show.stripPrefix("type "))
148152

149153
override val paramLists = internal.paramss.map{p =>
150154
new ParamList {
@@ -158,7 +162,7 @@ object representations extends TastyExtractor {
158162
}
159163

160164
class ValRepresentation(reflect: Reflection, internal: reflect.ValDef, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation with Modifiers with ReturnValue {
161-
import reflect._
165+
import reflect.{given, _}
162166

163167
override val name = internal.name
164168
override val path = extractPath(reflect)(internal.symbol)
@@ -171,7 +175,7 @@ object representations extends TastyExtractor {
171175
}
172176

173177
class TypeRepresentation(reflect: Reflection, internal: reflect.TypeDef, override val parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]) extends Representation with Modifiers with TypeParams {
174-
import reflect._
178+
import reflect.{given, _}
175179

176180
override val name = internal.name
177181
override val path = extractPath(reflect)(internal.symbol)
@@ -188,11 +192,11 @@ object representations extends TastyExtractor {
188192
}
189193

190194
def convertToRepresentation(reflect: Reflection)(tree: reflect.Tree, parentRepresentation: Option[Representation])(given mutablePackagesMap: scala.collection.mutable.HashMap[String, EmulatedPackageRepresentation]): Representation = {
191-
import reflect._
195+
import reflect.{given, _}
192196

193197
tree match {
194198
case reflect.IsPackageClause(t@reflect.PackageClause(_)) =>
195-
val noColorPid = t.pid.symbol.show(implicitly[reflect.Context].withoutColors)
199+
val noColorPid = t.pid.symbol.show
196200
val emulatedPackage = mutablePackagesMap.get(noColorPid) match {
197201
case Some(x) => x
198202
case None =>

0 commit comments

Comments
 (0)