@@ -24,6 +24,7 @@ import dotty.tools.backend.jvm.GenBCode
24
24
import Symbols ._ , Types ._ , Contexts ._ , StdNames ._ , Names ._ , NameOps ._
25
25
import Decorators ._
26
26
import scala .util .control .NonFatal
27
+ import printing .SyntaxHighlighting
27
28
28
29
/** An interpreter for Scala code which is based on the `dotc` compiler.
29
30
*
@@ -210,11 +211,11 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
210
211
if (! req.compile())
211
212
Interpreter .Error // an error happened during compilation, e.g. a type error
212
213
else {
213
- val (interpreterResultString , succeeded) = req.loadAndRun()
214
+ val (resultStrings , succeeded) = req.loadAndRun()
214
215
if (delayOutput)
215
- previousOutput = clean(interpreterResultString) :: previousOutput
216
+ previousOutput = resultStrings.map(clean) : :: previousOutput
216
217
else if (printResults || ! succeeded)
217
- out.print(clean(interpreterResultString ))
218
+ resultStrings.map(x => out.print(clean(x) ))
218
219
if (succeeded) {
219
220
prevRequests += req
220
221
Interpreter .Success
@@ -388,19 +389,31 @@ class CompilingInterpreter(out: PrintWriter, ictx: Context) extends Compiler wit
388
389
* a boolean indicating whether the run succeeded without throwing
389
390
* an exception.
390
391
*/
391
- def loadAndRun (): (String , Boolean ) = {
392
+ def loadAndRun (): (List [ String ] , Boolean ) = {
392
393
val interpreterResultObject : Class [_] =
393
394
Class .forName(resultObjectName, true , classLoader)
394
395
val resultValMethod : java.lang.reflect.Method =
395
396
interpreterResultObject.getMethod(" result" )
396
397
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
+ }
398
411
} catch {
399
412
case NonFatal (ex) =>
400
413
def cause (ex : Throwable ): Throwable =
401
414
if (ex.getCause eq null ) ex else cause(ex.getCause)
402
415
val orig = cause(ex)
403
- (stringFrom(str => orig.printStackTrace(str)), false )
416
+ (stringFrom(str => orig.printStackTrace(str)) :: Nil , false )
404
417
}
405
418
}
406
419
0 commit comments