From bcb229ee4c9e92b95d40a62ba4fe4197993e7361 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Fri, 13 Dec 2024 12:53:50 +0100 Subject: [PATCH 1/5] Add REPL init script setting --- compiler/src/dotty/tools/dotc/config/ScalaSettings.scala | 2 ++ compiler/src/dotty/tools/repl/ReplDriver.scala | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 7ac01b825bdd..8f474f68bd87 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -134,6 +134,8 @@ trait CommonScalaSettings: val encoding: Setting[String] = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding, aliases = List("--encoding")) val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path")) val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs")) + val replInitScript: Setting[String] = StringSetting("-repl-init-script", "code", "The code will be run on REPL startup.", "") + 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 36448a472ad9..d7ba2d3ce193 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -102,7 +102,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 * From ff28432796ca860a9c4e1ecb5998c7979372cb89 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Fri, 14 Mar 2025 10:23:43 +0100 Subject: [PATCH 2/5] Add REPL init script setting [Cherry-picked 5ea7c13070145c62111f9cb230ec2c4c2190b43f][modified] From 05d69a0460dfc8d0d26578648fdb9ea155d258c1 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 8 Jan 2025 15:44:13 +0100 Subject: [PATCH 3/5] Update default value --- compiler/src/dotty/tools/dotc/config/ScalaSettings.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 8f474f68bd87..6928e0617069 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -134,7 +134,7 @@ trait CommonScalaSettings: val encoding: Setting[String] = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding, aliases = List("--encoding")) val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path")) val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs")) - val replInitScript: Setting[String] = StringSetting("-repl-init-script", "code", "The code will be run on REPL startup.", "") + val replInitScript: Setting[String] = StringSetting("-repl-init-script", "code", "The code will be run on REPL startup.", "", aliases = List("--repl-init-script")) end CommonScalaSettings From cd091ef9a6fcf7c217acada4ff1be53cb2ad9f24 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Fri, 14 Mar 2025 10:24:44 +0100 Subject: [PATCH 4/5] Update default value [Cherry-picked 00e07c46ff3c29ae053508bedac21d29ccdd930c][modified] From 93d35e097720b8a631ec12e17c01988c2b558597 Mon Sep 17 00:00:00 2001 From: noti0na1 Date: Wed, 8 Jan 2025 15:52:51 +0100 Subject: [PATCH 5/5] Add a script test [Cherry-picked 87e084e4dab871ea3787addea3b233dd152fe07a] --- compiler/test-resources/repl/init-script-flag | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 compiler/test-resources/repl/init-script-flag 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!