Skip to content

Commit

Permalink
repl: Add compiler options to :reset
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Dec 21, 2021
1 parent 07eb71a commit 11b9e3c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ object Settings {
/** Reset the session to the initial state from when the repl program was
* started
*/
case object Reset extends Command {
case class Reset(arg: String) extends Command
object Reset {
val command: String = ":reset"
}

Expand All @@ -110,7 +111,7 @@ case object Help extends Command {
|:type <expression> evaluate the type of the given expression
|:doc <expression> print the documentation for the given expression
|:imports show import history
|:reset reset the repl to its initial state, forgetting all session entries
|:reset [options] reset the repl to its initial state, forgetting all session entries
|:settings <options> update compiler options, if possible
""".stripMargin
}
Expand All @@ -130,7 +131,7 @@ object ParseResult {
Quit.command -> (_ => Quit),
Quit.alias -> (_ => Quit),
Help.command -> (_ => Help),
Reset.command -> (_ => Reset),
Reset.command -> (arg => Reset(arg)),
Imports.command -> (_ => Imports),
Load.command -> (arg => Load(arg)),
TypeOf.command -> (arg => TypeOf(arg)),
Expand Down
12 changes: 6 additions & 6 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ class ReplDriver(settings: Array[String],
override def sourcesRequired: Boolean = false

/** Create a fresh and initialized context with IDE mode enabled */
private def initialCtx = {
private def initialCtx(settings: List[String]) = {
val rootCtx = initCtx.fresh.addMode(Mode.ReadPositions | Mode.Interactive)
rootCtx.setSetting(rootCtx.settings.YcookComments, true)
rootCtx.setSetting(rootCtx.settings.YreadComments, true)
setupRootCtx(settings, rootCtx)
setupRootCtx(this.settings ++ settings, rootCtx)
}

private def setupRootCtx(settings: Array[String], rootCtx: Context) = {
Expand All @@ -99,8 +99,8 @@ class ReplDriver(settings: Array[String],
* such, when the user enters `:reset` this method should be called to reset
* everything properly
*/
protected def resetToInitial(): Unit = {
rootCtx = initialCtx
protected def resetToInitial(settings: List[String] = Nil): Unit = {
rootCtx = initialCtx(settings)
if (rootCtx.settings.outputDir.isDefault(using rootCtx))
rootCtx = rootCtx.fresh
.setSetting(rootCtx.settings.outputDir, new VirtualDirectory("<REPL compilation output>"))
Expand Down Expand Up @@ -378,8 +378,8 @@ class ReplDriver(settings: Array[String],
out.println(Help.text)
state

case Reset =>
resetToInitial()
case Reset(arg) =>
resetToInitial(tokenize(arg))
initialState

case Imports =>
Expand Down
28 changes: 28 additions & 0 deletions compiler/test-resources/repl/reset-command
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
scala> def f(thread: Thread) = thread.stop()
there were 1 deprecation warning(s); re-run with -deprecation for details
def f(thread: Thread): Unit

scala>:reset -deprecation

scala> def f(thread: Thread) = thread.stop()
1 warning found
-- Deprecation Warning: --------------------------------------------------------
1 | def f(thread: Thread) = thread.stop()
| ^^^^^^^^^^^
|method stop in class Thread is deprecated since : see corresponding Javadoc for more information.
def f(thread: Thread): Unit

scala> def resetNoArgsStillWorks = 1
def resetNoArgsStillWorks: Int

scala>:reset

scala> resetNoArgsStillWorks
-- [E006] Not Found Error: -----------------------------------------------------
1 | resetNoArgsStillWorks
| ^^^^^^^^^^^^^^^^^^^^^
| Not found: resetNoArgsStillWorks
longer explanation available when compiling with `-explain`
1 error found

scala>:settings "-Dfoo=bar baz"

0 comments on commit 11b9e3c

Please sign in to comment.