Skip to content

Remove Worksheet dependency on JLine #5320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/JLineTerminal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import org.jline.reader.impl.history.DefaultHistory
import org.jline.terminal.TerminalBuilder
import org.jline.utils.AttributedString

final class JLineTerminal(needsTerminal: Boolean) extends java.io.Closeable {
final class JLineTerminal extends java.io.Closeable {
// import java.util.logging.{Logger, Level}
// Logger.getLogger("org.jline").setLevel(Level.FINEST)

private val terminal =
TerminalBuilder.builder()
.dumb(!needsTerminal) // fail early if we need a terminal and can't create one
.dumb(false) // fail early if not able to create a terminal
.build()
private val history = new DefaultHistory

Expand Down
7 changes: 1 addition & 6 deletions compiler/src/dotty/tools/repl/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,5 @@ package dotty.tools.repl
/** Main entry point to the REPL */
object Main {
def main(args: Array[String]): Unit =
new ReplDriver(args).runUntilQuit(needsTerminal = true)
}

object WorksheetMain {
def main(args: Array[String]): Unit =
new ReplDriver(args).runUntilQuit(needsTerminal = false)
new ReplDriver(args).runUntilQuit()
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class ReplDriver(settings: Array[String],
* observable outside of the CLI, for this reason, most helper methods are
* `protected final` to facilitate testing.
*/
final def runUntilQuit(needsTerminal: Boolean, initialState: State = initialState): State = {
val terminal = new JLineTerminal(needsTerminal)
final def runUntilQuit(initialState: State = initialState): State = {
val terminal = new JLineTerminal

/** Blockingly read a line, getting back a parse result */
def readLine(state: State): ParseResult = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package dotty.tools.languageserver.worksheet

import dotty.tools.dotc.core.Contexts.Context

import java.io.{File, PrintStream}
import java.io.{File, PrintStream, IOException}
import java.lang.ProcessBuilder.Redirect
import java.util.concurrent.CancellationException
import java.util.{Timer, TimerTask}

import org.eclipse.lsp4j.jsonrpc.CancelChecker

Expand Down Expand Up @@ -31,7 +34,7 @@ private object Evaluator {
def get(cancelChecker: CancelChecker)(implicit ctx: Context): Option[Evaluator] = synchronized {
val classpath = ctx.settings.classpath.value
previousEvaluator match {
case Some(cp, evaluator) if evaluator.isAlive() && cp == classpath =>
case Some(cp, evaluator) if evaluator.isAlive && cp == classpath =>
evaluator.reset(cancelChecker)
Some(evaluator)
case _ =>
Expand All @@ -52,12 +55,12 @@ private object Evaluator {
*/
private class Evaluator private (javaExec: String,
userClasspath: String,
cancelChecker: CancelChecker) {
private var cancelChecker: CancelChecker) {
private val process =
new ProcessBuilder(
javaExec,
"-classpath", scala.util.Properties.javaClassPath,
dotty.tools.repl.WorksheetMain.getClass.getName.stripSuffix("$"),
ReplProcess.getClass.getName.stripSuffix("$"),
"-classpath", userClasspath,
"-color:never")
.redirectErrorStream(true)
Expand All @@ -67,46 +70,54 @@ private class Evaluator private (javaExec: String,
private val processInput = new PrintStream(process.getOutputStream())

// Messages coming out of the REPL
private val processOutput = new ReplReader(process.getInputStream())
processOutput.start()

// The thread that monitors cancellation
private val cancellationThread = new CancellationThread(cancelChecker, this)
cancellationThread.start()
private val processOutput = new InputStreamConsumer(process.getInputStream())

// A timer that monitors cancellation
private val cancellationTimer = new Timer()
locally {
val task = new TimerTask {
def run(): Unit =
if (!isAlive)
cancellationTimer.cancel()
else
try cancelChecker.checkCanceled()
catch { case _: CancellationException => exit() }
}
val checkCancelledDelayMs = 50L
cancellationTimer.schedule(task, checkCancelledDelayMs, checkCancelledDelayMs)
}

// Wait for the REPL to be ready
processOutput.next()

/** Is the process that runs the REPL still alive? */
def isAlive(): Boolean = process.isAlive()
def isAlive: Boolean = process.isAlive

/**
* Submit `command` to the REPL, wait for the result.
*
* @param command The command to evaluate.
* @return The result from the REPL.
*/
def eval(command: String): Option[String] = {
processInput.println(command)
def eval(command: String): String = {
processInput.print(command)
processInput.print(InputStreamConsumer.delimiter)
processInput.flush()
processOutput.next().map(_.trim)

try processOutput.next().trim
catch { case _: IOException => "" }
}

/**
* Reset the REPL to its initial state, update the cancel checker.
*/
def reset(cancelChecker: CancelChecker): Unit = {
cancellationThread.setCancelChecker(cancelChecker)
this.cancelChecker = cancelChecker
eval(":reset")
}

/** Terminate this JVM. */
def exit(): Unit = {
processOutput.interrupt()
process.destroyForcibly()
Evaluator.previousEvaluator = None
cancellationThread.interrupt()
cancellationTimer.cancel()
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dotty.tools.languageserver.worksheet

import java.io.{InputStream, IOException}
import java.util.Scanner

class InputStreamConsumer(in: InputStream) {
private[this] val scanner =
new Scanner(in).useDelimiter(InputStreamConsumer.delimiter)

/** Finds and returns the next complete token from this input stream.
*
* A complete token is preceded and followed by input that matches the delimiter pattern.
* This method may block while waiting for input
*/
def next(): String =
try scanner.next()
catch {
case _: NoSuchElementException =>
throw new IOException("InputStream closed")
}
}

object InputStreamConsumer {
def delimiter = "\uE000" // withing private use area
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dotty.tools.languageserver.worksheet

import dotty.tools.repl.ReplDriver

object ReplProcess {
def main(args: Array[String]): Unit = {
val driver = new ReplDriver(args)
val in = new InputStreamConsumer(System.in)
var state = driver.initialState

while (true) {
val code = in.next() // blocking
state = driver.run(code)(state)
print(InputStreamConsumer.delimiter) // needed to mark the end of REPL output
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object Worksheet {
}
queries.foreach { (line, code) =>
cancelChecker.checkCanceled()
val res = evaluator.eval(code).getOrElse("")
val res = evaluator.eval(code)
cancelChecker.checkCanceled()
if (res.nonEmpty)
sendMessage(line, res)
Expand Down
2 changes: 1 addition & 1 deletion sbt-bridge/src/xsbt/ConsoleInterface.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ConsoleInterface {

val s1 = driver.run(initialCommands)(s0)
// TODO handle failure during initialisation
val s2 = driver.runUntilQuit(needsTerminal = true, s1)
val s2 = driver.runUntilQuit(s1)
driver.run(cleanupCommands)(s2)
}
}