Skip to content

Commit 92faa2c

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 cd8080b commit 92faa2c

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
@@ -221,6 +221,7 @@ private sealed trait XSettings:
221221
val XprintSuspension: Setting[Boolean] = BooleanSetting("-Xprint-suspension", "Show when code is suspended until macros are compiled.")
222222
val Xprompt: Setting[Boolean] = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).")
223223
val XreplDisableDisplay: Setting[Boolean] = BooleanSetting("-Xrepl-disable-display", "Do not display definitions in REPL.")
224+
val XreplMaxPrintElements: Setting[Int] = IntSetting("-Xrepl-max-print-elements", "Number of elements to be printed before output is truncated.", 1000)
224225
val XverifySignatures: Setting[Boolean] = BooleanSetting("-Xverify-signatures", "Verify generic signatures in generated bytecode.")
225226
val XignoreScala2Macros: Setting[Boolean] = BooleanSetting("-Xignore-scala2-macros", "Ignore errors when compiling code that calls Scala2 macros, these will fail at runtime.")
226227
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
@@ -23,17 +23,15 @@ import dotc.reporting.Diagnostic
2323
* `ReplDriver#resetToInitial` is called, the accompanying instance of
2424
* `Rendering` is no longer valid.
2525
*/
26-
private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
26+
private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None,
27+
maxPrintElements: Int = 1000) {
2728

2829
import Rendering._
2930

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

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

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

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

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

9088
/** Return a String representation of a value we got from `classLoader()`. */

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)