Skip to content

Commit 96036af

Browse files
Merge pull request #8190 from dotty-staging/use-extension-in-reflection
Use extensions for show* in Reflection
2 parents c48a598 + f175435 commit 96036af

File tree

2 files changed

+122
-129
lines changed

2 files changed

+122
-129
lines changed

library/src/scala/quoted/QuoteContext.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ class QuoteContext(val tasty: scala.tasty.Reflection) {
1414

1515
def show(expr: Expr[_], syntaxHighlight: SyntaxHighlight): String = {
1616
import tasty.{_, given}
17-
expr.unseal.show(syntaxHighlight)
17+
expr.unseal.showWith(syntaxHighlight)
1818
}
1919

2020
def show(tpe: Type[_], syntaxHighlight: SyntaxHighlight): String = {
2121
import tasty.{_, given}
22-
tpe.unseal.show(syntaxHighlight)
22+
tpe.unseal.showWith(syntaxHighlight)
2323
}
2424

2525
/** Report an error at the position of the macro expansion */

library/src/scala/tasty/Reflection.scala

Lines changed: 120 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,25 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
482482

483483
// ----- Tree -----------------------------------------------------
484484

485-
extension TreeOps on (self: Tree) {
485+
/** Members of Tree */
486+
extension TreeOps on (tree: Tree) {
486487
/** Position in the source code */
487-
def pos(given ctx: Context): Position = internal.Tree_pos(self)
488+
def pos(given ctx: Context): Position = internal.Tree_pos(tree)
488489

489-
def symbol(given ctx: Context): Symbol = internal.Tree_symbol(self)
490+
/** Symbol of defined or refered by this tree */
491+
def symbol(given ctx: Context): Symbol = internal.Tree_symbol(tree)
492+
493+
/** Shows the tree as extractors */
494+
def showExtractors(given ctx: Context): String =
495+
new ExtractorsPrinter[self.type](self).showTree(tree)
496+
497+
/** Shows the tree as fully typed source code */
498+
def show(given ctx: Context): String =
499+
tree.showWith(SyntaxHighlight.plain)
500+
501+
/** Shows the tree as fully typed source code */
502+
def showWith(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
503+
new SourceCodePrinter[self.type](self)(syntaxHighlight).showTree(tree)
490504
}
491505

492506
given (given Context): IsInstanceOf[PackageClause] = internal.isInstanceOfPackageClause
@@ -1581,6 +1595,21 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
15811595
/** Returns the type (Type) of T */
15821596
def typeOf[T](given qtype: scala.quoted.Type[T]): Type = qtype.unseal.tpe
15831597

1598+
/** Members of `TypeOrBounds` */
1599+
extension TypeOrBoundsOps on (tpe: TypeOrBounds) {
1600+
/** Shows the tree as extractors */
1601+
def showExtractors(given ctx: Context): String =
1602+
new ExtractorsPrinter[self.type](self).showTypeOrBounds(tpe)
1603+
1604+
/** Shows the tree as fully typed source code */
1605+
def show(given ctx: Context): String =
1606+
tpe.showWith(SyntaxHighlight.plain)
1607+
1608+
/** Shows the tree as fully typed source code */
1609+
def showWith(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
1610+
new SourceCodePrinter[self.type](self)(syntaxHighlight).showTypeOrBounds(tpe)
1611+
}
1612+
15841613
// ----- Types ----------------------------------------------------
15851614

15861615
extension TypeOps on (self: Type) {
@@ -1941,8 +1970,23 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
19411970
// CONSTANTS //
19421971
///////////////
19431972

1973+
/** Members of `Constant` */
19441974
extension ConstantOps on (const: Constant) {
1975+
1976+
/** Returns the value of the constant */
19451977
def value: Any = internal.Constant_value(const)
1978+
1979+
/** Shows the tree as extractors */
1980+
def showExtractors(given ctx: Context): String =
1981+
new ExtractorsPrinter[self.type](self).showConstant(const)
1982+
1983+
/** Shows the tree as fully typed source code */
1984+
def show(given ctx: Context): String =
1985+
const.showWith(SyntaxHighlight.plain)
1986+
1987+
/** Shows the tree as fully typed source code */
1988+
def showWith(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
1989+
new SourceCodePrinter[self.type](self)(syntaxHighlight).showConstant(const)
19461990
}
19471991

19481992
/** Module of Constant literals */
@@ -2046,36 +2090,37 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
20462090
internal.Symbol_noSymbol
20472091
}
20482092

2049-
extension symbolOps on (self: Symbol) {
2093+
/** Members of `Symbol` */
2094+
extension SymbolOps on (sym: Symbol) {
20502095

20512096
/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Throws if this symbol does not have an owner. */
2052-
def owner(given ctx: Context): Symbol = internal.Symbol_owner(self)
2097+
def owner(given ctx: Context): Symbol = internal.Symbol_owner(sym)
20532098

20542099
/** Owner of this symbol. The owner is the symbol in which this symbol is defined. Returns `NoSymbol` if this symbol does not have an owner. */
2055-
def maybeOwner(given ctx: Context): Symbol = internal.Symbol_maybeOwner(self)
2100+
def maybeOwner(given ctx: Context): Symbol = internal.Symbol_maybeOwner(sym)
20562101

20572102
/** Flags of this symbol */
2058-
def flags(given ctx: Context): Flags = internal.Symbol_flags(self)
2103+
def flags(given ctx: Context): Flags = internal.Symbol_flags(sym)
20592104

20602105
/** This symbol is private within the resulting type */
2061-
def privateWithin(given ctx: Context): Option[Type] = internal.Symbol_privateWithin(self)
2106+
def privateWithin(given ctx: Context): Option[Type] = internal.Symbol_privateWithin(sym)
20622107

20632108
/** This symbol is protected within the resulting type */
2064-
def protectedWithin(given ctx: Context): Option[Type] = internal.Symbol_protectedWithin(self)
2109+
def protectedWithin(given ctx: Context): Option[Type] = internal.Symbol_protectedWithin(sym)
20652110

20662111
/** The name of this symbol */
2067-
def name(given ctx: Context): String = internal.Symbol_name(self)
2112+
def name(given ctx: Context): String = internal.Symbol_name(sym)
20682113

20692114
/** The full name of this symbol up to the root package */
2070-
def fullName(given ctx: Context): String = internal.Symbol_fullName(self)
2115+
def fullName(given ctx: Context): String = internal.Symbol_fullName(sym)
20712116

20722117
/** The position of this symbol */
2073-
def pos(given ctx: Context): Position = internal.Symbol_pos(self)
2118+
def pos(given ctx: Context): Position = internal.Symbol_pos(sym)
20742119

2075-
def localContext(given ctx: Context): Context = internal.Symbol_localContext(self)
2120+
def localContext(given ctx: Context): Context = internal.Symbol_localContext(sym)
20762121

20772122
/** The comment for this symbol, if any */
2078-
def comment(given ctx: Context): Option[Comment] = internal.Symbol_comment(self)
2123+
def comment(given ctx: Context): Option[Comment] = internal.Symbol_comment(sym)
20792124

20802125
/** Tree of this definition
20812126
*
@@ -2087,97 +2132,109 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
20872132
* if this symbol `isBind` it will return a `Bind`
20882133
*/
20892134
def tree(given ctx: Context): Tree =
2090-
internal.Symbol_tree(self)
2135+
internal.Symbol_tree(sym)
20912136

20922137
/** Annotations attached to this symbol */
2093-
def annots(given ctx: Context): List[Term] = internal.Symbol_annots(self)
2138+
def annots(given ctx: Context): List[Term] = internal.Symbol_annots(sym)
20942139

2095-
def isDefinedInCurrentRun(given ctx: Context): Boolean = internal.Symbol_isDefinedInCurrentRun(self)
2140+
def isDefinedInCurrentRun(given ctx: Context): Boolean = internal.Symbol_isDefinedInCurrentRun(sym)
20962141

2097-
def isLocalDummy(given ctx: Context): Boolean = internal.Symbol_isLocalDummy(self)
2098-
def isRefinementClass(given ctx: Context): Boolean = internal.Symbol_isRefinementClass(self)
2099-
def isAliasType(given ctx: Context): Boolean = internal.Symbol_isAliasType(self)
2100-
def isAnonymousClass(given ctx: Context): Boolean = internal.Symbol_isAnonymousClass(self)
2101-
def isAnonymousFunction(given ctx: Context): Boolean = internal.Symbol_isAnonymousFunction(self)
2102-
def isAbstractType(given ctx: Context): Boolean = internal.Symbol_isAbstractType(self)
2103-
def isClassConstructor(given ctx: Context): Boolean = internal.Symbol_isClassConstructor(self)
2142+
def isLocalDummy(given ctx: Context): Boolean = internal.Symbol_isLocalDummy(sym)
2143+
def isRefinementClass(given ctx: Context): Boolean = internal.Symbol_isRefinementClass(sym)
2144+
def isAliasType(given ctx: Context): Boolean = internal.Symbol_isAliasType(sym)
2145+
def isAnonymousClass(given ctx: Context): Boolean = internal.Symbol_isAnonymousClass(sym)
2146+
def isAnonymousFunction(given ctx: Context): Boolean = internal.Symbol_isAnonymousFunction(sym)
2147+
def isAbstractType(given ctx: Context): Boolean = internal.Symbol_isAbstractType(sym)
2148+
def isClassConstructor(given ctx: Context): Boolean = internal.Symbol_isClassConstructor(sym)
21042149

21052150
/** Is this the definition of a type? */
2106-
def isType(given ctx: Context): Boolean = internal.Symbol_isType(self)
2151+
def isType(given ctx: Context): Boolean = internal.Symbol_isType(sym)
21072152

21082153
/** Is this the definition of a term? */
2109-
def isTerm(given ctx: Context): Boolean = internal.Symbol_isTerm(self)
2154+
def isTerm(given ctx: Context): Boolean = internal.Symbol_isTerm(sym)
21102155

21112156
/** Is this the definition of a PackageDef tree? */
2112-
def isPackageDef(given ctx: Context): Boolean = internal.Symbol_isPackageDef(self)
2157+
def isPackageDef(given ctx: Context): Boolean = internal.Symbol_isPackageDef(sym)
21132158

21142159
/** Is this the definition of a ClassDef tree? */
2115-
def isClassDef(given ctx: Context): Boolean = internal.Symbol_isClassDef(self)
2160+
def isClassDef(given ctx: Context): Boolean = internal.Symbol_isClassDef(sym)
21162161

21172162
/** Is this the definition of a TypeDef tree */
2118-
def isTypeDef(given ctx: Context): Boolean = internal.Symbol_isTypeDef(self)
2163+
def isTypeDef(given ctx: Context): Boolean = internal.Symbol_isTypeDef(sym)
21192164

21202165
/** Is this the definition of a ValDef tree? */
2121-
def isValDef(given ctx: Context): Boolean = internal.Symbol_isValDef(self)
2166+
def isValDef(given ctx: Context): Boolean = internal.Symbol_isValDef(sym)
21222167

21232168
/** Is this the definition of a DefDef tree? */
2124-
def isDefDef(given ctx: Context): Boolean = internal.Symbol_isDefDef(self)
2169+
def isDefDef(given ctx: Context): Boolean = internal.Symbol_isDefDef(sym)
21252170

21262171
/** Is this the definition of a Bind pattern? */
2127-
def isBind(given ctx: Context): Boolean = internal.Symbol_isBind(self)
2172+
def isBind(given ctx: Context): Boolean = internal.Symbol_isBind(sym)
21282173

21292174
/** Does this symbol represent a no definition? */
2130-
def isNoSymbol(given ctx: Context): Boolean = self == Symbol.noSymbol
2175+
def isNoSymbol(given ctx: Context): Boolean = sym == Symbol.noSymbol
21312176

21322177
/** Does this symbol represent a definition? */
2133-
def exists(given ctx: Context): Boolean = self != Symbol.noSymbol
2178+
def exists(given ctx: Context): Boolean = sym != Symbol.noSymbol
21342179

21352180
/** Fields directly declared in the class */
21362181
def fields(given ctx: Context): List[Symbol] =
2137-
internal.Symbol_fields(self)
2182+
internal.Symbol_fields(sym)
21382183

21392184
/** Field with the given name directly declared in the class */
21402185
def field(name: String)(given ctx: Context): Symbol =
2141-
internal.Symbol_field(self)(name)
2186+
internal.Symbol_field(sym)(name)
21422187

21432188
/** Get non-private named methods defined directly inside the class */
21442189
def classMethod(name: String)(given ctx: Context): List[Symbol] =
2145-
internal.Symbol_classMethod(self)(name)
2190+
internal.Symbol_classMethod(sym)(name)
21462191

21472192
/** Get all non-private methods defined directly inside the class, exluding constructors */
21482193
def classMethods(given ctx: Context): List[Symbol] =
2149-
internal.Symbol_classMethods(self)
2194+
internal.Symbol_classMethods(sym)
21502195

21512196
/** Get named non-private methods declared or inherited */
21522197
def method(name: String)(given ctx: Context): List[Symbol] =
2153-
internal.Symbol_method(self)(name)
2198+
internal.Symbol_method(sym)(name)
21542199

21552200
/** Get all non-private methods declared or inherited */
21562201
def methods(given ctx: Context): List[Symbol] =
2157-
internal.Symbol_methods(self)
2202+
internal.Symbol_methods(sym)
21582203

21592204
/** Fields of a case class type -- only the ones declared in primary constructor */
21602205
def caseFields(given ctx: Context): List[Symbol] =
2161-
internal.Symbol_caseFields(self)
2206+
internal.Symbol_caseFields(sym)
21622207

21632208
def isTypeParam(given ctx: Context): Boolean =
2164-
internal.Symbol_isTypeParam(self)
2209+
internal.Symbol_isTypeParam(sym)
21652210

21662211
/** Signature of this definition */
21672212
def signature(given ctx: Context): Signature =
2168-
internal.Symbol_signature(self)
2213+
internal.Symbol_signature(sym)
21692214

21702215
/** The class symbol of the companion module class */
21712216
def moduleClass(given ctx: Context): Symbol =
2172-
internal.Symbol_moduleClass(self)
2217+
internal.Symbol_moduleClass(sym)
21732218

21742219
/** The symbol of the companion class */
21752220
def companionClass(given ctx: Context): Symbol =
2176-
internal.Symbol_companionClass(self)
2221+
internal.Symbol_companionClass(sym)
21772222

21782223
/** The symbol of the companion module */
21792224
def companionModule(given ctx: Context): Symbol =
2180-
internal.Symbol_companionModule(self)
2225+
internal.Symbol_companionModule(sym)
2226+
2227+
/** Shows the tree as extractors */
2228+
def showExtractors(given ctx: Context): String =
2229+
new ExtractorsPrinter[self.type](self).showSymbol(sym)
2230+
2231+
/** Shows the tree as fully typed source code */
2232+
def show(given ctx: Context): String =
2233+
sym.showWith(SyntaxHighlight.plain)
2234+
2235+
/** Shows the tree as fully typed source code */
2236+
def showWith(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2237+
new SourceCodePrinter[self.type](self)(syntaxHighlight).showSymbol(sym)
21812238
}
21822239

21832240

@@ -2443,16 +2500,29 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
24432500
// FLAGS //
24442501
///////////////
24452502

2446-
extension FlagsOps on (self: Flags) {
2503+
/** Members of `Flag` */
2504+
extension FlagsOps on (flags: Flags) {
24472505

24482506
/** Is the given flag set a subset of this flag sets */
2449-
def is(that: Flags): Boolean = internal.Flags_is(self)(that)
2507+
def is(that: Flags): Boolean = internal.Flags_is(flags)(that)
24502508

24512509
/** Union of the two flag sets */
2452-
def |(that: Flags): Flags = internal.Flags_or(self)(that)
2510+
def |(that: Flags): Flags = internal.Flags_or(flags)(that)
24532511

24542512
/** Intersection of the two flag sets */
2455-
def &(that: Flags): Flags = internal.Flags_and(self)(that)
2513+
def &(that: Flags): Flags = internal.Flags_and(flags)(that)
2514+
2515+
/** Shows the tree as extractors */
2516+
def showExtractors(given ctx: Context): String =
2517+
new ExtractorsPrinter[self.type](self).showFlags(flags)
2518+
2519+
/** Shows the tree as fully typed source code */
2520+
def show(given ctx: Context): String =
2521+
flags.showWith(SyntaxHighlight.plain)
2522+
2523+
/** Shows the tree as fully typed source code */
2524+
def showWith(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2525+
new SourceCodePrinter[self.type](self)(syntaxHighlight).showFlags(flags)
24562526

24572527
}
24582528

@@ -2636,83 +2706,6 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
26362706
def warning(msg: => String, source: SourceFile, start: Int, end: Int)(given ctx: Context): Unit =
26372707
internal.warning(msg, source, start, end)
26382708

2639-
//////////////
2640-
// PRINTERS //
2641-
//////////////
2642-
2643-
/** Adds `show` as an extension method of a `Tree` */
2644-
implicit class TreeShowDeco(tree: Tree) {
2645-
/** Shows the tree as extractors */
2646-
def showExtractors(given ctx: Context): String =
2647-
new ExtractorsPrinter[self.type](self).showTree(tree)
2648-
2649-
/** Shows the tree as fully typed source code */
2650-
def show(given ctx: Context): String =
2651-
show(SyntaxHighlight.plain)
2652-
2653-
/** Shows the tree as fully typed source code */
2654-
def show(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2655-
new SourceCodePrinter[self.type](self)(syntaxHighlight).showTree(tree)
2656-
2657-
}
2658-
2659-
/** Adds `show` as an extension method of a `TypeOrBounds` */
2660-
implicit class TypeOrBoundsShowDeco(tpe: TypeOrBounds) {
2661-
/** Shows the tree as extractors */
2662-
def showExtractors(given ctx: Context): String =
2663-
new ExtractorsPrinter[self.type](self).showTypeOrBounds(tpe)
2664-
2665-
/** Shows the tree as fully typed source code */
2666-
def show(given ctx: Context): String =
2667-
show(SyntaxHighlight.plain)
2668-
2669-
/** Shows the tree as fully typed source code */
2670-
def show(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2671-
new SourceCodePrinter[self.type](self)(syntaxHighlight).showTypeOrBounds(tpe)
2672-
}
2673-
2674-
/** Adds `show` as an extension method of a `Constant` */
2675-
implicit class ConstantShowDeco(const: Constant) {
2676-
/** Shows the tree as extractors */
2677-
def showExtractors(given ctx: Context): String =
2678-
new ExtractorsPrinter[self.type](self).showConstant(const)
2679-
2680-
/** Shows the tree as fully typed source code */
2681-
def show(given ctx: Context): String = show(SyntaxHighlight.plain)
2682-
2683-
/** Shows the tree as fully typed source code */
2684-
def show(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2685-
new SourceCodePrinter[self.type](self)(syntaxHighlight).showConstant(const)
2686-
}
2687-
2688-
/** Adds `show` as an extension method of a `Symbol` */
2689-
implicit class SymbolShowDeco(symbol: Symbol) {
2690-
/** Shows the tree as extractors */
2691-
def showExtractors(given ctx: Context): String =
2692-
new ExtractorsPrinter[self.type](self).showSymbol(symbol)
2693-
2694-
/** Shows the tree as fully typed source code */
2695-
def show(given ctx: Context): String = show(SyntaxHighlight.plain)
2696-
2697-
/** Shows the tree as fully typed source code */
2698-
def show(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2699-
new SourceCodePrinter[self.type](self)(syntaxHighlight).showSymbol(symbol)
2700-
}
2701-
2702-
/** Adds `show` as an extension method of a `Flags` */
2703-
implicit class FlagsShowDeco(flags: Flags) {
2704-
/** Shows the tree as extractors */
2705-
def showExtractors(given ctx: Context): String =
2706-
new ExtractorsPrinter[self.type](self).showFlags(flags)
2707-
2708-
/** Shows the tree as fully typed source code */
2709-
def show(given ctx: Context): String = show(SyntaxHighlight.plain)
2710-
2711-
/** Shows the tree as fully typed source code */
2712-
def show(syntaxHighlight: SyntaxHighlight)(given ctx: Context): String =
2713-
new SourceCodePrinter[self.type](self)(syntaxHighlight).showFlags(flags)
2714-
}
2715-
27162709

27172710
//////////////
27182711
// COMMENTS //

0 commit comments

Comments
 (0)