Skip to content

Commit

Permalink
Implement :settings in the REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Nov 20, 2021
1 parent e3586bf commit 5923bfe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/MacroClassLoader.scala
Expand Up @@ -23,7 +23,7 @@ object MacroClassLoader {
private def makeMacroClassLoader(using Context): ClassLoader = trace("new macro class loader") {
val entries = ClassPath.expandPath(ctx.settings.classpath.value, expandStar=true)
val urls = entries.map(cp => java.nio.file.Paths.get(cp).toUri.toURL).toArray
val out = ctx.settings.outputDir.value.jpath.toUri.toURL // to find classes in case of suspended compilation
new java.net.URLClassLoader(urls :+ out, getClass.getClassLoader)
val out = Option(ctx.settings.outputDir.value.toURL) // to find classes in case of suspended compilation
new java.net.URLClassLoader(urls ++ out.toList, getClass.getClassLoader)
}
}
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/repl/ParseResult.scala
Expand Up @@ -80,6 +80,11 @@ case object Imports extends Command {
val command: String = ":imports"
}

case class Settings(arg: String) extends Command
object Settings {
val command: String = ":settings"
}

/** Reset the session to the initial state from when the repl program was
* started
*/
Expand Down Expand Up @@ -128,7 +133,8 @@ object ParseResult {
Imports.command -> (_ => Imports),
Load.command -> (arg => Load(arg)),
TypeOf.command -> (arg => TypeOf(arg)),
DocOf.command -> (arg => DocOf(arg))
DocOf.command -> (arg => DocOf(arg)),
Settings.command -> (arg => Settings(arg)),
)

def apply(source: SourceFile)(implicit state: State): ParseResult = {
Expand Down
15 changes: 15 additions & 0 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Expand Up @@ -5,6 +5,7 @@ import java.nio.charset.StandardCharsets

import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.{tpd, untpd}
import dotty.tools.dotc.config.CommandLineParser.tokenize
import dotty.tools.dotc.config.Properties.{javaVersion, javaVmName, simpleVersionString}
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.{unfusedPhases, typerPhase}
Expand Down Expand Up @@ -414,6 +415,20 @@ class ReplDriver(settings: Array[String],
}
state

case Settings(arg) => arg match
case "" =>
given ctx: Context = state.context
for (s <- ctx.settings.userSetSettings(ctx.settingsState).sortBy(_.name))
out.println(s"${s.name} = ${if s.value == "" then "\"\"" else s.value}")
state
case _ =>
setup(tokenize(arg).toArray, rootCtx) match
case Some((files, ictx)) =>
ictx.base.initialize()(using ictx)
rootCtx = ictx
case _ =>
state.copy(context = rootCtx)

case Quit =>
// end of the world!
state
Expand Down
15 changes: 15 additions & 0 deletions compiler/test-resources/repl/settings-command
@@ -0,0 +1,15 @@
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>:settings -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>

0 comments on commit 5923bfe

Please sign in to comment.