Skip to content

Commit b82d865

Browse files
committed
REPL selectively trims prefix/suffix newline
ScalaRunTime.replStringOf adds extra newlines. Try not to trim more than was added. New API will not include the newlines.
1 parent abbbcef commit b82d865

File tree

11 files changed

+39
-21
lines changed

11 files changed

+39
-21
lines changed

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,29 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
5555
// We need to use the ScalaRunTime class coming from the scala-library
5656
// on the user classpath, and not the one available in the current
5757
// 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.
5960
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+
}
6381
}
6482
myClassLoader
6583
}
@@ -82,7 +100,7 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None) {
82100
resObj
83101
.getDeclaredMethods.find(_.getName == sym.name.encode.toString)
84102
.map(_.invoke(null))
85-
val string = value.map(replStringOf(_).trim)
103+
val string = value.map(replStringOf(_))
86104
if (!sym.is(Flags.Method) && sym.info == defn.UnitType)
87105
None
88106
else

compiler/test-resources/repl/i1369

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
scala> print("foo")
22
foo
33
scala> "Hello"
4-
val res0: String = Hello
4+
val res0: String = "Hello"

compiler/test-resources/repl/i3388

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
scala> val foo = "1"; foo.toInt
2-
val foo: String = 1
2+
val foo: String = "1"
33
val res0: Int = 1

compiler/test-resources/repl/i4852

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
scala> inline def foo[T](t : T*) : Any = t
22
def foo[T](t: T*): Any
33
scala> foo(1, "hi", false)
4-
val res0: Any = ArraySeq(1, hi, false)
4+
val res0: Any = ArraySeq(1, "hi", false)
55
scala> foo()
66
val res1: Any = ArraySeq()

compiler/test-resources/repl/i5218

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
scala> val tuple = (1, "2", 3L)
2-
val tuple: (Int, String, Long) = (1,2,3)
2+
val tuple: (Int, String, Long) = (1, "2", 3)
33
scala> 0.0 *: tuple
4-
val res0: (Double, Int, String, Long) = (0.0,1,2,3)
4+
val res0: (Double, Int, String, Long) = (0.0, 1, "2", 3)
55
scala> tuple ++ tuple
6-
val res1: Int *: scala.Tuple.Concat[(String, Long), tuple.type] = (1,2,3,1,2,3)
6+
val res1: Int *: scala.Tuple.Concat[(String, Long), tuple.type] = (1, "2", 3, 1, "2", 3)

compiler/test-resources/repl/i6474

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ scala> object Foo2 { type T[+A] = [B] =>> (A, B) }
55
scala> object Foo3 { type T[+A] = [B] =>> [C] =>> (A, B) }
66
// defined object Foo3
77
scala> ((1, 2): Foo1.T[Int]): Foo1.T[Any]
8-
val res0: (Any, Int) = (1,2)
8+
val res0: (Any, Int) = (1, 2)
99
scala> ((1, 2): Foo2.T[Int][Int]): Foo2.T[Any][Int]
10-
val res1: (Any, Int) = (1,2)
10+
val res1: (Any, Int) = (1, 2)
1111
scala> (1, 2): Foo3.T[Int][Int]
1212
1 | (1, 2): Foo3.T[Int][Int]
1313
| ^^^^^^^^^^^^^^^^
1414
| Missing type parameter for Foo3.T[Int][Int]
1515
scala> ((1, 2): Foo3.T[Int][Int][Int]): Foo3.T[Any][Int][Int]
16-
val res2: (Any, Int) = (1,2)
16+
val res2: (Any, Int) = (1, 2)
1717
scala> object Foo3 { type T[A] = [B] =>> [C] =>> (A, B) }
1818
// defined object Foo3
1919
scala> ((1, 2): Foo3.T[Int][Int][Int])
20-
val res3: (Int, Int) = (1,2)
20+
val res3: (Int, Int) = (1, 2)
2121
scala> ((1, 2): Foo3.T[Int][Int][Int])
22-
val res4: (Int, Int) = (1,2)
22+
val res4: (Int, Int) = (1, 2)

compiler/test-resources/repl/i6986

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
scala> enum SingleCase { case TheCase }
22
// defined class SingleCase
33
scala> SingleCase.TheCase
4-
val res0: SingleCase = TheCase
4+
val res0: SingleCase = TheCase()

compiler/test-resources/repl/i7410

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
scala> enum E { case A, B, C }
22
// defined class E
33
scala> E.A
4-
val res0: E = A
4+
val res0: E = A()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
scala> List()
22
val res0: List[Nothing] = List()
33
scala> Option.empty
4-
val res1: Option[Nothing] = None
4+
val res1: Option[Nothing] = None()
55
scala> Map()
66
val res2: Map[Nothing, Nothing] = Map()

compiler/test-resources/type-printer/prefixless

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
scala> List(1,2,3)
22
val res0: List[Int] = List(1, 2, 3)
33
scala> Map("foo" -> 1)
4-
val res1: Map[String, Int] = Map(foo -> 1)
4+
val res1: Map[String, Int] = Map("foo" -> 1)
55
scala> Seq('a','b')
66
val res2: Seq[Char] = List(a, b)
77
scala> Set(4, 5)

compiler/test-resources/type-printer/vals

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ val xs: List[Int] = List(1)
77
scala> scala.util.Try(1)
88
val res0: scala.util.Try[Int] = Success(1)
99
scala> Map(1 -> "one")
10-
val res1: Map[Int, String] = Map(1 -> one)
10+
val res1: Map[Int, String] = Map(1 -> "one")

0 commit comments

Comments
 (0)