Skip to content

Commit 3c989a1

Browse files
authored
simplify rendering: we don't need reflection any longer, apparently (#57)
* I guess we only needed that while we used the stock repl renderer... * refactor: simplify, use Option for Optional value - reflection made that difficult previously
1 parent aed0643 commit 3c989a1

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@ println(bar) //1
117117

118118
Unlike the stock Scala REPL, scala-repl-pp does _not_ truncate the output by default. You can optionally specify the maxHeight parameter though:
119119
```
120+
120121
./scala-repl-pp --maxHeight 5
121-
scala> (1 to 100000).toSeq
122-
val res0: scala.collection.immutable.Range.Inclusive = Range(
123-
1,
124-
2,
125-
3,
126-
...
122+
123+
scala> (1 to 100000).map(_.toString).toSeq
124+
val res0: IndexedSeq[String] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9,
125+
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
126+
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
127+
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
128+
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
129+
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
130+
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 1...
127131
```
128132

129133
## Scripting

core/src/main/scala/replpp/PPrinter.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import scala.util.matching.Regex
55

66
object PPrinter {
77
private var pprinter: pprint.PPrinter = null
8-
private var maxHeight: Int = Int.MaxValue
8+
private var maxHeight: Option[Int] = None
99

10-
def apply(objectToRender: Object, maxHeight: Int = Int.MaxValue): String = {
10+
def apply(objectToRender: Object, maxHeight: Option[Int] = None): String = {
1111
val _pprinter = this.synchronized {
1212
// initialise on first use and whenever the maxHeight setting changed
1313
if (pprinter == null || this.maxHeight != maxHeight) {
@@ -19,9 +19,9 @@ object PPrinter {
1919
_pprinter.apply(objectToRender).render
2020
}
2121

22-
private def create(maxHeight: Int): pprint.PPrinter = {
22+
private def create(maxHeight: Option[Int] = None): pprint.PPrinter = {
2323
new pprint.PPrinter(
24-
defaultHeight = maxHeight,
24+
defaultHeight = maxHeight.getOrElse(Int.MaxValue),
2525
colorLiteral = fansi.Attrs.Empty, // leave color highlighting to the repl
2626
colorApplyPrefix = fansi.Attrs.Empty) {
2727

core/src/main/scala/replpp/ReplDriver.scala

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ class ReplDriver(args: Array[String],
3737
val terminal = new JLineTerminal {
3838
override protected def promptStr = prompt
3939
}
40-
initializeRenderer()
40+
41+
// configure rendering to use our pprinter for displaying results
42+
rendering.myReplStringOf = (objectToRender: Object, maxElements: Int, maxCharacters: Int) =>
43+
PPrinter.apply(objectToRender, maxHeight)
44+
4145
greeting.foreach(out.println)
4246

4347
@tailrec
@@ -117,18 +121,4 @@ class ReplDriver(args: Array[String],
117121
private def parseInput(input: String, state: State): ParseResult =
118122
ParseResult(input)(using state)
119123

120-
/** configure rendering to use our pprinter for displaying results */
121-
private def initializeRenderer() = {
122-
rendering.myReplStringOf = {
123-
// We need to use the PPrinter class from the on the user classpath, and not the one available in the current
124-
// classloader, so we use reflection instead of simply calling `replpp.PPrinter:apply`.
125-
// This is analogous to what happens in dotty.tools.repl.Rendering.
126-
val pprinter = Class.forName("replpp.PPrinter", true, rendering.myClassLoader)
127-
val renderingMethod = pprinter.getMethod("apply", classOf[Object], classOf[Int])
128-
(objectToRender: Object, maxElements: Int, maxCharacters: Int) => {
129-
renderingMethod.invoke(null, objectToRender, maxHeight.getOrElse(Int.MaxValue)).asInstanceOf[String]
130-
}
131-
}
132-
}
133-
134124
}

0 commit comments

Comments
 (0)