Skip to content

Commit f7f0387

Browse files
committed
Fix scala#1369: Ensure REPL always emits a newline
1 parent 62348de commit f7f0387

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ class CompilingInterpreter(
335335
case stat: TypeDef => new TypeAliasHandler(stat)
336336
case stat: Import => new ImportHandler(stat)
337337
// case DocDef(_, documented) => chooseHandler(documented)
338-
case stat => new GenericHandler(stat)
338+
case _ => new GenericHandler(stat)
339339
}
340340

341341
private val handlers: List[StatementHandler] = trees.map(chooseHandler)
@@ -661,17 +661,24 @@ class CompilingInterpreter(
661661
def resultExtractionCode(req: Request, code: PrintWriter) = {}
662662
}
663663

664-
private class GenericHandler(statement: Tree) extends StatementHandler(statement)
664+
private class GenericHandler(statement: Tree) extends StatementHandler(statement) {
665+
override def resultExtractionCode(req: Request, code: PrintWriter) = {
666+
code.print(""" + "\n" """)
667+
}
668+
}
665669

666670
private abstract class ValOrPatHandler(statement: Tree)
667671
extends StatementHandler(statement) {
668672
override val boundNames: List[Name] = _boundNames
669673
override def valAndVarNames = boundNames
670674

671675
override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
672-
if (!shouldShowResult(req)) return
673-
val resultExtractors = boundNames.map(name => resultExtractor(req, name))
674-
code.print(resultExtractors.mkString(""))
676+
if (!shouldShowResult(req)) {
677+
code.print(""" + "\n" """)
678+
} else {
679+
val resultExtractors = boundNames.map(name => resultExtractor(req, name))
680+
code.print(resultExtractors.mkString(""))
681+
}
675682
}
676683

677684
private def resultExtractor(req: Request, varName: Name): String = {

tests/repl/newline.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
scala> print("Hello, world!")
2+
Hello, world!
3+
scala> println("Hello, world!")
4+
Hello, world!
5+
6+
scala> var x = 0
7+
x: Int = 0
8+
scala> x *= { print("Hello, world!"); 2 }
9+
Hello, world!
10+
scala> :quit

tests/repl/vars.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ x: Int = 0
33
scala> x = x + 1
44
x: Int = 1
55
scala> x *= 2
6+
67
scala> x
78
res2: Int = 2
89
scala> :quit

0 commit comments

Comments
 (0)