Skip to content

Commit f992c16

Browse files
author
Rajesh Veeranki
committed
Added runWrapper to perform initial/cleanup commands without printing output
1 parent b5c3b1c commit f992c16

File tree

4 files changed

+30
-16
lines changed

4 files changed

+30
-16
lines changed

compiler/src/dotty/tools/repl/Main.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package dotty.tools.repl
33
/** Main entry point to the REPL */
44
object Main {
55
def main(args: Array[String]): Unit =
6-
new ReplDriver(args).runUntilQuit()
6+
new ReplDriver(args).runWrapper()
77
}

compiler/src/dotty/tools/repl/ParseResult.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ import dotc.reporting._
1010

1111
import results._
1212

13+
sealed trait Parsing
14+
1315
/** A parsing result from string input */
14-
sealed trait ParseResult
16+
sealed trait ParseResult extends Parsing
17+
18+
/** Suppress visual output when this is passed */
19+
case class Silent(underlying: ParseResult) extends Parsing
1520

1621
/** An error free parsing resulting in a list of untyped trees */
1722
case class Parsed(sourceCode: String, trees: List[untpd.Tree]) extends ParseResult
@@ -27,11 +32,6 @@ case object Newline extends ParseResult
2732
/** `ctrl-c` obtained from input string */
2833
case object SigKill extends ParseResult
2934

30-
/** Suppress visual output when this is passed */
31-
case class Silent(underlying: ParseResult) extends ParseResult {
32-
require(!underlying.isInstanceOf[Silent])
33-
}
34-
3535
/** A command is on the format:
3636
*
3737
* ```none

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ case class Completions(cursor: Int,
7777
/** Main REPL instance, orchestrating input, compilation and presentation */
7878
class ReplDriver(settings: Array[String],
7979
protected val out: PrintStream = System.out,
80-
protected val classLoader: Option[ClassLoader] = None) extends Driver {
80+
protected val classLoader: Option[ClassLoader] = None,
81+
initialCommands: Array[String] = Array.empty,
82+
cleanupCommands: Array[String] = Array.empty) extends Driver {
8183

8284
/** Overridden to `false` in order to not have to give sources on the
8385
* commandline
@@ -139,12 +141,24 @@ class ReplDriver(settings: Array[String],
139141
}
140142
}
141143

144+
final def runWrapper(initialState: State = initState): State = {
145+
val state = runBootstrapCommands(initialCommands)(initialState)
146+
val userState = runUntilQuit(state)
147+
runBootstrapCommands(cleanupCommands)(userState)
148+
}
149+
142150
final def run(input: String)(implicit state: State): State =
143151
run(ParseResult(input)(state.run.runContext))(state.newRun(compiler, rootCtx))
144152

145153
final def run(res: ParseResult)(implicit state: State): State =
146154
interpret(res)
147155

156+
final def runBootstrapCommands(cmds: Array[String])(implicit state: State): State = {
157+
cmds.map(ParseResult.apply(_)(rootCtx)).map(Silent.apply(_)).foldLeft(state) { (s, cmd) =>
158+
interpret(cmd)(s)
159+
}
160+
}
161+
148162
/** Extract possible completions at the index of `cursor` in `expr` */
149163
protected[this] final def completions(cursor: Int, expr: String, state0: State): Completions = {
150164
// TODO move some of this logic to `Interactive`
@@ -179,12 +193,12 @@ class ReplDriver(settings: Array[String],
179193
private def extractImports(trees: List[untpd.Tree])(implicit context: Context): List[(untpd.Import, String)] =
180194
trees.collect { case imp: untpd.Import => (imp, imp.show) }
181195

182-
private def interpret(res: ParseResult)(implicit state: State): State = {
183-
val isSilent = res.isInstanceOf[Silent]
184-
val parseResult = res match {
185-
case Silent(v) => v
186-
case _ => res
196+
private def interpret(res: Parsing)(implicit state: State): State = {
197+
val (parseResult, isSilent) = res match {
198+
case Silent(x) => (x, true)
199+
case x: ParseResult => (x, false)
187200
}
201+
188202
parseResult match {
189203
case parsed: Parsed =>
190204
compile(parsed, isSilent)

sbt-bridge/src/xsbt/ConsoleInterface.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class ConsoleInterface {
3737
//
3838
// 1. Introduce `case class Silent(res: ParseResult) extends ParseResult`
3939
// 2. Perform all steps in `interpret` as usual without printing to `out`
40-
initialCommands: String,
41-
cleanupCommands: String,
40+
initialCommands: Array[String],
41+
cleanupCommands: Array[String],
4242
loader: ClassLoader,
4343
bindNames: Array[String],
4444
bindValues: Array[Any],
@@ -51,6 +51,6 @@ class ConsoleInterface {
5151
} ++
5252
Array("-classpath", classpathString)
5353

54-
new ReplDriver(completeArgs, classLoader = Some(loader)).runUntilQuit()
54+
new ReplDriver(completeArgs, classLoader = Some(loader), initialCommands = initialCommands, cleanupCommands = cleanupCommands).runWrapper()
5555
}
5656
}

0 commit comments

Comments
 (0)