Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 495fdb8

Browse files
committed
Merge pull request scala#4576 from som-snytt/issue/9206-more
SI-9206 REPL custom bits
2 parents c8fbc41 + fac81d7 commit 495fdb8

File tree

93 files changed

+174
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

93 files changed

+174
-265
lines changed

src/compiler/scala/tools/nsc/Properties.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ object Properties extends scala.util.PropertiesTrait {
1212
protected def pickJarBasedOn = classOf[Global]
1313

1414
// settings based on jar properties, falling back to System prefixed by "scala."
15+
16+
// messages to display at startup or prompt, format string with string parameters
17+
// Scala version, Java version, JVM name
1518
def residentPromptString = scalaPropOrElse("resident.prompt", "\nnsc> ")
1619
def shellPromptString = scalaPropOrElse("shell.prompt", "%nscala> ")
20+
def shellWelcomeString = scalaPropOrElse("shell.welcome",
21+
"""Welcome to Scala %1$#s (%3$s, Java %2$s).
22+
|Type in expressions for evaluation. Or try :help.""".stripMargin
23+
)
24+
1725
// message to display at EOF (which by default ends with
1826
// a newline so as not to break the user's terminal)
1927
def shellInterruptedString = scalaPropOrElse("shell.interrupted", f":quit$lineSeparator")

src/library/scala/sys/BooleanProp.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ object BooleanProp {
5050
def get: String = "" + value
5151
val clear, enable, disable, toggle = ()
5252
def option = if (isSet) Some(value) else None
53+
//def or[T1 >: Boolean](alt: => T1): T1 = if (value) true else alt
5354

5455
protected def zero = false
5556
}

src/library/scala/sys/Prop.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ trait Prop[+T] {
5858
*/
5959
def option: Option[T]
6060

61+
// Do not open until 2.12.
62+
//** This value if the property is set, an alternative value otherwise. */
63+
//def or[T1 >: T](alt: => T1): T1
64+
6165
/** Removes the property from the underlying map.
6266
*/
6367
def clear(): Unit

src/partest-extras/scala/tools/partest/ReplTest.scala

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
package scala.tools.partest
77

88
import scala.tools.nsc.Settings
9-
import scala.tools.nsc.interpreter.ILoop
9+
import scala.tools.nsc.interpreter.{ ILoop, replProps }
1010
import java.lang.reflect.{ Method => JMethod, Field => JField }
11+
import scala.util.matching.Regex
1112
import scala.util.matching.Regex.Match
1213

1314
/** A class for testing repl code.
@@ -19,30 +20,33 @@ abstract class ReplTest extends DirectTest {
1920
// final because we need to enforce the existence of a couple settings.
2021
final override def settings: Settings = {
2122
val s = super.settings
22-
// s.Yreplsync.value = true
2323
s.Xnojline.value = true
2424
transformSettings(s)
2525
}
26+
def normalize(s: String) = s
2627
/** True for SessionTest to preserve session text. */
2728
def inSession: Boolean = false
28-
/** True to preserve welcome text. */
29+
/** True to preserve welcome header, eliding version number. */
2930
def welcoming: Boolean = false
30-
lazy val welcome = "(Welcome to Scala) version .*".r
31-
def normalize(s: String) = s match {
32-
case welcome(w) => w
33-
case s => s
34-
}
35-
def unwelcoming(s: String) = s match {
36-
case welcome(w) => false
37-
case _ => true
38-
}
31+
lazy val header = replProps.welcome
3932
def eval() = {
4033
val s = settings
4134
log("eval(): settings = " + s)
42-
//ILoop.runForTranscript(code, s).lines drop 1 // not always first line
4335
val lines = ILoop.runForTranscript(code, s, inSession = inSession).lines
44-
if (welcoming) lines map normalize
45-
else lines filter unwelcoming
36+
(if (welcoming) {
37+
val welcome = "(Welcome to Scala).*".r
38+
//val welcome = Regex.quote(header.lines.next).r
39+
//val version = "(.*version).*".r // version on separate line?
40+
//var inHead = false
41+
lines map {
42+
//case s @ welcome() => inHead = true ; s
43+
//case version(s) if inHead => inHead = false ; s
44+
case welcome(s) => s
45+
case s => s
46+
}
47+
} else {
48+
lines drop header.lines.size
49+
}) map normalize
4650
}
4751
def show() = eval() foreach println
4852
}

src/repl-jline/scala/tools/nsc/interpreter/jline/FileBackedHistory.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package scala.tools.nsc.interpreter.jline
77

88
import _root_.jline.console.history.PersistentHistory
99

10-
1110
import scala.tools.nsc.interpreter
12-
import scala.tools.nsc.io.{File, Path}
11+
import scala.reflect.io.{ File, Path }
12+
import scala.tools.nsc.Properties.{ propOrNone, userHome }
1313

1414
/** TODO: file locking.
1515
*/
@@ -85,9 +85,9 @@ object FileBackedHistory {
8585
// val ContinuationChar = '\003'
8686
// val ContinuationNL: String = Array('\003', '\n').mkString
8787

88-
import scala.tools.nsc.Properties.userHome
89-
90-
def defaultFileName = ".scala_history"
88+
final val defaultFileName = ".scala_history"
9189

92-
def defaultFile: File = File(Path(userHome) / defaultFileName)
90+
def defaultFile: File = File(
91+
propOrNone("scala.shell.histfile") map (Path.apply) getOrElse (Path(userHome) / defaultFileName)
92+
)
9393
}

src/repl/scala/tools/nsc/interpreter/Formatting.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ class Formatting(indent: Int) {
3030
}
3131
)
3232
}
33+
object Formatting {
34+
def forPrompt(prompt: String) = new Formatting(prompt.lines.toList.last.length)
35+
}

src/repl/scala/tools/nsc/interpreter/ILoop.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,9 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
5656

5757
private var globalFuture: Future[Boolean] = _
5858

59-
/** Print a welcome message */
60-
def printWelcome() {
61-
echo(s"""
62-
|Welcome to Scala $versionString ($javaVmName, Java $javaVersion).
63-
|Type in expressions to have them evaluated.
64-
|Type :help for more information.""".trim.stripMargin
65-
)
59+
/** Print a welcome message! */
60+
def printWelcome(): Unit = {
61+
Option(replProps.welcome) filter (!_.isEmpty) foreach echo
6662
replinfo("[info] started at " + new java.util.Date)
6763
}
6864

@@ -111,10 +107,6 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
111107
}
112108

113109
class ILoopInterpreter extends IMain(settings, out) {
114-
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
115-
override lazy val formatting: Formatting = new Formatting(
116-
(replProps.promptString format Properties.versionNumberString).lines.toList.last.length
117-
)
118110
override protected def parentClassLoader =
119111
settings.explicitParentLoader.getOrElse( classOf[ILoop].getClassLoader )
120112
}
@@ -680,9 +672,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
680672
}
681673

682674
def verbosity() = {
683-
val old = intp.printResults
684-
intp.printResults = !old
685-
echo("Switched " + (if (old) "off" else "on") + " result printing.")
675+
intp.printResults = !intp.printResults
676+
replinfo(s"Result printing is ${ if (intp.printResults) "on" else "off" }.")
686677
}
687678

688679
/** Run one command submitted by the user. Two values are returned:
@@ -762,7 +753,8 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
762753

763754
private object paste extends Pasted {
764755
import scala.util.matching.Regex.quote
765-
val ContinueString = " | "
756+
val ContinuePrompt = replProps.continuePrompt
757+
val ContinueString = replProps.continueText // " | "
766758
val PromptString = prompt.lines.toList.last
767759
val anyPrompt = s"""\\s*(?:${quote(PromptString.trim)}|${quote(AltPromptString.trim)})\\s*""".r
768760

@@ -806,7 +798,7 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
806798
echo("You typed two blank lines. Starting a new command.")
807799
None
808800
case IR.Incomplete =>
809-
in.readLine(paste.ContinueString) match {
801+
in.readLine(paste.ContinuePrompt) match {
810802
case null =>
811803
// we know compilation is going to fail since we're at EOF and the
812804
// parser thinks the input is still incomplete, but since this is
@@ -949,8 +941,9 @@ object ILoop {
949941
Console.withOut(ostream) {
950942
val output = new JPrintWriter(new OutputStreamWriter(ostream), true) {
951943
// skip margin prefix for continuation lines, unless preserving session text for test
944+
// should test for repl.paste.ContinueString or replProps.continueText.contains(ch)
952945
override def write(str: String) =
953-
if (!inSession && (str forall (ch => ch.isWhitespace || ch == '|'))) () // repl.paste.ContinueString
946+
if (!inSession && (str forall (ch => ch.isWhitespace || ch == '|'))) ()
954947
else super.write(str)
955948
}
956949
val input = new BufferedReader(new StringReader(code.trim + "\n")) {

src/repl/scala/tools/nsc/interpreter/IMain.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
113113
def this() = this(new Settings())
114114

115115
// the expanded prompt but without color escapes and without leading newline, for purposes of indenting
116-
lazy val formatting: Formatting = new Formatting(
117-
(replProps.promptString format Properties.versionNumberString).lines.toList.last.length
118-
)
116+
lazy val formatting = Formatting.forPrompt(replProps.promptText)
119117
lazy val reporter: ReplReporter = new ReplReporter(this)
120118

121119
import formatting.indentCode

src/repl/scala/tools/nsc/interpreter/ReplProps.scala

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
package scala.tools.nsc
77
package interpreter
88

9-
import Properties.shellPromptString
9+
import Properties.{ javaVersion, javaVmName, shellPromptString, shellWelcomeString,
10+
versionString, versionNumberString }
1011
import scala.sys._
1112
import Prop._
13+
import java.util.{ Formattable, FormattableFlags, Formatter }
1214

1315
class ReplProps {
1416
private def bool(name: String) = BooleanProp.keyExists(name)
@@ -22,12 +24,44 @@ class ReplProps {
2224
val trace = bool("scala.repl.trace")
2325
val power = bool("scala.repl.power")
2426

25-
// Handy system prop for shell prompt, or else pick it up from compiler.properties
26-
val promptString = Prop[String]("scala.repl.prompt").option getOrElse (if (info) "%nscala %s> " else shellPromptString)
27-
val prompt = {
27+
def enversion(s: String) = {
28+
import FormattableFlags._
29+
val v = new Formattable {
30+
override def formatTo(formatter: Formatter, flags: Int, width: Int, precision: Int) = {
31+
val version = if ((flags & ALTERNATE) != 0) versionNumberString else versionString
32+
val left = if ((flags & LEFT_JUSTIFY) != 0) "-" else ""
33+
val w = if (width >= 0) s"$width" else ""
34+
val p = if (precision >= 0) s".$precision" else ""
35+
val fmt = s"%${left}${w}${p}s"
36+
formatter.format(fmt, version)
37+
}
38+
}
39+
s.format(v, javaVersion, javaVmName)
40+
}
41+
def encolor(s: String) = {
2842
import scala.io.AnsiColor.{ MAGENTA, RESET }
29-
val p = promptString format Properties.versionNumberString
30-
if (colorOk) s"$MAGENTA$p$RESET" else p
43+
if (colorOk) s"$MAGENTA$s$RESET" else s
44+
}
45+
46+
// Handy system prop for shell prompt, or else pick it up from compiler.properties
47+
val promptString = Prop[String]("scala.repl.prompt").option getOrElse (if (info) "%nscala %#s> " else shellPromptString)
48+
val promptText = enversion(promptString)
49+
val prompt = encolor(promptText)
50+
51+
// Prompt for continued input, will be right-adjusted to width of the primary prompt
52+
val continueString = Prop[String]("scala.repl.continue").option getOrElse "| "
53+
val continueText = {
54+
val text = enversion(continueString)
55+
val margin = promptText.lines.toList.last.length - text.length
56+
if (margin > 0) " " * margin + text else text
57+
}
58+
val continuePrompt = encolor(continueText)
59+
60+
// Next time.
61+
//def welcome = enversion(Prop[String]("scala.repl.welcome") or shellWelcomeString)
62+
def welcome = enversion {
63+
val p = Prop[String]("scala.repl.welcome")
64+
if (p.isSet) p.get else shellWelcomeString
3165
}
3266

3367
/** CSV of paged,across to enable pagination or `-x` style

test/files/jvm/interpreter.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> // basics
53

test/files/jvm/throws-annot-from-java.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> :power
53
** Power User mode enabled - BEEP WHIR GYVE **

test/files/jvm/xml05.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> <city name="San Jos&eacute;"/>
53
res0: scala.xml.Elem = <city name="San Jos&eacute;"/>

test/files/run/class-symbol-contravariant.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> :power
53
** Power User mode enabled - BEEP WHIR GYVE **

test/files/run/constant-type.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> :power
53
** Power User mode enabled - BEEP WHIR GYVE **

test/files/run/constrained-types.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> class Annot(obj: Any) extends annotation.Annotation with annotation.TypeConstraint
53
defined class Annot

test/files/run/kind-repl-command.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> :kind scala.Option
53
scala.Option's kind is F[+A]

test/files/run/lub-visibility.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> // should infer List[scala.collection.immutable.Seq[Nothing]]
53

test/files/run/macro-bundle-repl.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import scala.language.experimental.macros
53
import scala.language.experimental.macros

test/files/run/macro-repl-basic.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import language.experimental.macros
53
import language.experimental.macros

test/files/run/macro-repl-dontexpand.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> def bar1(c: scala.reflect.macros.blackbox.Context) = ???
53
bar1: (c: scala.reflect.macros.blackbox.Context)Nothing

test/files/run/macro-system-properties.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import scala.language.experimental._, scala.reflect.macros.blackbox.Context
53
import scala.language.experimental._

test/files/run/reflection-equality.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> class X {
53
def methodIntIntInt(x: Int, y: Int) = x+y

test/files/run/reflection-magicsymbols-repl.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import scala.reflect.runtime.universe._
53
import scala.reflect.runtime.universe._

test/files/run/reflection-repl-classes.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> class A
53
defined class A

test/files/run/reflection-repl-elementary.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> scala.reflect.runtime.universe.typeOf[List[Nothing]]
53
res0: reflect.runtime.universe.Type = scala.List[Nothing]

test/files/run/reify-repl-fail-gracefully.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import language.experimental.macros
53
import language.experimental.macros

test/files/run/reify_newimpl_22.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import scala.reflect.runtime.universe._
53
import scala.reflect.runtime.universe._

test/files/run/reify_newimpl_23.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> import scala.reflect.runtime.universe._
53
import scala.reflect.runtime.universe._

test/files/run/reify_newimpl_25.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> {
53
import scala.reflect.runtime.universe._

test/files/run/reify_newimpl_26.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Type in expressions to have them evaluated.
2-
Type :help for more information.
31

42
scala> def foo[T]{
53
import scala.reflect.runtime.universe._

0 commit comments

Comments
 (0)