@@ -114,19 +114,39 @@ class ReplDriver(settings: Array[String],
114
114
* `protected final` to facilitate testing.
115
115
*/
116
116
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
+
117
136
@ tailrec def loop (state : State ): State = {
118
- val res = readLine()( state)
137
+ val res = readLine(state)
119
138
120
139
if (res == Quit ) state
121
140
else {
122
141
// readLine potentially destroys the run, so a new one is needed for the
123
142
// 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) )
126
145
}
127
146
}
128
147
129
- withRedirectedOutput { loop(initState) }
148
+ try withRedirectedOutput { loop(initState) }
149
+ finally terminal.close()
130
150
}
131
151
132
152
final def run (input : String )(implicit state : State ): State = withRedirectedOutput {
@@ -167,26 +187,6 @@ class ReplDriver(settings: Array[String],
167
187
.getOrElse(Nil )
168
188
}
169
189
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
-
190
190
private def extractImports (trees : List [untpd.Tree ]): List [untpd.Import ] =
191
191
trees.collect { case imp : untpd.Import => imp }
192
192
0 commit comments