diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-05-12 11:30:34 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2016-05-12 11:35:37 +0200 |
commit | bce6abd2fad9aeb54d263803f729ee53b1f49597 (patch) | |
tree | 7964f8823bd44bec63714dcf85e5f2b483b383be /src | |
parent | 134ad7a6a172fec97dc438dd0aff3766a0f0944a (diff) | |
download | dotty-bce6abd2fad9aeb54d263803f729ee53b1f49597.tar.gz dotty-bce6abd2fad9aeb54d263803f729ee53b1f49597.tar.bz2 dotty-bce6abd2fad9aeb54d263803f729ee53b1f49597.zip |
Fix stdout redirect for REPL's println
When printing in the REPL via `println`, the output would end up on the
same line, since stdout had not been redirected. This commit remedies
that.
It also adds syntax highlighting to result types.
Diffstat (limited to 'src')
-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) } } |