Skip to content

REPL: open up for extensibility #16276

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 4 commits into from
Nov 17, 2022
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
5 changes: 3 additions & 2 deletions compiler/src/dotty/tools/repl/JLineTerminal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import org.jline.reader.impl.history.DefaultHistory
import org.jline.terminal.TerminalBuilder
import org.jline.utils.AttributedString

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

Expand All @@ -30,7 +30,8 @@ final class JLineTerminal extends java.io.Closeable {
private def blue(str: String)(using Context) =
if (ctx.settings.color.value != "never") Console.BLUE + str + Console.RESET
else str
private def prompt(using Context) = blue("\nscala> ")
protected def promptStr = "scala"
private def prompt(using Context) = blue(s"\n$promptStr> ")
private def newLinePrompt(using Context) = blue(" | ")

/** Blockingly read line from `System.in`
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/repl/Rendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ private[repl] class Rendering(parentClassLoader: Option[ClassLoader] = None):

import Rendering._

private var myClassLoader: AbstractFileClassLoader = _
var myClassLoader: AbstractFileClassLoader = _

/** (value, maxElements, maxCharacters) => String */
private var myReplStringOf: (Object, Int, Int) => String = _
var myReplStringOf: (Object, Int, Int) => String = _

/** Class loader used to load compiled code */
private[repl] def classLoader()(using Context) =
Expand Down
25 changes: 16 additions & 9 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class ReplDriver(settings: Array[String],
private var rootCtx: Context = _
private var shouldStart: Boolean = _
private var compiler: ReplCompiler = _
private var rendering: Rendering = _
protected var rendering: Rendering = _

// initialize the REPL session as part of the constructor so that once `run`
// is called, we're in business
Expand All @@ -138,7 +138,7 @@ 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(using initialState: State = initialState)(): State = {
def runUntilQuit(using initialState: State = initialState)(): State = {
val terminal = new JLineTerminal

out.println(
Expand Down Expand Up @@ -176,7 +176,12 @@ class ReplDriver(settings: Array[String],
interpret(ParseResult.complete(input))
}

private def runBody(body: => State): State = rendering.classLoader()(using rootCtx).asContext(withRedirectedOutput(body))
final def runQuietly(input: String)(using State): State = runBody {
val parsed = ParseResult(input)
interpret(parsed, quiet = true)
}

protected def runBody(body: => State): State = rendering.classLoader()(using rootCtx).asContext(withRedirectedOutput(body))

// TODO: i5069
final def bind(name: String, value: Any)(using state: State): State = state
Expand Down Expand Up @@ -242,10 +247,10 @@ class ReplDriver(settings: Array[String],
.getOrElse(Nil)
end completions

private def interpret(res: ParseResult)(using state: State): State = {
protected def interpret(res: ParseResult, quiet: Boolean = false)(using state: State): State = {
res match {
case parsed: Parsed if parsed.trees.nonEmpty =>
compile(parsed, state)
compile(parsed, state, quiet)

case SyntaxErrors(_, errs, _) =>
displayErrors(errs)
Expand All @@ -263,7 +268,7 @@ class ReplDriver(settings: Array[String],
}

/** Compile `parsed` trees and evolve `state` in accordance */
private def compile(parsed: Parsed, istate: State): State = {
private def compile(parsed: Parsed, istate: State, quiet: Boolean = false): State = {
def extractNewestWrapper(tree: untpd.Tree): Name = tree match {
case PackageDef(_, (obj: untpd.ModuleDef) :: Nil) => obj.name.moduleClassName
case _ => nme.NO_NAME
Expand Down Expand Up @@ -314,9 +319,11 @@ class ReplDriver(settings: Array[String],
given Ordering[Diagnostic] =
Ordering[(Int, Int, Int)].on(d => (d.pos.line, -d.level, d.pos.column))

(definitions ++ warnings)
.sorted
.foreach(printDiagnostic)
if (!quiet) {
(definitions ++ warnings)
.sorted
.foreach(printDiagnostic)
}

updatedState
}
Expand Down