Skip to content

Commit 467d867

Browse files
committed
REPL: make truncation behaviour configurable
via `-Xrepl-max-print-elements`. The default behaviour is unchanged: ``` scala> 1.to(300).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, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 22 ... large output truncated, print value to show all ``` The user can now configure a higher threshold before truncating kicks in: ``` scala -Xrepl-max-print-elements:2000 scala> 1.to(300).toList // prints the entire list ```
1 parent d54157d commit 467d867

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ private sealed trait XSettings:
231231
val XprintSuspension: Setting[Boolean] = BooleanSetting("-Xprint-suspension", "Show when code is suspended until macros are compiled.")
232232
val Xprompt: Setting[Boolean] = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).")
233233
val XreplDisableDisplay: Setting[Boolean] = BooleanSetting("-Xrepl-disable-display", "Do not display definitions in REPL.")
234+
val XreplMaxPrintElements: Setting[Int] = IntSetting("-Xrepl-max-print-elements", "Number of elements to be printed before output is truncated.", 1000)
234235
val XverifySignatures: Setting[Boolean] = BooleanSetting("-Xverify-signatures", "Verify generic signatures in generated bytecode.")
235236
val XignoreScala2Macros: Setting[Boolean] = BooleanSetting("-Xignore-scala2-macros", "Ignore errors when compiling code that calls Scala2 macros, these will fail at runtime.")
236237
val XimportSuggestionTimeout: Setting[Int] = IntSetting("-Ximport-suggestion-timeout", "Timeout (in ms) for searching for import suggestions when errors are reported.", 8000)

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ import dotc.transform.ValueClasses
2424
* `ReplDriver#resetToInitial` is called, the accompanying instance of
2525
* `Rendering` is no longer valid.
2626
*/
27-
private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
27+
private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None,
28+
maxPrintElements: Int = 1000):
2829

2930
import Rendering._
3031

31-
private val MaxStringElements: Int = 1000 // no need to mkString billions of elements
32-
3332
private var myClassLoader: AbstractFileClassLoader = _
3433

3534
private var myReplStringOf: Object => String = _
3635

37-
3836
/** Class loader used to load compiled code */
3937
private[repl] def classLoader()(using Context) =
4038
if (myClassLoader != null && myClassLoader.root == ctx.settings.outputDir.value) myClassLoader
@@ -64,12 +62,12 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
6462
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int], classOf[Boolean])
6563
val truly = java.lang.Boolean.TRUE
6664

67-
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements), truly).asInstanceOf[String]
65+
(value: Object) => meth.invoke(null, value, Integer.valueOf(maxPrintElements), truly).asInstanceOf[String]
6866
} catch {
6967
case _: NoSuchMethodException =>
7068
val meth = scalaRuntime.getMethod(renderer, classOf[Object], classOf[Int])
7169

72-
(value: Object) => meth.invoke(null, value, Integer.valueOf(MaxStringElements)).asInstanceOf[String]
70+
(value: Object) => meth.invoke(null, value, Integer.valueOf(maxPrintElements)).asInstanceOf[String]
7371
}
7472
}
7573
myClassLoader
@@ -84,8 +82,8 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
8482
private[repl] def truncate(str: String): String =
8583
val showTruncated = " ... large output truncated, print value to show all"
8684
val ncp = str.codePointCount(0, str.length) // to not cut inside code point
87-
if ncp <= MaxStringElements then str
88-
else str.substring(0, str.offsetByCodePoints(0, MaxStringElements - 1)) + showTruncated
85+
if ncp <= maxPrintElements then str
86+
else str.substring(0, str.offsetByCodePoints(0, maxPrintElements - 1)) + showTruncated
8987

9088
/** Return a String representation of a value we got from `classLoader()`. */
9189
private[repl] def replStringOf(value: Object)(using Context): String =

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ class ReplDriver(settings: Array[String],
111111
if (rootCtx.settings.outputDir.isDefault(using rootCtx))
112112
rootCtx = rootCtx.fresh
113113
.setSetting(rootCtx.settings.outputDir, new VirtualDirectory("<REPL compilation output>"))
114+
114115
compiler = new ReplCompiler
115-
rendering = new Rendering(classLoader)
116+
rendering = new Rendering(
117+
classLoader,
118+
maxPrintElements = rootCtx.settings.XreplMaxPrintElements.valueIn(rootCtx.settingsState)
119+
)
116120
}
117121

118122
private var rootCtx: Context = _

0 commit comments

Comments
 (0)