Skip to content

Fix #1369 Print a newline after interpreted commands #3911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ case class Completions(cursor: Int,

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

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

private def interpret(res: ParseResult)(implicit state: State): State =
res match {
private def interpret(res: ParseResult)(implicit state: State): State = {
val newState = res match {
case parsed: Parsed if parsed.trees.nonEmpty =>
compile(parsed)
.withHistory(parsed.sourceCode :: state.history)
Expand All @@ -216,6 +216,9 @@ class ReplDriver(settings: Array[String],
case _ => // new line, empty tree
state
}
out.println()
newState
}

/** Compile `parsed` trees and evolve `state` in accordance */
protected[this] final def compile(parsed: Parsed)(implicit state: State): State = {
Expand Down
4 changes: 4 additions & 0 deletions compiler/test-resources/repl/i1369
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
scala> print("foo")
foo
scala> "Hello"
val res0: String = Hello
38 changes: 10 additions & 28 deletions compiler/test/dotty/tools/repl/ReplTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,20 @@ import org.junit.Assert.fail
import dotc.reporting.diagnostic.MessageContainer
import results.Result

sealed class NullPrintStream extends ByteArrayOutputStream {
override def write(b: Int) = ()
override def write(b: Array[Byte], off: Int, len: Int) = ()
override def writeTo(out: OutputStream) = ()
}

sealed class StoringPrintStream extends PrintStream(new NullPrintStream) {
private[this] var sb = new StringBuilder

override def println(obj: Object) =
println(obj.toString)

override def println(str: String) = {
sb.append(str)
sb += '\n'
}

def flushStored(): String = {
val str = sb.toString
sb = new StringBuilder
str
}
}

class ReplTest extends ReplDriver(
class ReplTest private (out: ByteArrayOutputStream) extends ReplDriver(
Array("-classpath", List(Jars.dottyLib, Jars.dottyInterfaces).mkString(":"), "-color:never"),
new StoringPrintStream
new PrintStream(out)
) with MessageRendering {

/** Get the stored output from `out`, resetting the `StoringPrintStream` */
def storedOutput(): String =
stripColor(out.asInstanceOf[StoringPrintStream].flushStored())
def this() = this(new ByteArrayOutputStream)

/** Get the stored output from `out`, resetting the buffer */
def storedOutput(): String = {
val output = stripColor(out.toString)
out.reset()
output
}

protected implicit def toParsed(expr: String)(implicit state: State): Parsed = {
implicit val ctx = state.run.runContext
Expand Down
10 changes: 5 additions & 5 deletions compiler/test/dotty/tools/repl/ScriptedTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class ScriptedTests extends ReplTest with MessageRendering {

private def testFile(f: JFile): Unit = {
val prompt = "scala>"
val lines = Source.fromFile(f).getLines.buffered
val lines = Source.fromFile(f).getLines().buffered

assert(lines.head.startsWith(prompt),
s"""Each file has to start with the prompt: "$prompt"""")

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

assert(input.startsWith(prompt),
s"""Each file has to start with the prompt: "$prompt"""")

if (!input.startsWith(prompt)) extractInputs(prompt)
else if (lines.hasNext) {
// read lines and strip trailing whitespace:
Expand Down Expand Up @@ -60,7 +60,7 @@ class ScriptedTests extends ReplTest with MessageRendering {
}

val expectedOutput =
Source.fromFile(f).getLines.flatMap(filterEmpties).mkString("\n")
Source.fromFile(f).getLines().flatMap(filterEmpties).mkString("\n")
val actualOutput = {
resetToInitial()
init()
Expand Down