summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/util
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-08-01 22:45:12 +0000
committerPaul Phillips <paulp@improving.org>2011-08-01 22:45:12 +0000
commit60ee9924b7449ec64cffcecd6accd1a856c4fa3a (patch)
treecbf4fb3bdcae173197389b2a88d45ae64b45e1fe /src/compiler/scala/tools/nsc/util
parent257b6c91a52dc805dfb413b323a70e52f6499c2e (diff)
downloadscala-60ee9924b7449ec64cffcecd6accd1a856c4fa3a.tar.gz
scala-60ee9924b7449ec64cffcecd6accd1a856c4fa3a.tar.bz2
scala-60ee9924b7449ec64cffcecd6accd1a856c4fa3a.zip
Tired of ugly-printing in the repl, I sort of f...
Tired of ugly-printing in the repl, I sort of finished some old code for pretty printing token streams. It is at least a lot prettier than it once was, and I threw in some power mode helpers. Now you can do this. % scala -Dscala.repl.power Welcome to Scala version 2.10.0.r25427-b20110801144412 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). // .u turns a string into an URL like .r does into a regex, and .pp pretty prints the url scala> "https://raw.github.com/scalaz/scalaz/master/example/src/main/scala/scal az/example/ExampleIteratee.scala".u.pp package scalaz.example object ExampleIteratee { def main (args: Array[String]) = run import scalaz._ import Scalaz._ import IterV._ [etc it's all there in real life] } No review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/util')
-rw-r--r--src/compiler/scala/tools/nsc/util/Indenter.scala85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/nsc/util/Indenter.scala b/src/compiler/scala/tools/nsc/util/Indenter.scala
new file mode 100644
index 0000000000..f9ddc4a194
--- /dev/null
+++ b/src/compiler/scala/tools/nsc/util/Indenter.scala
@@ -0,0 +1,85 @@
+package scala.tools.nsc
+package util
+
+import java.io.PrintStream
+
+class Indenter(var stringFn: Any => String) {
+ def this() = this("" + _)
+ def out: PrintStream = System.out
+
+ var indentSpaces = 2
+ var isSorted = false
+ var openString = ""
+ var closeString = ""
+
+ def braces: this.type = {
+ openString = " {"
+ closeString = "}"
+ this
+ }
+ def sorted: this.type = { isSorted = true ; this }
+ def stringify(fn: Any => String): this.type = {
+ stringFn = fn
+ this
+ }
+
+ def atStartOfLine = startOfLine
+ private var indentLevel = 0
+ private var startOfLine = true
+ def indent: this.type = { indentLevel += 1 ; this }
+ def undent: this.type = { indentLevel -= 1 ; this }
+ def currentIndent = " " * indentLevel * indentSpaces
+ def printIndent() = {
+ out.print(currentIndent)
+ startOfLine = true
+ }
+
+ // Execute the given body indented one level further.
+ def >>[T](body: => T): T = {
+ indentLevel += 1
+ try body
+ finally indentLevel -= 1
+ }
+
+ def openIndent(token: Any) {
+ print(token + "\n")
+ indent
+ printIndent()
+ }
+ def closeIndent(token: Any) {
+ print("\n")
+ undent
+ printIndent()
+ print(token)
+ }
+ def finishLine(token: Any) {
+ print(token)
+ printIndent()
+ }
+ def nextIndent(endOfLine: Any) = finishLine(endOfLine)
+
+ def block(label: String)(body: => Unit) {
+ if (label != "" || openString != "")
+ pp(label + openString)
+
+ this >> body
+
+ if (closeString != "")
+ pp(closeString)
+ }
+ def print(x: Any) = {
+ out print stringFn(x)
+ out.flush()
+ startOfLine = false
+ }
+ def pps(xs: TraversableOnce[Any]) {
+ if (isSorted) xs.toSeq.sortBy("" + _) foreach pp
+ else xs foreach pp
+ }
+ def pp(x: Any) {
+ printIndent()
+ out println stringFn(x)
+ out.flush()
+ startOfLine = false
+ }
+}