Skip to content

Override Console.out and Console.err the REPL #3940

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 1 commit into from
Jan 29, 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
38 changes: 22 additions & 16 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ case class Completions(cursor: Int,

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

/** Overridden to `false` in order to not have to give sources on the
Expand Down Expand Up @@ -135,26 +135,32 @@ class ReplDriver(settings: Array[String],
* observable outside of the CLI, for this reason, most helper methods are
* `protected final` to facilitate testing.
*/
@tailrec final def runUntilQuit(state: State = initState): State = {
val res = readLine()(state)
final def runUntilQuit(): State = {
@tailrec def loop(state: State): State = {
val res = readLine()(state)

if (res == Quit) {
out.println()
state
}
else {
// readLine potentially destroys the run, so a new one is needed for the
// rest of the interpretation:
implicit val freshState = state.newRun(compiler, rootCtx)
runUntilQuit(interpret(res))
if (res == Quit) {
out.println()
state
}
else {
// readLine potentially destroys the run, so a new one is needed for the
// rest of the interpretation:
implicit val freshState = state.newRun(compiler, rootCtx)
loop(interpret(res))
}
}

withRedirectedOutput { loop(initState) }
}

final def run(input: String)(implicit state: State): State =
run(ParseResult(input)(state.run.runContext))(state.newRun(compiler, rootCtx))
final def run(input: String)(implicit state: State): State = withRedirectedOutput {
val parsed = ParseResult(input)(state.run.runContext)
interpret(parsed)(state.newRun(compiler, rootCtx))
}

final def run(res: ParseResult)(implicit state: State): State =
interpret(res)
private def withRedirectedOutput(op: => State): State =
Console.withOut(out) { Console.withErr(out) { op } }

/** Extract possible completions at the index of `cursor` in `expr` */
protected[this] final def completions(cursor: Int, expr: String, state0: State): Completions = {
Expand Down
2 changes: 2 additions & 0 deletions compiler/test-resources/repl/outputStream
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scala> println("Hello World!")
Hello World!