summaryrefslogtreecommitdiff
path: root/src/repl/scala/tools/nsc/interpreter/ILoop.scala
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2015-09-27 18:52:23 -0700
committerSom Snytt <som.snytt@gmail.com>2015-09-27 18:52:23 -0700
commitbc3589d3ebab7735fbbb130fbb7c8ccb1146eef7 (patch)
treed60802e1dc337d87e8098f8bd836147a09e33ab7 /src/repl/scala/tools/nsc/interpreter/ILoop.scala
parent27da46343cd545534819300235bc64ab74958c92 (diff)
downloadscala-bc3589d3ebab7735fbbb130fbb7c8ccb1146eef7.tar.gz
scala-bc3589d3ebab7735fbbb130fbb7c8ccb1146eef7.tar.bz2
scala-bc3589d3ebab7735fbbb130fbb7c8ccb1146eef7.zip
SI-9492 REPL paste here doc
Simple here documentish syntax for REPL paste. This makes it easier to paste a block of script (as opposed to transcript). It also means you won't accidentally ctl-D out of the REPL and then out of SBT and then out of the terminal window. ``` scala> :paste < EOF // Entering paste mode (EOF to finish) class C { def c = 42 } EOF // Exiting paste mode, now interpreting. defined class C scala> new C().c res0: Int = 42 scala> :paste <| EOF // Entering paste mode (EOF to finish) |class D { def d = 42 } EOF // Exiting paste mode, now interpreting. defined class D scala> new D().d res1: Int = 42 scala> :quit ```
Diffstat (limited to 'src/repl/scala/tools/nsc/interpreter/ILoop.scala')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ILoop.scala48
1 files changed, 32 insertions, 16 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/ILoop.scala b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
index f844029b64..b4fa5b607c 100644
--- a/src/repl/scala/tools/nsc/interpreter/ILoop.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ILoop.scala
@@ -694,25 +694,37 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
Iterator continually in.readLine("") takeWhile (x => x != null && cond(x))
}
+ /* :paste -raw file
+ * or
+ * :paste < EOF
+ * your code
+ * EOF
+ * :paste <~ EOF
+ * ~your code
+ * EOF
+ */
def pasteCommand(arg: String): Result = {
var shouldReplay: Option[String] = None
def result = Result(keepRunning = true, shouldReplay)
- val (raw, file) =
- if (arg.isEmpty) (false, None)
+ val (raw, file, margin) =
+ if (arg.isEmpty) (false, None, 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
+ def maybeRaw(ss: List[String]) = if (ss.nonEmpty && ss.head == "-raw") (true, ss.tail) else (false, ss)
+ def maybeHere(ss: List[String]) =
+ if (ss.nonEmpty && ss.head.startsWith("<")) (ss.head.dropWhile(_ == '<'), ss.tail)
+ else (null, ss)
+
+ val (raw0, ss0) = maybeRaw(words(arg))
+ val (margin0, ss1) = maybeHere(ss0)
+ val file0 = ss1 match {
+ case Nil => null
+ case x :: Nil => x
+ case _ => echo("usage: :paste [-raw] file | < EOF") ; return result
}
+ (raw0, Option(file0), Option(margin0))
}
- val code = file match {
- case Some(name) =>
+ val code = (file, margin) match {
+ case (Some(name), None) =>
withFile(name) { f =>
shouldReplay = Some(s":paste $arg")
val s = f.slurp.trim
@@ -720,9 +732,13 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
else echo(s"Pasting file $f...")
s
} getOrElse ""
- case None =>
- echo("// Entering paste mode (ctrl-D to finish)\n")
- val text = (readWhile(_ => true) mkString "\n").trim
+ case (eof, _) =>
+ echo(s"// Entering paste mode (${ eof getOrElse "ctrl-D" } to finish)\n")
+ val input = readWhile(s => eof.isEmpty || eof.get != s) mkString "\n"
+ val text = (
+ margin filter (_.nonEmpty) map (input stripMargin _.head) // ignore excess chars in "<<||"
+ getOrElse input
+ ).trim
if (text.isEmpty) echo("\n// Nothing pasted, nothing gained.\n")
else echo("\n// Exiting paste mode, now interpreting.\n")
text