Skip to content

Commit 15be4d5

Browse files
committed
Polishing
1 parent 75d55cc commit 15be4d5

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import org.jline.reader.impl.history.DefaultHistory
1414
import org.jline.terminal.TerminalBuilder
1515
import org.jline.utils.AttributedString
1616

17-
final class JLineTerminal {
17+
final class JLineTerminal extends java.io.Closeable {
18+
// import java.util.logging.{Logger, Level}
19+
// Logger.getLogger("org.jline").setLevel(Level.FINEST)
20+
1821
private val terminal = TerminalBuilder.terminal()
1922
private val history = new DefaultHistory
2023

@@ -52,6 +55,8 @@ final class JLineTerminal {
5255
lineReader.readLine(prompt)
5356
}
5457

58+
def close() = terminal.close()
59+
5560
/** Provide syntax highlighting */
5661
private class Highlighter extends reader.Highlighter {
5762
def highlight(reader: LineReader, buffer: String): AttributedString = {

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)