Skip to content

Commit 7719a3c

Browse files
committed
Workaround JLine bug for mid-line tab completion
Before: ``` scala> {" ".char<TAB>} charAt chars scala> {" ".char<CURSOR>}} ``` I noticed that pressing <SPACE>-<BACKSPACE> re-rendered the line correctly, so I've added this workaround to our customization of the JLine console reader. After: ``` scala> {" ".char<TAB>} charAt chars scala> {" ".char<CURSOR>} ``` We can delete this workaround when JLine 2.13.1 is released, but I haven't heard back about when this might happen.
1 parent b89874b commit 7719a3c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala

+23-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ package scala.tools.nsc.interpreter.jline
1010
import java.util.{Collection => JCollection, List => JList}
1111

1212
import _root_.jline.{console => jconsole}
13-
import jconsole.completer.{Completer, ArgumentCompleter}
13+
import jline.console.ConsoleReader
14+
import jline.console.completer.{CompletionHandler, Completer, ArgumentCompleter}
1415
import jconsole.history.{History => JHistory}
1516

1617

@@ -142,6 +143,27 @@ private class JLineConsoleReader extends jconsole.ConsoleReader with interpreter
142143
case NoCompletion => ()
143144
case _ => this addCompleter completer
144145
}
146+
147+
// This is a workaround for https://github.com/jline/jline2/issues/208
148+
// and should not be necessary once we upgrade to JLine 2.13.1
149+
///
150+
// Test by:
151+
// scala> {" ".char}<LEFT><TAB>
152+
//
153+
// And checking we don't get an extra } on the line.
154+
///
155+
val handler = getCompletionHandler
156+
setCompletionHandler(new CompletionHandler {
157+
override def complete(consoleReader: ConsoleReader, list: JList[CharSequence], i: Int): Boolean = {
158+
try {
159+
handler.complete(consoleReader, list, i)
160+
} finally if (getCursorBuffer.cursor != getCursorBuffer.length()) {
161+
print(" ")
162+
getCursorBuffer.write(' ')
163+
backspace()
164+
}
165+
}
166+
})
145167
setAutoprintThreshold(400) // max completion candidates without warning
146168
}
147169
}

0 commit comments

Comments
 (0)