Skip to content

Commit

Permalink
handle .inputtrc
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Mar 29, 2021
1 parent 107c727 commit a6a9cbe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ lazy val compiler = configureAsSubproject(project)
|org.jline.terminal.impl.jna.*;resolution:=optional
|org.jline.terminal.spi;resolution:=optional
|org.jline.utils;resolution:=optional
|org.jline.builtins;resolution:=optional
|scala.*;version="$${range;[==,=+);$${ver}}"
|*""".stripMargin.linesIterator.mkString(","),
"Class-Path" -> "scala-reflect.jar scala-library.jar"
Expand Down
36 changes: 35 additions & 1 deletion src/repl-frontend/scala/tools/nsc/interpreter/jline/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@
package scala.tools.nsc.interpreter
package jline

import org.jline.builtins.InputRC
import org.jline.reader.Parser.ParseContext
import org.jline.reader.impl.{DefaultParser, LineReaderImpl}
import org.jline.reader._
import org.jline.reader.impl.{DefaultParser, LineReaderImpl}
import org.jline.terminal.Terminal

import java.io.{ByteArrayInputStream, File}
import java.net.{MalformedURLException, URL}
import java.util.{List => JList}
import scala.io.Source
import scala.tools.nsc.interpreter.shell.{Accumulator, ShellConfig}
import scala.util.Using
import scala.util.control.NonFatal

/** A Reader that delegates to JLine3.
*/
Expand Down Expand Up @@ -68,6 +74,31 @@ object Reader {

System.setProperty(LineReader.PROP_SUPPORT_PARSEDLINE, java.lang.Boolean.TRUE.toString())

def inputrcFileUrl(): Option[URL] = {
sys.props
.get("jline.inputrc")
.flatMap { path =>
try Some(new URL(path))
catch {
case _: MalformedURLException =>
Some(new File(path).toURI.toURL)
}
}.orElse {
sys.props.get("user.home").map { home =>
val f = new File(home).toPath.resolve(".inputrc").toFile
(if (f.isFile) f else new File("/etc/inputrc")).toURI.toURL
}
}
}

def urlByteArray(url: URL): Array[Byte] = {
Using.resource(Source.fromURL(url).bufferedReader()) {
bufferedReader =>
LazyList.continually(bufferedReader.read).takeWhile(_ != -1).map(_.toByte).toArray
}
}

lazy val inputrcFileContents: Option[Array[Byte]] = inputrcFileUrl().map(in => urlByteArray(in))
val jlineTerminal = TerminalBuilder.builder().jna(true).build()
val completer = new Completion(completion)
val parser = new ReplParser(repl)
Expand All @@ -94,6 +125,9 @@ object Reader {
}

val reader = builder.build()
try inputrcFileContents.foreach(f => InputRC.configure(reader, new ByteArrayInputStream(f))) catch {
case NonFatal(_) =>
} //ignore
locally {
import LineReader._
// VIINS, VICMD, EMACS
Expand Down

0 comments on commit a6a9cbe

Please sign in to comment.