diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 9e34f8d726b5..7da0f2c4e33e 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -149,6 +149,7 @@ private sealed trait VerboseSettings: val Vprofile: Setting[Boolean] = BooleanSetting("-Vprofile", "Show metrics about sources and internal representations to estimate compile-time complexity.") val VprofileSortedBy = ChoiceSetting("-Vprofile-sorted-by", "key", "Show metrics about sources and internal representations sorted by given column name", List("name", "path", "lines", "tokens", "tasty", "complexity"), "") val VprofileDetails = IntSetting("-Vprofile-details", "Show metrics about sources and internal representations of the most complex methods", 0) + val VreplMaxPrintElements: Setting[Int] = IntSetting("-Vrepl-max-print-elements", "Number of elements to be printed before output is truncated.", 1000) /** -W "Warnings" settings */ diff --git a/compiler/src/dotty/tools/repl/Rendering.scala b/compiler/src/dotty/tools/repl/Rendering.scala index 608ca23c5fec..6cb89a2b4981 100644 --- a/compiler/src/dotty/tools/repl/Rendering.scala +++ b/compiler/src/dotty/tools/repl/Rendering.scala @@ -32,8 +32,11 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None): private var myClassLoader: AbstractFileClassLoader = _ - private var myReplStringOf: Object => String = _ + /** (value, maxElements) => String */ + private var myReplStringOf: (Object, Int) => String = _ + /** info to add if output got truncated */ + private val infoOutputGotTruncated = " ... large output truncated, print value to show all" /** Class loader used to load compiled code */ private[repl] def classLoader()(using Context) = @@ -60,17 +63,28 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None): // For old API, try to clean up extraneous newlines by stripping suffix and maybe prefix newline. val scalaRuntime = Class.forName("scala.runtime.ScalaRunTime", true, myClassLoader) val renderer = "stringOf" // was: replStringOf - try { - val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean]) - val truly = java.lang.Boolean.TRUE - - (value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements), truly).asInstanceOf[String] - } catch { - case _: NoSuchMethodException => - val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int]) + def stringOfMaybeTruncated(value: Object, maxElements: Int): String = { + try { + val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean]) + val truly = java.lang.Boolean.TRUE + meth.invoke(null, value, maxElements, truly).asInstanceOf[String] + } catch { + case _: NoSuchMethodException => + val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int]) + meth.invoke(null, value, maxElements).asInstanceOf[String] + } + } - (value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements)).asInstanceOf[String] + (value: Object, maxElements: Int) => { + // `ScalaRuntime.stringOf` may truncate the output, in which case we want to indicate that fact to the user + // In order to figure out if it did get truncated, we invoke it twice - once with the `maxElements` that we + // want to print, and once without a limit. If the first is shorter, truncation did occur. + val maybeTruncated = stringOfMaybeTruncated(value, maxElements) + val notTruncated = stringOfMaybeTruncated(value, Int.MaxValue) + if (maybeTruncated.length == notTruncated.length) maybeTruncated + else maybeTruncated + infoOutputGotTruncated } + } myClassLoader } @@ -82,16 +96,16 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None): * https://github.com/scala/bug/issues/12337 */ private[repl] def truncate(str: String): String = - val showTruncated = " ... large output truncated, print value to show all" val ncp = str.codePointCount(0, str.length) // to not cut inside code point if ncp <= MaxStringElements then str - else str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 1)) + showTruncated + else str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 1)) + infoOutputGotTruncated /** Return a String representation of a value we got from `classLoader()`. */ private[repl] def replStringOf(value: Object)(using Context): String = assert(myReplStringOf != null, "replStringOf should only be called on values creating using `classLoader()`, but `classLoader()` has not been called so far") - val res = myReplStringOf(value) + val maxPrintElements = ctx.settings.VreplMaxPrintElements.valueIn(ctx.settingsState) + val res = myReplStringOf(value, maxPrintElements) if res == null then "null // non-null reference has null-valued toString" else truncate(res) /** Load the value of the symbol using reflection. diff --git a/compiler/test-resources/repl/settings-repl-disable-display b/compiler/test-resources/repl/settings-repl-disable-display new file mode 100644 index 000000000000..ba2c1c64574b --- /dev/null +++ b/compiler/test-resources/repl/settings-repl-disable-display @@ -0,0 +1,12 @@ +scala> 1 +val res0: Int = 1 + +scala>:settings -Xrepl-disable-display + +scala> 2 + +scala>:reset +Resetting REPL state. + +scala> 3 +val res0: Int = 3 \ No newline at end of file diff --git a/compiler/test-resources/repl/settings-repl-max-print-elements b/compiler/test-resources/repl/settings-repl-max-print-elements new file mode 100644 index 000000000000..b203e689f020 --- /dev/null +++ b/compiler/test-resources/repl/settings-repl-max-print-elements @@ -0,0 +1,7 @@ +scala> 1.to(200).toList +val res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200) + +scala>:settings -Vrepl-max-print-elements:20 + +scala> 1.to(300).toList +val res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) ... large output truncated, print value to show all