From 72298b838354e621b2fed61734d394d5befa0708 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 1 Jul 2013 03:55:27 -0700 Subject: SI-6419 Repl save session command A simple save command to write out the current replay stack. ``` scala> val i = 7 i: Int = 7 scala> val j= 8 j: Int = 8 scala> i * j res0: Int = 56 scala> :save multy.script scala> :q apm@mara:~/tmp$ cat multy.script val i = 7 val j= 8 i * j apm@mara:~/tmp$ skala Welcome to Scala version 2.11.0-20130626-204845-a83ca5bdf7 (OpenJDK 64-Bit Server VM, Java 1.7.0_21). Type in expressions to have them evaluated. Type :help for more information. scala> :load multy.script Loading multy.script... i: Int = 7 j: Int = 8 res0: Int = 56 scala> :load multy.script Loading multy.script... i: Int = 7 j: Int = 8 res1: Int = 56 ``` --- src/repl/scala/tools/nsc/interpreter/ILoop.scala | 7 +++++++ test/files/run/repl-save.check | 18 ++++++++++++++++++ test/files/run/repl-save.scala | 16 ++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 test/files/run/repl-save.check create mode 100644 test/files/run/repl-save.scala diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala index a84d076e76..392aac7c2e 100644 --- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala +++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala @@ -220,6 +220,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) nullary("quit", "exit the interpreter", () => Result(keepRunning = false, None)), nullary("replay", "reset execution and replay all previous commands", replay), nullary("reset", "reset the repl to its initial state, forgetting all session entries", resetCommand), + cmd("save", "", "save replayable session to a file", saveCommand), shCommand, nullary("silent", "disable/enable automatic printing of results", verbosity), cmd("type", "[-v] ", "display the type of an expression without evaluating it", typeCommand), @@ -470,6 +471,12 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter) Result(keepRunning = true, shouldReplay) } + def saveCommand(filename: String): Result = ( + if (filename.isEmpty) echo("File name is required.") + else if (replayCommandStack.isEmpty) echo("No replay commands in session") + else File(filename).printlnAll(replayCommands: _*) + ) + def addClasspath(arg: String): Unit = { val f = File(arg).normalize if (f.exists) { diff --git a/test/files/run/repl-save.check b/test/files/run/repl-save.check new file mode 100644 index 0000000000..7ed769278f --- /dev/null +++ b/test/files/run/repl-save.check @@ -0,0 +1,18 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> val i = 7 +i: Int = 7 + +scala> val j = 8 +j: Int = 8 + +scala> i * j +res0: Int = 56 + +scala> :save repl-save-run.obj/session.repl + +scala> +val i = 7 +val j = 8 +i * j diff --git a/test/files/run/repl-save.scala b/test/files/run/repl-save.scala new file mode 100644 index 0000000000..465aff225b --- /dev/null +++ b/test/files/run/repl-save.scala @@ -0,0 +1,16 @@ +import scala.tools.partest.ReplTest + +object Test extends ReplTest { + def code = s""" + |val i = 7 + |val j = 8 + |i * j + |:save $saveto + """.stripMargin.trim + def saveto = testOutput / "session.repl" + + override def show() = { + super.show() + Console print saveto.toFile.slurp + } +} -- cgit v1.2.3