summaryrefslogtreecommitdiff
path: root/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@typesafe.com>2015-06-17 11:06:31 -0700
committerAdriaan Moors <adriaan.moors@typesafe.com>2015-06-18 10:33:33 -0700
commitdfb70b632c1e8a2c6ce27eaacb74dbbb47ce9532 (patch)
tree277124732e329eb78b6168ad8ad3a6c908fb407f /src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala
parent43139faa4f4348b95907e06883f2fefb41ea3a3b (diff)
downloadscala-dfb70b632c1e8a2c6ce27eaacb74dbbb47ce9532.tar.gz
scala-dfb70b632c1e8a2c6ce27eaacb74dbbb47ce9532.tar.bz2
scala-dfb70b632c1e8a2c6ce27eaacb74dbbb47ce9532.zip
SI-9339 Support classpaths with no single compatible jline
As usual, the repl will use whatever jline 2 jar on the classpath, if there is one. Failing that, there's a fallback and an override. If instantiating the standard `jline.InteractiveReader` fails, we fall back to an embedded, shaded, version of jline, provided by `jline_embedded.InteractiveReader`. (Assume `import scala.tools.nsc.interpreter._` for this message.) The instantiation of `InteractiveReader` eagerly exercises jline, so that a linkage error will result if jline is missing or if the provided one is not binary compatible. The property `scala.repl.reader` overrides this behavior, if set to the FQN of a class that looks like `YourInteractiveReader` below. ``` class YourInteractiveReader(completer: () => Completion) extends InteractiveReader ``` The repl logs which classes it tried to instantiate under `-Ydebug`. # Changes to source & build The core of the repl (`src/repl`) no longer depends on jline. The jline interface is now in `src/repl-jline`. The embedded jline + our interface to it are generated by the `quick.repl` target. The build now also enforces that only `src/repl-jline` depends on jline. The sources in `src/repl` are now sure to be independent of it, though they do use reflection to instantiate a suitable subclass of `InteractiveReader`, as explained above. The `quick.repl` target builds the sources in `src/repl` and `src/repl-jline`, producing a jar for the `repl-jline` classes, which is then transformed using jarjar to obtain a shaded copy of the `scala.tools.nsc.interpreter.jline` package. Jarjar is used to combine the `jline` jar and the `repl-jline` into a new jar, rewriting package names as follows: - `org.fusesource` -> `scala.tools.fusesource_embedded` - `jline` -> `scala.tools.jline_embedded` - `scala.tools.nsc.interpreter.jline` -> `scala.tools.nsc.interpreter.jline_embedded` Classes not reachable from `scala.tools.**` are pruned, as well as empty dirs. The classes in the `repl-jline` jar as well as those in the rewritten one are copied to the repl's output directory. PS: The sbt build is not updated, sorry. PPS: A more recent fork of jarjar: https://github.com/shevek/jarjar.
Diffstat (limited to 'src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala')
-rw-r--r--src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala b/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala
new file mode 100644
index 0000000000..f0fce13fe8
--- /dev/null
+++ b/src/repl-jline/scala/tools/nsc/interpreter/jline/JLineReader.scala
@@ -0,0 +1,143 @@
+/** NSC -- new Scala compiler
+ *
+ * Copyright 2005-2015 LAMP/EPFL
+ * @author Stepan Koltsov
+ * @author Adriaan Moors
+ */
+
+package scala.tools.nsc.interpreter.jline
+
+import java.util.{Collection => JCollection, List => JList}
+
+import _root_.jline.{console => jconsole}
+import jconsole.completer.{Completer, ArgumentCompleter}
+import jconsole.history.{History => JHistory}
+
+
+import scala.tools.nsc.interpreter
+import scala.tools.nsc.interpreter.Completion
+import scala.tools.nsc.interpreter.Completion.Candidates
+import scala.tools.nsc.interpreter.session.History
+
+/**
+ * Reads from the console using JLine.
+ *
+ * Eagerly instantiates all relevant JLine classes, so that we can detect linkage errors on `new JLineReader` and retry.
+ */
+class InteractiveReader(completer: () => Completion) extends interpreter.InteractiveReader {
+ val interactive = true
+
+ val history: History = new JLineHistory.JLineFileHistory()
+
+ private val consoleReader = {
+ val reader = new JLineConsoleReader()
+
+ reader setPaginationEnabled interpreter.`package`.isPaged
+
+ // ASAP
+ reader setExpandEvents false
+
+ reader setHistory history.asInstanceOf[JHistory]
+
+ reader
+ }
+
+ private[this] var _completion: Completion = interpreter.NoCompletion
+ def completion: Completion = _completion
+
+ override def postInit() = {
+ _completion = completer()
+
+ consoleReader.initCompletion(completion)
+ }
+
+ def reset() = consoleReader.getTerminal().reset()
+ def redrawLine() = consoleReader.redrawLineAndFlush()
+ def readOneLine(prompt: String) = consoleReader.readLine(prompt)
+ def readOneKey(prompt: String) = consoleReader.readOneKey(prompt)
+}
+
+// implements a jline interface
+private class JLineConsoleReader extends jconsole.ConsoleReader with interpreter.VariColumnTabulator {
+ val isAcross = interpreter.`package`.isAcross
+ val marginSize = 3
+
+ def width = getTerminal.getWidth()
+ def height = getTerminal.getHeight()
+
+ private def morePrompt = "--More--"
+
+ private def emulateMore(): Int = {
+ val key = readOneKey(morePrompt)
+ try key match {
+ case '\r' | '\n' => 1
+ case 'q' => -1
+ case _ => height - 1
+ }
+ finally {
+ eraseLine()
+ // TODO: still not quite managing to erase --More-- and get
+ // back to a scala prompt without another keypress.
+ if (key == 'q') {
+ putString(getPrompt())
+ redrawLine()
+ flush()
+ }
+ }
+ }
+
+ override def printColumns(items: JCollection[_ <: CharSequence]): Unit = {
+ import scala.tools.nsc.interpreter.javaCharSeqCollectionToScala
+ printColumns_(items: List[String])
+ }
+
+ private def printColumns_(items: List[String]): Unit = if (items exists (_ != "")) {
+ val grouped = tabulate(items)
+ var linesLeft = if (isPaginationEnabled()) height - 1 else Int.MaxValue
+ grouped foreach { xs =>
+ println(xs.mkString)
+ linesLeft -= 1
+ if (linesLeft <= 0) {
+ linesLeft = emulateMore()
+ if (linesLeft < 0)
+ return
+ }
+ }
+ }
+
+ def readOneKey(prompt: String) = {
+ this.print(prompt)
+ this.flush()
+ this.readCharacter()
+ }
+
+ def eraseLine() = resetPromptLine("", "", 0)
+
+ def redrawLineAndFlush(): Unit = {
+ flush(); drawLine(); flush()
+ }
+
+ // A hook for running code after the repl is done initializing.
+ def initCompletion(completion: Completion): Unit = {
+ this setBellEnabled false
+
+ if (completion ne interpreter.NoCompletion) {
+ val jlineCompleter = new ArgumentCompleter(new JLineDelimiter,
+ new Completer {
+ val tc = completion.completer()
+ def complete(_buf: String, cursor: Int, candidates: JList[CharSequence]): Int = {
+ val buf = if (_buf == null) "" else _buf
+ val Candidates(newCursor, newCandidates) = tc.complete(buf, cursor)
+ newCandidates foreach (candidates add _)
+ newCursor
+ }
+ }
+ )
+
+ jlineCompleter setStrict false
+
+ this addCompleter jlineCompleter
+ this setAutoprintThreshold 400 // max completion candidates without warning
+ }
+ }
+}