summaryrefslogtreecommitdiff
path: root/src/repl/scala/tools/nsc/interpreter/ILoop.scala
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2013-07-08 12:26:48 -0700
committerSom Snytt <som.snytt@gmail.com>2013-07-11 19:13:32 -0700
commite7468f3a1aaae7ac4a763226f0a2b9a7a375f493 (patch)
tree9b629bee12c6049a2d62107807bfadd922c88b34 /src/repl/scala/tools/nsc/interpreter/ILoop.scala
parent816a444f177b5f2bb90a2e89802d06c26f6a21ff (diff)
downloadscala-e7468f3a1aaae7ac4a763226f0a2b9a7a375f493.tar.gz
scala-e7468f3a1aaae7ac4a763226f0a2b9a7a375f493.tar.bz2
scala-e7468f3a1aaae7ac4a763226f0a2b9a7a375f493.zip
SI-4684 Repl supports raw paste
By special request, :paste -raw simply compiles the pasted code to the repl output dir. The -raw flag means no wrapping; the pasted code must be ordinary top level Scala code, not script.
Diffstat (limited to 'src/repl/scala/tools/nsc/interpreter/ILoop.scala')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ILoop.scala39
1 files changed, 30 insertions, 9 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
index aa65a858df..a623ee5055 100644
--- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
@@ -217,7 +217,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
cmd("javap", "<path|class>", "disassemble a file or class name", javapCommand),
cmd("line", "<id>|<line>", "place line(s) at the end of history", lineCommand),
cmd("load", "<path>", "interpret lines in a file", loadCommand),
- cmd("paste", "[path]", "enter paste mode or paste a file", pasteCommand),
+ cmd("paste", "[-raw] [path]", "enter paste mode or paste a file", pasteCommand),
nullary("power", "enable power user mode", powerCmd),
nullary("quit", "exit the interpreter", () => Result(keepRunning = false, None)),
nullary("replay", "reset execution and replay all previous commands", replay),
@@ -666,24 +666,38 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
def pasteCommand(arg: String): Result = {
var shouldReplay: Option[String] = None
- val code = (
- if (arg.nonEmpty) {
- withFile(arg)(f => {
+ def result = Result(keepRunning = true, shouldReplay)
+ val (raw, file) =
+ if (arg.isEmpty) (false, None)
+ else {
+ val r = """(-raw)?(\s+)?([^\-]\S*)?""".r
+ arg match {
+ case r(flag, sep, name) =>
+ if (flag != null && name != null && sep == null)
+ echo(s"""I assume you mean "$flag $name"?""")
+ (flag != null, Option(name))
+ case _ =>
+ echo("usage: :paste -raw file")
+ return result
+ }
+ }
+ val code = file match {
+ case Some(name) =>
+ withFile(name)(f => {
shouldReplay = Some(s":paste $arg")
val s = f.slurp.trim
if (s.isEmpty) echo(s"File contains no code: $f")
else echo(s"Pasting file $f...")
s
}) getOrElse ""
- } else {
+ case None =>
echo("// Entering paste mode (ctrl-D to finish)\n")
val text = (readWhile(_ => true) mkString "\n").trim
if (text.isEmpty) echo("\n// Nothing pasted, nothing gained.\n")
else echo("\n// Exiting paste mode, now interpreting.\n")
text
- }
- )
- if (code.nonEmpty) {
+ }
+ def interpretCode() = {
val res = intp interpret code
// if input is incomplete, let the compiler try to say why
if (res == IR.Incomplete) {
@@ -693,7 +707,14 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
if (errless) echo("...but compilation found no error? Good luck with that.")
}
}
- Result(keepRunning = true, shouldReplay)
+ def compileCode() = {
+ val errless = intp compileSources new BatchSourceFile("<pastie>", code)
+ if (!errless) echo("There were compilation errors!")
+ }
+ if (code.nonEmpty) {
+ if (raw) compileCode() else interpretCode()
+ }
+ result
}
private object paste extends Pasted {