Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Feb 22, 2021
1 parent 5f0f964 commit 8bf6996
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/repl-frontend/scala/tools/nsc/interpreter/jline/Reader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
package scala.tools.nsc.interpreter
package jline

import java.util.{List => JList}

import org.jline.reader.{Candidate, Completer, CompletingParsedLine, EOFError, EndOfFileException, History, LineReader, ParsedLine, Parser, SyntaxError, UserInterruptException}
import org.jline.builtins.InputRC
import org.jline.reader.Parser.ParseContext
import org.jline.reader._
import org.jline.reader.impl.{DefaultParser, LineReaderImpl}
import org.jline.terminal.Terminal

import shell.{Accumulator, ShellConfig}
import Parser.ParseContext
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 @@ -69,6 +74,32 @@ 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 @@ -95,6 +126,10 @@ 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 8bf6996

Please sign in to comment.