summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSeth Tisue <seth@tisue.net>2015-10-14 17:20:58 +0200
committerSeth Tisue <seth@tisue.net>2015-10-14 17:20:58 +0200
commit68ce049c7a99f7c538d702182b46877806f54596 (patch)
treeb2115b39701dae3e7bcdd87ee89cc27c96951ced /src
parent9834fc8654077b8bf63237b7278fa48ed01e200d (diff)
parentf29096285f39dfea25c0644ed3066fb9630def78 (diff)
downloadscala-68ce049c7a99f7c538d702182b46877806f54596.tar.gz
scala-68ce049c7a99f7c538d702182b46877806f54596.tar.bz2
scala-68ce049c7a99f7c538d702182b46877806f54596.zip
Merge pull request #4771 from som-snytt/issue/9492-here
SI-9492 REPL paste here doc
Diffstat (limited to 'src')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ILoop.scala51
-rw-r--r--src/repl/scala/tools/nsc/interpreter/ReplProps.scala2
2 files changed, 37 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..bf7c8551e5 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,16 @@ 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 delimiter = eof orElse replProps.pasteDelimiter.option
+ val input = readWhile(s => delimiter.isEmpty || delimiter.get != s) mkString "\n"
+ val text = (
+ margin filter (_.nonEmpty) map {
+ case "-" => input.lines map (_.trim) mkString "\n"
+ case m => input stripMargin m.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
diff --git a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
index 588d92f81b..f3115d9800 100644
--- a/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
+++ b/src/repl/scala/tools/nsc/interpreter/ReplProps.scala
@@ -64,6 +64,8 @@ class ReplProps {
if (p.isSet) p.get else shellWelcomeString
}
+ val pasteDelimiter = Prop[String]("scala.repl.here")
+
/** CSV of paged,across to enable pagination or `-x` style
* columns, "across" instead of down the column. Since
* pagination turns off columnar output, these flags are