Skip to content

Commit 85e2844

Browse files
committed
Add support for disabling redirected output in the REPL driver for usage in worksheets in the Scala Plugin for IntelliJ IDEA
- Calling `setOut/setErr` in a concurrent environment without any synchronization (such as the Scala compile server in the Scala Plugin for IntelliJ IDEA, which is used to execute Scala 3 worksheets) can lead to unpredictable outcomes where the out/err streams are not restored properly after changing. - This change adds a new default method `redirectOutput` which can be overriden by others to control the redirecting behavior of the REPL driver.
1 parent 04f5a0b commit 85e2844

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,23 @@ class ReplDriver(settings: Array[String],
187187
// TODO: i5069
188188
final def bind(name: String, value: Any)(using state: State): State = state
189189

190+
protected def redirectOutput: Boolean = true
191+
190192
// redirecting the output allows us to test `println` in scripted tests
191193
private def withRedirectedOutput(op: => State): State = {
192-
val savedOut = System.out
193-
val savedErr = System.err
194-
try {
195-
System.setOut(out)
196-
System.setErr(out)
197-
op
198-
}
199-
finally {
200-
System.setOut(savedOut)
201-
System.setErr(savedErr)
202-
}
194+
if redirectOutput then
195+
val savedOut = System.out
196+
val savedErr = System.err
197+
try {
198+
System.setOut(out)
199+
System.setErr(out)
200+
op
201+
}
202+
finally {
203+
System.setOut(savedOut)
204+
System.setErr(savedErr)
205+
}
206+
else op
203207
}
204208

205209
private def newRun(state: State, reporter: StoreReporter = newStoreReporter) = {

0 commit comments

Comments
 (0)