diff options
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/JLineReader.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/interpreter/JLineReader.scala | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/JLineReader.scala b/src/compiler/scala/tools/nsc/interpreter/JLineReader.scala index 8f42305bcb..d09567eadf 100644 --- a/src/compiler/scala/tools/nsc/interpreter/JLineReader.scala +++ b/src/compiler/scala/tools/nsc/interpreter/JLineReader.scala @@ -7,12 +7,57 @@ package scala.tools.nsc package interpreter import java.io.File +import java.util.{ List => JList } import scala.tools.jline.console.ConsoleReader import scala.tools.jline.console.completer._ +import scala.tools.jline.console.history._ +import scala.tools.jline.console.history.{ FileHistory, PersistentHistory, History => JHistory } +import scala.tools.jline.console.history.History.{ Entry => JEntry } +import scala.tools.jline.console.ConsoleReader +import scala.collection.JavaConverters._ +import Properties.userHome + +/** A wrapper for JLine's History. + */ +class JLineHistory(val jhistory: JHistory) extends History { + def asJavaList = jhistory.entries() + def asStrings = asList map (_.value.toString) + def asList: List[JEntry] = asJavaList.asScala.toList + def index = jhistory.index() + def size = jhistory.size() + + def grep(s: String) = asStrings filter (_ contains s) + def flush() = jhistory match { + case x: PersistentHistory => x.flush() + case _ => () + } +} + +object JLineHistory { + val ScalaHistoryFile = ".scala_history" + + def apply() = new JLineHistory( + try newFile() + catch { case x : Exception => + Console.println("Error creating file history: memory history only. " + x) + newMemory() + } + ) + + def newMemory() = new MemoryHistory() + def newFile() = new FileHistory(new File(userHome, ScalaHistoryFile)) { + // flush after every add to avoid installing a shutdown hook. + // (The shutdown hook approach also loses history when they aren't run.) + override def add(item: CharSequence): Unit = { + super.add(item) + flush() + } + } +} /** Reads from the console using JLine */ class JLineReader(val completion: Completion) extends InteractiveReader { - lazy val history = History() + lazy val history = JLineHistory() def reset() = consoleReader.getTerminal().reset() def init() = consoleReader.getTerminal().init() @@ -24,7 +69,12 @@ class JLineReader(val completion: Completion) extends InteractiveReader { } def argCompletor: ArgumentCompleter = { - val c = new ArgumentCompleter(new JLineDelimiter, completion.completer()) + val wrapped = new Completer { + val cc = completion.completer() + def complete(buffer: String, cursor: Int, candidates: JList[CharSequence]): Int = + cc.complete(buffer, cursor, candidates) + } + val c = new ArgumentCompleter(new JLineDelimiter, wrapped) c setStrict false c } @@ -37,7 +87,7 @@ class JLineReader(val completion: Completion) extends InteractiveReader { if (completion ne Completion.Empty) { r addCompleter argCompletor - r setAutoprintThreshold 250 // max completion candidates without warning + r setAutoprintThreshold 400 // max completion candidates without warning } r @@ -48,3 +98,10 @@ class JLineReader(val completion: Completion) extends InteractiveReader { val interactive = true } +object JLineReader { + def apply(intp: IMain): InteractiveReader = apply(new JLineCompletion(intp)) + def apply(comp: Completion): InteractiveReader = { + try new JLineReader(comp) + catch { case e @ (_: Exception | _: NoClassDefFoundError) => new SimpleReader } + } +} |