Skip to content

Commit 583a05d

Browse files
authored
Merge pull request #173 from scala/backport-lts-3.3-22206
Backport "Partial Fix scala#21242: Add REPL init script setting" to 3.3 LTS
2 parents f39226a + 93d35e0 commit 583a05d

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ trait CommonScalaSettings:
134134
val encoding: Setting[String] = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding, aliases = List("--encoding"))
135135
val usejavacp: Setting[Boolean] = BooleanSetting("-usejavacp", "Utilize the java.class.path in classpath resolution.", aliases = List("--use-java-class-path"))
136136
val scalajs: Setting[Boolean] = BooleanSetting("-scalajs", "Compile in Scala.js mode (requires scalajs-library.jar on the classpath).", aliases = List("--scalajs"))
137+
val replInitScript: Setting[String] = StringSetting("-repl-init-script", "code", "The code will be run on REPL startup.", "", aliases = List("--repl-init-script"))
138+
137139
end CommonScalaSettings
138140

139141
/** -P "plugin" settings. Various tools might support plugins. */

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ class ReplDriver(settings: Array[String],
102102
}
103103

104104
/** the initial, empty state of the REPL session */
105-
final def initialState: State = State(0, 0, Map.empty, Set.empty, false, rootCtx)
105+
final def initialState: State =
106+
val emptyState = State(0, 0, Map.empty, Set.empty, false, rootCtx)
107+
val initScript = rootCtx.settings.replInitScript.value(using rootCtx)
108+
initScript.trim() match
109+
case "" => emptyState
110+
case script => run(script)(using emptyState)
106111

107112
/** Reset state of repl to the initial state
108113
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
scala>:reset --repl-init-script:'println("Hello from init script!")'
2+
Resetting REPL state with the following settings:
3+
--repl-init-script:println("Hello from init script!")
4+
5+
Hello from init script!

0 commit comments

Comments
 (0)