From 57670a38ca55cc04c9d765bdf04584cad5581d41 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Wed, 27 Apr 2016 13:38:38 +0200 Subject: Stop interpreter from interpreting twice on enter --- src/dotty/tools/dotc/repl/AmmoniteReader.scala | 4 ++-- .../tools/dotc/repl/CompilingInterpreter.scala | 25 +++++++++++++++++++++- src/dotty/tools/dotc/repl/Interpreter.scala | 6 ++++++ src/dotty/tools/dotc/repl/InterpreterLoop.scala | 9 ++++++-- 4 files changed, 39 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/repl/AmmoniteReader.scala b/src/dotty/tools/dotc/repl/AmmoniteReader.scala index a3b2a1c56..0a49b8ea3 100644 --- a/src/dotty/tools/dotc/repl/AmmoniteReader.scala +++ b/src/dotty/tools/dotc/repl/AmmoniteReader.scala @@ -15,9 +15,9 @@ class AmmoniteReader(val interpreter: Interpreter)(implicit ctx: Context) extend val interactive = true def incompleteInput(str: String): Boolean = - interpreter.beQuietDuring(interpreter.interpret(str)) match { + interpreter.delayOutputDuring(interpreter.interpret(str)) match { case Interpreter.Incomplete => true - case _ => false // TODO: should perhaps save output here? + case _ => false } val reader = new java.io.InputStreamReader(System.in) diff --git a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index cfcce106e..d322aa404 100644 --- a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -78,6 +78,27 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit /** whether to print out result lines */ private var printResults: Boolean = true + private var delayOutput: Boolean = false + + var previousOutput: String = null + + override def lastOutput() = + if (previousOutput == null) None + else { + val ret = Some(previousOutput) + previousOutput = null + ret + } + + override def delayOutputDuring[T](operation: => T): T = { + val old = delayOutput + try { + delayOutput = true + operation + } finally { + delayOutput = old + } + } /** Temporarily be quiet */ override def beQuietDuring[T](operation: => T): T = { @@ -188,7 +209,9 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit Interpreter.Error // an error happened during compilation, e.g. a type error else { val (interpreterResultString, succeeded) = req.loadAndRun() - if (printResults || !succeeded) + if (delayOutput) + previousOutput = clean(interpreterResultString) + else if (printResults || !succeeded) out.print(clean(interpreterResultString)) if (succeeded) { prevRequests += req diff --git a/src/dotty/tools/dotc/repl/Interpreter.scala b/src/dotty/tools/dotc/repl/Interpreter.scala index ea587a097..590baae0d 100644 --- a/src/dotty/tools/dotc/repl/Interpreter.scala +++ b/src/dotty/tools/dotc/repl/Interpreter.scala @@ -33,4 +33,10 @@ trait Interpreter { /** Suppress output during evaluation of `operation`. */ def beQuietDuring[T](operation: => T): T + + /** Suppresses output and saves it for `lastOutput` to collect */ + def delayOutputDuring[T](operation: => T): T + + /** Gets the last output not printed immediately */ + def lastOutput(): Option[String] } diff --git a/src/dotty/tools/dotc/repl/InterpreterLoop.scala b/src/dotty/tools/dotc/repl/InterpreterLoop.scala index 64414fec3..53fd09c07 100644 --- a/src/dotty/tools/dotc/repl/InterpreterLoop.scala +++ b/src/dotty/tools/dotc/repl/InterpreterLoop.scala @@ -18,7 +18,7 @@ import scala.concurrent.ExecutionContext.Implicits.global * After instantiation, clients should call the `run` method. * * @author Moez A. Abdel-Gawad - * @author Lex Spoon + * @author Lex Spoon * @author Martin Odersky */ class InterpreterLoop(compiler: Compiler, config: REPL.Config)(implicit ctx: Context) { @@ -167,7 +167,12 @@ class InterpreterLoop(compiler: Compiler, config: REPL.Config)(implicit ctx: Con else if (line startsWith ":") output.println("Unknown command. Type :help for help.") else - shouldReplay = interpretStartingWith(line) + shouldReplay = interpreter.lastOutput() match { // don't interpret twice + case Some(oldRes) => + output.print(oldRes) + Some(line) + case _ => interpretStartingWith(line) + } (true, shouldReplay) } -- cgit v1.2.3