@@ -114,19 +114,39 @@ class ReplDriver(settings: Array[String],
114114 * `protected final` to facilitate testing.
115115 */
116116 final def runUntilQuit (): State = {
117+ val terminal = new JLineTerminal ()
118+
119+ /** Blockingly read a line, getting back a parse result */
120+ def readLine (state : State ): ParseResult = {
121+ val completer : Completer = { (_, line, candidates) =>
122+ val comps = completions(line.cursor, line.line, state)
123+ candidates.addAll(comps.asJava)
124+ }
125+ implicit val ctx = state.run.runContext
126+ try {
127+ val line = terminal.readLine(completer)
128+ ParseResult (line)
129+ }
130+ catch {
131+ case _ : EndOfFileException => // Ctrl+D
132+ Quit
133+ }
134+ }
135+
117136 @ tailrec def loop (state : State ): State = {
118- val res = readLine()( state)
137+ val res = readLine(state)
119138
120139 if (res == Quit ) state
121140 else {
122141 // readLine potentially destroys the run, so a new one is needed for the
123142 // rest of the interpretation:
124- implicit val freshState = state.newRun(compiler, rootCtx)
125- loop(interpret(res))
143+ val freshState = state.newRun(compiler, rootCtx)
144+ loop(interpret(res)(freshState) )
126145 }
127146 }
128147
129- withRedirectedOutput { loop(initState) }
148+ try withRedirectedOutput { loop(initState) }
149+ finally terminal.close()
130150 }
131151
132152 final def run (input : String )(implicit state : State ): State = withRedirectedOutput {
@@ -167,26 +187,6 @@ class ReplDriver(settings: Array[String],
167187 .getOrElse(Nil )
168188 }
169189
170- // lazy because the REPL tests do not rely on the JLine reader
171- private lazy val terminal = new JLineTerminal ()
172-
173- /** Blockingly read a line, getting back a parse result */
174- private def readLine ()(implicit state : State ): ParseResult = {
175- implicit val ctx = state.run.runContext
176- val completer : Completer = { (_, line, candidates) =>
177- val comps = completions(line.cursor, line.line, state)
178- candidates.addAll(comps.asJava)
179- }
180- try {
181- val line = terminal.readLine(completer)
182- ParseResult (line)
183- }
184- catch {
185- case _ : EndOfFileException => // Ctrl+D
186- Quit
187- }
188- }
189-
190190 private def extractImports (trees : List [untpd.Tree ]): List [untpd.Import ] =
191191 trees.collect { case imp : untpd.Import => imp }
192192
0 commit comments