diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 6a8a88a429e5..599812a9a390 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -126,6 +126,7 @@ trait CommonScalaSettings: val encoding: Setting[String] = StringSetting(RootSetting, "encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding, aliases = List("--encoding")) val usejavacp: Setting[Boolean] = BooleanSetting(RootSetting, "usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path")) val scalajs: Setting[Boolean] = BooleanSetting(RootSetting, "scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs")) + val replInitScript: Setting[String] = StringSetting(RootSetting, "repl-init-script", "code", "The code will be run on REPL startup.", "", aliases = List("--repl-init-script")) end CommonScalaSettings /** -P "plugin" settings. Various tools might support plugins. */ diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index 3a2b8ce00bbe..96aa31b8753b 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -116,7 +116,12 @@ class ReplDriver(settings: Array[String], } /** the initial, empty state of the REPL session */ - final def initialState: State = State(0, 0, Map.empty, Set.empty, false, rootCtx) + final def initialState: State = + val emptyState = State(0, 0, Map.empty, Set.empty, false, rootCtx) + val initScript = rootCtx.settings.replInitScript.value(using rootCtx) + initScript.trim() match + case "" => emptyState + case script => run(script)(using emptyState) /** Reset state of repl to the initial state * diff --git a/compiler/test-resources/repl/init-script-flag b/compiler/test-resources/repl/init-script-flag new file mode 100644 index 000000000000..373f21e15e93 --- /dev/null +++ b/compiler/test-resources/repl/init-script-flag @@ -0,0 +1,5 @@ +scala>:reset --repl-init-script:'println("Hello from init script!")' +Resetting REPL state with the following settings: + --repl-init-script:println("Hello from init script!") + +Hello from init script!