Skip to content

Commit be25213

Browse files
authored
Merge pull request #3911 from benkobalog/fix-#1369
Fix #1369 Print a newline after interpreted commands
2 parents d109c0b + 579e833 commit be25213

File tree

4 files changed

+26
-37
lines changed

4 files changed

+26
-37
lines changed

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ case class Completions(cursor: Int,
8181

8282
/** Main REPL instance, orchestrating input, compilation and presentation */
8383
class ReplDriver(settings: Array[String],
84-
protected val out: PrintStream = Console.out,
85-
protected val classLoader: Option[ClassLoader] = None) extends Driver {
84+
out: PrintStream = Console.out,
85+
classLoader: Option[ClassLoader] = None) extends Driver {
8686

8787
/** Overridden to `false` in order to not have to give sources on the
8888
* commandline
@@ -197,8 +197,8 @@ class ReplDriver(settings: Array[String],
197197
private def extractImports(trees: List[untpd.Tree]): List[untpd.Import] =
198198
trees.collect { case imp: untpd.Import => imp }
199199

200-
private def interpret(res: ParseResult)(implicit state: State): State =
201-
res match {
200+
private def interpret(res: ParseResult)(implicit state: State): State = {
201+
val newState = res match {
202202
case parsed: Parsed if parsed.trees.nonEmpty =>
203203
compile(parsed)
204204
.withHistory(parsed.sourceCode :: state.history)
@@ -216,6 +216,9 @@ class ReplDriver(settings: Array[String],
216216
case _ => // new line, empty tree
217217
state
218218
}
219+
out.println()
220+
newState
221+
}
219222

220223
/** Compile `parsed` trees and evolve `state` in accordance */
221224
protected[this] final def compile(parsed: Parsed)(implicit state: State): State = {

compiler/test-resources/repl/i1369

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
scala> print("foo")
2+
foo
3+
scala> "Hello"
4+
val res0: String = Hello

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,20 @@ import org.junit.Assert.fail
1111
import dotc.reporting.diagnostic.MessageContainer
1212
import results.Result
1313

14-
sealed class NullPrintStream extends ByteArrayOutputStream {
15-
override def write(b: Int) = ()
16-
override def write(b: Array[Byte], off: Int, len: Int) = ()
17-
override def writeTo(out: OutputStream) = ()
18-
}
19-
20-
sealed class StoringPrintStream extends PrintStream(new NullPrintStream) {
21-
private[this] var sb = new StringBuilder
22-
23-
override def println(obj: Object) =
24-
println(obj.toString)
25-
26-
override def println(str: String) = {
27-
sb.append(str)
28-
sb += '\n'
29-
}
3014

31-
def flushStored(): String = {
32-
val str = sb.toString
33-
sb = new StringBuilder
34-
str
35-
}
36-
}
37-
38-
class ReplTest extends ReplDriver(
15+
class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
3916
Array("-classpath", List(Jars.dottyLib, Jars.dottyInterfaces).mkString(":"), "-color:never"),
40-
new StoringPrintStream
17+
new PrintStream(out)
4118
) with MessageRendering {
4219

43-
/** Get the stored output from `out`, resetting the `StoringPrintStream` */
44-
def storedOutput(): String =
45-
stripColor(out.asInstanceOf[StoringPrintStream].flushStored())
20+
def this() = this(new ByteArrayOutputStream)
21+
22+
/** Get the stored output from `out`, resetting the buffer */
23+
def storedOutput(): String = {
24+
val output = stripColor(out.toString)
25+
out.reset()
26+
output
27+
}
4628

4729
protected implicit def toParsed(expr: String)(implicit state: State): Parsed = {
4830
implicit val ctx = state.run.runContext

compiler/test/dotty/tools/repl/ScriptedTests.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ class ScriptedTests extends ReplTest with MessageRendering {
2222

2323
private def testFile(f: JFile): Unit = {
2424
val prompt = "scala>"
25-
val lines = Source.fromFile(f).getLines.buffered
25+
val lines = Source.fromFile(f).getLines().buffered
26+
27+
assert(lines.head.startsWith(prompt),
28+
s"""Each file has to start with the prompt: "$prompt"""")
2629

2730
def extractInputs(prompt: String): List[String] = {
2831
val input = lines.next()
2932

30-
assert(input.startsWith(prompt),
31-
s"""Each file has to start with the prompt: "$prompt"""")
32-
3333
if (!input.startsWith(prompt)) extractInputs(prompt)
3434
else if (lines.hasNext) {
3535
// read lines and strip trailing whitespace:
@@ -60,7 +60,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
6060
}
6161

6262
val expectedOutput =
63-
Source.fromFile(f).getLines.flatMap(filterEmpties).mkString("\n")
63+
Source.fromFile(f).getLines().flatMap(filterEmpties).mkString("\n")
6464
val actualOutput = {
6565
resetToInitial()
6666
init()

0 commit comments

Comments
 (0)