Skip to content

Commit 9ab10e7

Browse files
Merge pull request #6276 from dotty-staging/fix-#6270
Fix #6270: Allow change of color setting in TASTy Reflect
2 parents e84ea97 + 31087c6 commit 9ab10e7

File tree

9 files changed

+60
-18
lines changed

9 files changed

+60
-18
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
3232

3333
def Context_source(self: Context): java.nio.file.Path = self.compilationUnit.source.file.jpath
3434

35+
def Context_printColors(self: Context): Boolean = self.settings.color.value(self) == "always"
36+
37+
def Context_withColors(self: Context): Context = ctx.fresh.setSetting(ctx.settings.color, "always")
38+
39+
def Context_withoutColors(self: Context): Context = ctx.fresh.setSetting(ctx.settings.color, "never")
40+
3541
//
3642
// REPORTING
3743
//

library/src-non-bootstrapped/scala/tasty/reflect/QuotedOps.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ trait QuotedOps extends Core { self: Printers =>
1212
def cast[U: scala.quoted.Type](implicit ctx: Context): scala.quoted.Expr[U] =
1313
kernel.QuotedExpr_cast[U](expr)
1414

15-
/** Show a source code like representation of this expression */
15+
/** Show a source code like representation of this expression.
16+
* Will print Ansi colors if ctx.printColors is enabled.
17+
*/
1618
def show(implicit ctx: Context): String =
1719
unseal.showCode
1820
}
@@ -22,7 +24,9 @@ trait QuotedOps extends Core { self: Printers =>
2224
def unseal(implicit ctx: Context): TypeTree =
2325
kernel.QuotedType_unseal(tpe)
2426

25-
/** Show a source code like representation of this type */
27+
/** Show a source code like representation of this type
28+
* Will print Ansi colors if ctx.printColors is enabled.
29+
*/
2630
def show(implicit ctx: Context): String =
2731
unseal.showCode
2832
}

library/src/scala/tasty/Reflection.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class Reflection(val kernel: Kernel)
1616
with Printers
1717
with ReportingOps
1818
with RootPosition
19-
with SettingsOps
2019
with SignatureOps
2120
with StandardDefinitions
2221
with SymbolOps

library/src/scala/tasty/reflect/ContextOps.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ trait ContextOps extends Core {
99

1010
/** Returns the source file being compiled. The path is relative to the current working directory. */
1111
def source: java.nio.file.Path = kernel.Context_source(self)
12+
13+
/** Returns true if the generated strings are allowed to use colors */
14+
def printColors: Boolean = kernel.Context_printColors(self)
15+
16+
/** Returns a new context where printColors is true */
17+
def withColors: Context = kernel.Context_withColors(self)
18+
19+
/** Returns a new context where printColors is false */
20+
def withoutColors: Context = kernel.Context_withoutColors(self)
1221
}
1322

1423
/** Context of the macro expansion */

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ trait Kernel {
139139
/** Returns the source file being compiled. The path is relative to the current working directory. */
140140
def Context_source(self: Context): java.nio.file.Path
141141

142+
/** Returns true if the generated strings are allowed to use colors */
143+
def Context_printColors(self: Context): Boolean
144+
145+
/** Returns a new context where printColors is true */
146+
def Context_withColors(self: Context): Context
147+
148+
/** Returns a new context where printColors is false */
149+
def Context_withoutColors(self: Context): Context
150+
151+
142152
//
143153
// REPORTING
144154
//

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ trait Printers
1414
with ImportSelectorOps
1515
with PatternOps
1616
with PositionOps
17-
with SettingsOps
1817
with SignatureOps
1918
with StandardDefinitions
2019
with SymbolOps
@@ -426,7 +425,7 @@ trait Printers
426425

427426
class SourceCodePrinter extends Printer {
428427

429-
private[this] val color: Boolean = settings.color
428+
private def color(implicit ctx: Context): Boolean = kernel.Context_printColors(ctx)
430429

431430
def showTree(tree: Tree)(implicit ctx: Context): String =
432431
(new Buffer).printTree(tree).result()

library/src/scala/tasty/reflect/SettingsOps.scala

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.quoted._
2+
import scala.tasty._
3+
4+
object api {
5+
inline def (x: => String) reflect : String =
6+
${ reflImpl('x) }
7+
8+
private def reflImpl(x: Expr[String])(implicit refl: Reflection): Expr[String] = {
9+
import refl._
10+
x.show(the[Context].withoutColors).toExpr
11+
}
12+
13+
inline def (x: => String) reflectColor : String =
14+
${ reflImplColor('x) }
15+
16+
private def reflImplColor(x: Expr[String])(implicit refl: Reflection): Expr[String] = {
17+
import refl._
18+
x.show(the[Context].withColors).toExpr
19+
}
20+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import api._
2+
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
assert(args(0).reflect == "args.apply(0)")
6+
assert(args(0).reflectColor.contains("\u001b")) // Contains Ansi color
7+
}
8+
}

0 commit comments

Comments
 (0)