From 5acaee8c8200598384b83608440f06b19b46c0cb Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Thu, 6 Nov 2025 18:15:31 +0100 Subject: [PATCH] Reintroduce reflective REPL pprint call. The class name changed, but the reflective call is still necessary for deeply nested objects to be formatted correctly. --- repl/src/dotty/tools/repl/Rendering.scala | 29 ++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/repl/src/dotty/tools/repl/Rendering.scala b/repl/src/dotty/tools/repl/Rendering.scala index 3f62654fafae..693347103481 100644 --- a/repl/src/dotty/tools/repl/Rendering.scala +++ b/repl/src/dotty/tools/repl/Rendering.scala @@ -27,9 +27,32 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None): var myClassLoader: AbstractFileClassLoader = uninitialized private def pprintRender(value: Any, width: Int, height: Int, initialOffset: Int)(using Context): String = { - pprint.PPrinter.BlackWhite - .apply(value, width = width, height = height, initialOffset = initialOffset) - .plainText + def fallback() = + pprint.PPrinter.BlackWhite + .apply(value, width = width, height = height, initialOffset = initialOffset) + .plainText + try + val cl = classLoader() + val pprintCls = Class.forName("pprint.PPrinter$BlackWhite$", false, cl) + val fansiStrCls = Class.forName("fansi.Str", false, cl) + val BlackWhite = pprintCls.getField("MODULE$").get(null) + val BlackWhite_apply = pprintCls.getMethod("apply", + classOf[Any], // value + classOf[Int], // width + classOf[Int], // height + classOf[Int], // indentation + classOf[Int], // initialOffset + classOf[Boolean], // escape Unicode + classOf[Boolean], // show field names + ) + val FansiStr_plainText = fansiStrCls.getMethod("plainText") + val fansiStr = BlackWhite_apply.invoke( + BlackWhite, value, width, height, 2, initialOffset, false, true + ) + FansiStr_plainText.invoke(fansiStr).asInstanceOf[String] + catch + case ex: ClassNotFoundException => fallback() + case ex: NoSuchMethodException => fallback() }