@@ -49,36 +49,40 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
49
49
// We need to use the ScalaRunTime class coming from the scala-library
50
50
// on the user classpath, and not the one available in the current
51
51
// classloader, so we use reflection instead of simply calling
52
- // `ScalaRunTime.replStringOf`. Probe for new API without extraneous newlines.
53
- // For old API, try to clean up extraneous newlines by stripping suffix and maybe prefix newline.
52
+ // `ScalaRunTime.stringOf`. Also probe for new stringOf that does string quoting, etc.
54
53
val scalaRuntime = Class .forName(" scala.runtime.ScalaRunTime" , true , myClassLoader)
55
54
val renderer = " stringOf"
56
- def stringOfMaybeTruncated (value : Object , maxElements : Int ): String = {
57
- try {
58
- val meth = scalaRuntime.getMethod(renderer, classOf [Object ], classOf [Int ], classOf [Boolean ])
59
- val truly = java.lang.Boolean .TRUE
60
- meth.invoke(null , value, maxElements, truly).asInstanceOf [String ]
61
- } catch {
62
- case _ : NoSuchMethodException =>
63
- val meth = scalaRuntime.getMethod(renderer, classOf [Object ], classOf [Int ])
64
- meth.invoke(null , value, maxElements).asInstanceOf [String ]
65
- }
66
- }
67
-
68
- (value : Object , maxElements : Int , maxCharacters : Int ) => {
69
- // `ScalaRuntime.stringOf` may truncate the output, in which case we want to indicate that fact to the user
70
- // In order to figure out if it did get truncated, we invoke it twice - once with the `maxElements` that we
71
- // want to print, and once without a limit. If the first is shorter, truncation did occur.
72
- val notTruncated = stringOfMaybeTruncated(value, Int .MaxValue )
73
- val maybeTruncatedByElementCount = stringOfMaybeTruncated(value, maxElements)
74
- val maybeTruncated = truncate(maybeTruncatedByElementCount, maxCharacters)
75
-
76
- // our string representation may have been truncated by element and/or character count
77
- // if so, append an info string - but only once
78
- if (notTruncated.length == maybeTruncated.length) maybeTruncated
79
- else s " $maybeTruncated ... large output truncated, print value to show all "
80
- }
81
-
55
+ val stringOfInvoker : (Object , Int ) => String =
56
+ def richStringOf : (Object , Int ) => String =
57
+ val method = scalaRuntime.getMethod(renderer, classOf [Object ], classOf [Int ], classOf [Boolean ])
58
+ val richly = java.lang.Boolean .TRUE // add a repl option for enriched output
59
+ (value, maxElements) => method.invoke(null , value, maxElements, richly).asInstanceOf [String ]
60
+ def poorStringOf : (Object , Int ) => String =
61
+ try
62
+ val method = scalaRuntime.getMethod(renderer, classOf [Object ], classOf [Int ])
63
+ (value, maxElements) => method.invoke(null , value, maxElements).asInstanceOf [String ]
64
+ catch case _ : NoSuchMethodException => (value, maxElements) => String .valueOf(value).take(maxElements)
65
+ try richStringOf
66
+ catch case _ : NoSuchMethodException => poorStringOf
67
+ def stringOfMaybeTruncated (value : Object , maxElements : Int ): String = stringOfInvoker(value, maxElements)
68
+
69
+ // require value != null
70
+ // `ScalaRuntime.stringOf` returns null iff value.toString == null, let caller handle that.
71
+ // `ScalaRuntime.stringOf` may truncate the output, in which case we want to indicate that fact to the user
72
+ // In order to figure out if it did get truncated, we invoke it twice - once with the `maxElements` that we
73
+ // want to print, and once without a limit. If the first is shorter, truncation did occur.
74
+ // Note that `stringOf` has new API in flight to handle truncation, see stringOfMaybeTruncated.
75
+ (value : Object , maxElements : Int , maxCharacters : Int ) =>
76
+ stringOfMaybeTruncated(value, Int .MaxValue ) match
77
+ case null => null
78
+ case notTruncated =>
79
+ val maybeTruncated =
80
+ val maybeTruncatedByElementCount = stringOfMaybeTruncated(value, maxElements)
81
+ truncate(maybeTruncatedByElementCount, maxCharacters)
82
+ // our string representation may have been truncated by element and/or character count
83
+ // if so, append an info string - but only once
84
+ if notTruncated.length == maybeTruncated.length then maybeTruncated
85
+ else s " $maybeTruncated ... large output truncated, print value to show all "
82
86
}
83
87
myClassLoader
84
88
}
@@ -89,13 +93,18 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
89
93
else str.substring(0 , str.offsetByCodePoints(0 , maxPrintCharacters - 1 ))
90
94
91
95
/** Return a String representation of a value we got from `classLoader()`. */
92
- private [repl] def replStringOf (value : Object )(using Context ): String =
96
+ private [repl] def replStringOf (sym : Symbol , value : Object )(using Context ): String =
93
97
assert(myReplStringOf != null ,
94
98
" replStringOf should only be called on values creating using `classLoader()`, but `classLoader()` has not been called so far" )
95
99
val maxPrintElements = ctx.settings.VreplMaxPrintElements .valueIn(ctx.settingsState)
96
100
val maxPrintCharacters = ctx.settings.VreplMaxPrintCharacters .valueIn(ctx.settingsState)
97
- val res = myReplStringOf(value, maxPrintElements, maxPrintCharacters)
98
- if res == null then " null // non-null reference has null-valued toString" else res
101
+ // stringOf returns null if value.toString returns null. Show some text as a fallback.
102
+ def fallback = s """ null // result of " ${sym.name}.toString" is null """
103
+ if value == null then " null" else
104
+ myReplStringOf(value, maxPrintElements, maxPrintCharacters) match
105
+ case null => fallback
106
+ case res => res
107
+ end if
99
108
100
109
/** Load the value of the symbol using reflection.
101
110
*
@@ -107,17 +116,15 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):
107
116
val symValue = resObj
108
117
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
109
118
.flatMap(result => rewrapValueClass(sym.info.classSymbol, result.invoke(null )))
110
- val valueString = symValue.map(replStringOf)
119
+ symValue
120
+ .filter(_ => sym.is(Flags .Method ) || sym.info != defn.UnitType )
121
+ .map(value => stripReplPrefix(replStringOf(sym, value)))
111
122
112
- if (! sym.is(Flags .Method ) && sym.info == defn.UnitType )
113
- None
123
+ private def stripReplPrefix (s : String ): String =
124
+ if (s.startsWith(REPL_WRAPPER_NAME_PREFIX ))
125
+ s.drop(REPL_WRAPPER_NAME_PREFIX .length).dropWhile(c => c.isDigit || c == '$' )
114
126
else
115
- valueString.map { s =>
116
- if (s.startsWith(REPL_WRAPPER_NAME_PREFIX ))
117
- s.drop(REPL_WRAPPER_NAME_PREFIX .length).dropWhile(c => c.isDigit || c == '$' )
118
- else
119
- s
120
- }
127
+ s
121
128
122
129
/** Rewrap value class to their Wrapper class
123
130
*
0 commit comments