Skip to content

Commit aa62668

Browse files
committed
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.
1 parent 134ad7a commit aa62668

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import dotty.tools.backend.jvm.GenBCode
2424
import Symbols._, Types._, Contexts._, StdNames._, Names._, NameOps._
2525
import Decorators._
2626
import scala.util.control.NonFatal
27+
import printing.SyntaxHighlighting
2728

2829
/** An interpreter for Scala code which is based on the `dotc` compiler.
2930
*
@@ -210,11 +211,11 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
210211
if (!req.compile())
211212
Interpreter.Error // an error happened during compilation, e.g. a type error
212213
else {
213-
val (interpreterResultString, succeeded) = req.loadAndRun()
214+
val (resultStrings, succeeded) = req.loadAndRun()
214215
if (delayOutput)
215-
previousOutput = clean(interpreterResultString) :: previousOutput
216+
previousOutput = resultStrings.map(clean) ::: previousOutput
216217
else if (printResults || !succeeded)
217-
out.print(clean(interpreterResultString))
218+
resultStrings.map(x => out.print(clean(x)))
218219
if (succeeded) {
219220
prevRequests += req
220221
Interpreter.Success
@@ -388,19 +389,31 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
388389
* a boolean indicating whether the run succeeded without throwing
389390
* an exception.
390391
*/
391-
def loadAndRun(): (String, Boolean) = {
392+
def loadAndRun(): (List[String], Boolean) = {
392393
val interpreterResultObject: Class[_] =
393394
Class.forName(resultObjectName, true, classLoader)
394395
val resultValMethod: java.lang.reflect.Method =
395396
interpreterResultObject.getMethod("result")
396397
try {
397-
(resultValMethod.invoke(interpreterResultObject).toString, true)
398+
val ps = new java.io.ByteArrayOutputStream()
399+
Console.withOut(ps) {
400+
val res = SyntaxHighlighting(resultValMethod.invoke(interpreterResultObject).toString).toArray
401+
val prints = ps.toString("utf-8")
402+
val printList =
403+
if (prints == "") Nil
404+
else prints :: Nil
405+
406+
if (!delayOutput)
407+
out.print(prints)
408+
409+
(printList :+ new String(res), true)
410+
}
398411
} catch {
399412
case NonFatal(ex) =>
400413
def cause(ex: Throwable): Throwable =
401414
if (ex.getCause eq null) ex else cause(ex.getCause)
402415
val orig = cause(ex)
403-
(stringFrom(str => orig.printStackTrace(str)), false)
416+
(stringFrom(str => orig.printStackTrace(str)) :: Nil, false)
404417
}
405418
}
406419

0 commit comments

Comments
 (0)