Skip to content

Commit c28c16e

Browse files
authored
REPL: make truncation behaviour configurable via -Vrepl-max-print-elements (#16011)
2 parents 731522a + 253143b commit c28c16e

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private sealed trait VerboseSettings:
149149
val Vprofile: Setting[Boolean] = BooleanSetting("-Vprofile", "Show metrics about sources and internal representations to estimate compile-time complexity.")
150150
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"), "")
151151
val VprofileDetails = IntSetting("-Vprofile-details", "Show metrics about sources and internal representations of the most complex methods", 0)
152+
val VreplMaxPrintElements: Setting[Int] = IntSetting("-Vrepl-max-print-elements", "Number of elements to be printed before output is truncated.", 1000)
152153

153154
/** -W "Warnings" settings
154155
*/

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
3232

3333
private var myClassLoader: AbstractFileClassLoader = _
3434

35-
private var myReplStringOf: Object => String = _
35+
/** (value, maxElements) => String */
36+
private var myReplStringOf: (Object, Int) => String = _
3637

38+
/** info to add if output got truncated */
39+
private val infoOutputGotTruncated = " ... large output truncated, print value to show all"
3740

3841
/** Class loader used to load compiled code */
3942
private[repl] def classLoader()(using Context) =
@@ -60,17 +63,28 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
6063
// For old API, try to clean up extraneous newlines by stripping suffix and maybe prefix newline.
6164
val scalaRuntime = Class.forName("scala.runtime.ScalaRunTime", true, myClassLoader)
6265
val renderer = "stringOf" // was: replStringOf
63-
try {
64-
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean])
65-
val truly = java.lang.Boolean.TRUE
66-
67-
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements), truly).asInstanceOf[String]
68-
} catch {
69-
case _: NoSuchMethodException =>
70-
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int])
66+
def stringOfMaybeTruncated(value: Object, maxElements: Int): String = {
67+
try {
68+
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean])
69+
val truly = java.lang.Boolean.TRUE
70+
meth.invoke(null, value, maxElements, truly).asInstanceOf[String]
71+
} catch {
72+
case _: NoSuchMethodException =>
73+
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int])
74+
meth.invoke(null, value, maxElements).asInstanceOf[String]
75+
}
76+
}
7177

72-
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements)).asInstanceOf[String]
78+
(value: Object, maxElements: Int) => {
79+
// `ScalaRuntime.stringOf` may truncate the output, in which case we want to indicate that fact to the user
80+
// In order to figure out if it did get truncated, we invoke it twice - once with the `maxElements` that we
81+
// want to print, and once without a limit. If the first is shorter, truncation did occur.
82+
val maybeTruncated = stringOfMaybeTruncated(value, maxElements)
83+
val notTruncated = stringOfMaybeTruncated(value, Int.MaxValue)
84+
if (maybeTruncated.length == notTruncated.length) maybeTruncated
85+
else maybeTruncated + infoOutputGotTruncated
7386
}
87+
7488
}
7589
myClassLoader
7690
}
@@ -82,16 +96,16 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
8296
* https://github.com/scala/bug/issues/12337
8397
*/
8498
private[repl] def truncate(str: String): String =
85-
val showTruncated = " ... large output truncated, print value to show all"
8699
val ncp = str.codePointCount(0, str.length) // to not cut inside code point
87100
if ncp <= MaxStringElements then str
88-
else str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 1)) + showTruncated
101+
else str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 1)) + infoOutputGotTruncated
89102

90103
/** Return a String representation of a value we got from `classLoader()`. */
91104
private[repl] def replStringOf(value: Object)(using Context): String =
92105
assert(myReplStringOf != null,
93106
"replStringOf should only be called on values creating using `classLoader()`, but `classLoader()` has not been called so far")
94-
val res = myReplStringOf(value)
107+
val maxPrintElements = ctx.settings.VreplMaxPrintElements.valueIn(ctx.settingsState)
108+
val res = myReplStringOf(value, maxPrintElements)
95109
if res == null then "null // non-null reference has null-valued toString" else truncate(res)
96110

97111
/** Load the value of the symbol using reflection.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
scala> 1
2+
val res0: Int = 1
3+
4+
scala>:settings -Xrepl-disable-display
5+
6+
scala> 2
7+
8+
scala>:reset
9+
Resetting REPL state.
10+
11+
scala> 3
12+
val res0: Int = 3
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
scala> 1.to(200).toList
2+
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)
3+
4+
scala>:settings -Vrepl-max-print-elements:20
5+
6+
scala> 1.to(300).toList
7+
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

0 commit comments

Comments
 (0)