diff options
author | Paul Phillips <paulp@improving.org> | 2010-01-16 21:52:44 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2010-01-16 21:52:44 +0000 |
commit | 3b00d9d7e54619b5273d442c27a628aa7c2a3bf3 (patch) | |
tree | 7f3302910c01cb156eb6f51083ce994eb2d48c54 | |
parent | d9b01e2c583dd4a99f3861bb41db50cd9f448700 (diff) | |
download | scala-3b00d9d7e54619b5273d442c27a628aa7c2a3bf3.tar.gz scala-3b00d9d7e54619b5273d442c27a628aa7c2a3bf3.tar.bz2 scala-3b00d9d7e54619b5273d442c27a628aa7c2a3bf3.zip |
New repl feature: you can start a line with .
on the previous result. For instance:
scala> (1 to 10).iterator
res0: Iterator[Int] = non-empty iterator
scala> .toList.sum
res1: Int = 55
-rw-r--r-- | src/compiler/scala/tools/nsc/Interpreter.scala | 21 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/InterpreterLoop.scala | 5 |
2 files changed, 22 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/Interpreter.scala b/src/compiler/scala/tools/nsc/Interpreter.scala index 1655c0130d..1185600c94 100644 --- a/src/compiler/scala/tools/nsc/Interpreter.scala +++ b/src/compiler/scala/tools/nsc/Interpreter.scala @@ -185,12 +185,17 @@ class Interpreter(val settings: Settings, out: PrintWriter) /** Generates names pre0, pre1, etc. via calls to apply method */ class NameCreator(pre: String) { private var x = -1 + var mostRecent: String = null + def apply(): String = { x += 1 val name = pre + x.toString // make sure we don't overwrite their unwisely named res3 etc. - if (allBoundNames exists (_.toString == name)) apply() - else name + mostRecent = + if (allBoundNames exists (_.toString == name)) apply() + else name + + mostRecent } def reset(): Unit = x = -1 def didGenerate(name: String) = @@ -868,6 +873,18 @@ class Interpreter(val settings: Settings, out: PrintWriter) } } + /** Returns the name of the most recent interpreter result. + * Mostly this exists so you can conveniently invoke methods on + * the previous result. + */ + def mostRecentVar: String = + prevRequests.last.handlers.last.member match { + case x: ValOrDefDef => x.name + case Assign(Ident(name), _) => name + case ModuleDef(_, name, _) => name + case _ => varNameCreator.mostRecent + } + private def requestForName(name: Name): Option[Request] = { for (req <- prevRequests.toList.reverse) { if (req.handlers.exists(_.boundNames contains name)) diff --git a/src/compiler/scala/tools/nsc/InterpreterLoop.scala b/src/compiler/scala/tools/nsc/InterpreterLoop.scala index 2b926d8e80..0f68f8fc5a 100644 --- a/src/compiler/scala/tools/nsc/InterpreterLoop.scala +++ b/src/compiler/scala/tools/nsc/InterpreterLoop.scala @@ -342,7 +342,7 @@ class InterpreterLoop(in0: Option[BufferedReader], out: PrintWriter) { def power() { powerUserOn = true - interpreter.powerUser() + out println interpreter.powerUser() interpreter.quietBind("history", "scala.collection.immutable.List[String]", historyList.toList) } @@ -387,7 +387,8 @@ class InterpreterLoop(in0: Option[BufferedReader], out: PrintWriter) { * to be recorded for replay, if any. */ def interpretStartingWith(code: String): Option[String] = - interpreter.interpret(code) match { + if (code startsWith ".") interpretStartingWith(interpreter.mostRecentVar + code) + else interpreter.interpret(code) match { case IR.Error => None case IR.Success => Some(code) case IR.Incomplete => |