@@ -55,11 +55,29 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
55
55
// We need to use the ScalaRunTime class coming from the scala-library
56
56
// on the user classpath, and not the one available in the current
57
57
// classloader, so we use reflection instead of simply calling
58
- // `ScalaRunTime.replStringOf`.
58
+ // `ScalaRunTime.replStringOf`. Probe for new API without extraneous newlines.
59
+ // For old API, try to clean up extraneous newlines by stripping suffix and maybe prefix newline.
59
60
val scalaRuntime = Class .forName(" scala.runtime.ScalaRunTime" , true , myClassLoader)
60
- val meth = scalaRuntime.getMethod(" replStringOf" , classOf [Object ], classOf [Int ])
61
-
62
- (value : Object ) => meth.invoke(null , value, Integer .valueOf(MaxStringElements )).asInstanceOf [String ]
61
+ try {
62
+ val meth = scalaRuntime.getMethod(" replStringOf" , classOf [Object ], classOf [Int ], classOf [Boolean ])
63
+ val truly = java.lang.Boolean .TRUE
64
+
65
+ (value : Object ) => meth.invoke(null , value, Integer .valueOf(MaxStringElements ), truly).asInstanceOf [String ]
66
+ } catch {
67
+ case _ : NoSuchMethodException =>
68
+ val meth = scalaRuntime.getMethod(" replStringOf" , classOf [Object ], classOf [Int ])
69
+
70
+ (value : Object ) => {
71
+ val res = meth.invoke(null , value, Integer .valueOf(MaxStringElements )).asInstanceOf [String ]
72
+ val len = res.length()
73
+ if len == 0 || res.charAt(len- 1 ) != '\n ' then
74
+ res
75
+ else if len == 1 || res.charAt(0 ) != '\n ' then
76
+ res.substring(0 , len- 1 )
77
+ else
78
+ res.substring(1 , len- 1 )
79
+ }
80
+ }
63
81
}
64
82
myClassLoader
65
83
}
@@ -82,7 +100,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
82
100
resObj
83
101
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
84
102
.map(_.invoke(null ))
85
- val string = value.map(replStringOf(_).trim )
103
+ val string = value.map(replStringOf(_))
86
104
if (! sym.is(Flags .Method ) && sym.info == defn.UnitType )
87
105
None
88
106
else
0 commit comments