Skip to content

Commit ad52fbe

Browse files
committed
REPL - invoke pprint reflectively
fixes #24111
1 parent 9d428fd commit ad52fbe

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

compiler/src/dotty/tools/repl/Rendering.scala

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
2626

2727
var myClassLoader: AbstractFileClassLoader = uninitialized
2828

29+
private var BlackWhite: AnyRef = uninitialized
30+
private var BlackWhite_apply: java.lang.reflect.Method = uninitialized
31+
private var FansiStr_plainText: java.lang.reflect.Method = uninitialized
32+
private def pprintInit()(using Context): Unit = {
33+
if BlackWhite_apply == null then
34+
val cl = classLoader()
35+
val pprintCls = Class.forName("dotty.shaded.pprint.PPrinter$BlackWhite$", false, cl)
36+
BlackWhite = pprintCls.getField("MODULE$").get(null)
37+
BlackWhite_apply = pprintCls.getMethod("apply",
38+
classOf[Any], // value
39+
classOf[Int], // width
40+
classOf[Int], // height
41+
classOf[Int], // indentation
42+
classOf[Int], // initialOffset
43+
classOf[Boolean], // escape Unicode
44+
classOf[Boolean], // show field names
45+
)
46+
val fansiStrCls = Class.forName("dotty.shaded.fansi.Str", false, cl)
47+
FansiStr_plainText = fansiStrCls.getMethod("plainText")
48+
end if
49+
}
50+
private def pprintRender(value: Any, width: Int, height: Int, initialOffset: Int)(using Context): String = {
51+
pprintInit()
52+
val fansiStr = BlackWhite_apply.invoke(BlackWhite, value, width, height, 2, initialOffset, false, true)
53+
FansiStr_plainText.invoke(fansiStr).asInstanceOf[String]
54+
}
55+
2956

3057
/** Class loader used to load compiled code */
3158
private[repl] def classLoader()(using Context) =
@@ -55,7 +82,8 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
5582
/** Return a String representation of a value we got from `classLoader()`. */
5683
private[repl] def replStringOf(sym: Symbol, value: Object)(using Context): String = {
5784
// pretty-print things with 100 cols 50 rows by default,
58-
dotty.shaded.pprint.PPrinter.BlackWhite.apply(value, width = 100, height = 50).plainText
85+
// dotty.shaded.pprint.PPrinter.BlackWhite.apply(value, width = 100, height = 50).plainText
86+
pprintRender(value, width = 100, height = 50, initialOffset = 0 /* TODO: include offset */)
5987
}
6088

6189
/** Load the value of the symbol using reflection.

0 commit comments

Comments
 (0)