diff options
-rw-r--r-- | src/dotty/tools/dotc/repl/CompilingInterpreter.scala | 24 |
1 files changed, 18 insertions, 6 deletions
diff --git a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala index b3b7ab13c..10a9038a1 100644 --- a/src/dotty/tools/dotc/repl/CompilingInterpreter.scala +++ b/src/dotty/tools/dotc/repl/CompilingInterpreter.scala @@ -24,6 +24,7 @@ import dotty.tools.backend.jvm.GenBCode import Symbols._, Types._, Contexts._, StdNames._, Names._, NameOps._ import Decorators._ import scala.util.control.NonFatal +import printing.SyntaxHighlighting /** An interpreter for Scala code which is based on the `dotc` compiler. * @@ -210,11 +211,11 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit if (!req.compile()) Interpreter.Error // an error happened during compilation, e.g. a type error else { - val (interpreterResultString, succeeded) = req.loadAndRun() + val (resultStrings, succeeded) = req.loadAndRun() if (delayOutput) - previousOutput = clean(interpreterResultString) :: previousOutput + previousOutput = resultStrings.map(clean) ::: previousOutput else if (printResults || !succeeded) - out.print(clean(interpreterResultString)) + resultStrings.map(x => out.print(clean(x))) if (succeeded) { prevRequests += req Interpreter.Success @@ -388,19 +389,30 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit * a boolean indicating whether the run succeeded without throwing * an exception. */ - def loadAndRun(): (String, Boolean) = { + def loadAndRun(): (List[String], Boolean) = { val interpreterResultObject: Class[_] = Class.forName(resultObjectName, true, classLoader) val resultValMethod: java.lang.reflect.Method = interpreterResultObject.getMethod("result") try { - (resultValMethod.invoke(interpreterResultObject).toString, true) + val ps = new java.io.ByteArrayOutputStream() + Console.withOut(ps) { + val res = SyntaxHighlighting(resultValMethod.invoke(interpreterResultObject).toString).toArray + val prints = ps.toString("utf-8") + val printList = + if (prints == "") Nil + else prints :: Nil + + if (!delayOutput) out.print(prints) + + (printList :+ new String(res), true) + } } catch { case NonFatal(ex) => def cause(ex: Throwable): Throwable = if (ex.getCause eq null) ex else cause(ex.getCause) val orig = cause(ex) - (stringFrom(str => orig.printStackTrace(str)), false) + (stringFrom(str => orig.printStackTrace(str)) :: Nil, false) } } |