Skip to content

Commit d0ff1ce

Browse files
natsukagamitgodzik
authored andcommitted
REPL: Add back :silent command (scala#22248)
[Cherry-picked 3e62731]
1 parent a023203 commit d0ff1ce

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ object Reset {
9393
val command: String = ":reset"
9494
}
9595

96+
/** Toggle automatic printing of results */
97+
case object Silent extends Command:
98+
val command: String = ":silent"
99+
96100
/** `:quit` exits the repl */
97101
case object Quit extends Command {
98102
val command: String = ":quit"
@@ -113,6 +117,7 @@ case object Help extends Command {
113117
|:imports show import history
114118
|:reset [options] reset the repl to its initial state, forgetting all session entries
115119
|:settings <options> update compiler options, if possible
120+
|:silent disable/enable automatic printing of results
116121
""".stripMargin
117122
}
118123

@@ -137,6 +142,7 @@ object ParseResult {
137142
TypeOf.command -> (arg => TypeOf(arg)),
138143
DocOf.command -> (arg => DocOf(arg)),
139144
Settings.command -> (arg => Settings(arg)),
145+
Silent.command -> (_ => Silent),
140146
)
141147

142148
def apply(source: SourceFile)(using state: State): ParseResult = {

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

+11-14
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ import scala.util.Using
5959
* @param valIndex the index of next value binding for free expressions
6060
* @param imports a map from object index to the list of user defined imports
6161
* @param invalidObjectIndexes the set of object indexes that failed to initialize
62+
* @param quiet whether we print evaluation results
6263
* @param context the latest compiler context
6364
*/
6465
case class State(objectIndex: Int,
6566
valIndex: Int,
6667
imports: Map[Int, List[tpd.Import]],
6768
invalidObjectIndexes: Set[Int],
69+
quiet: Boolean,
6870
context: Context):
6971
def validObjectIndexes = (1 to objectIndex).filterNot(invalidObjectIndexes.contains(_))
7072

@@ -100,7 +102,7 @@ class ReplDriver(settings: Array[String],
100102
}
101103

102104
/** the initial, empty state of the REPL session */
103-
final def initialState: State = State(0, 0, Map.empty, Set.empty, rootCtx)
105+
final def initialState: State = State(0, 0, Map.empty, Set.empty, false, rootCtx)
104106

105107
/** Reset state of repl to the initial state
106108
*
@@ -203,11 +205,6 @@ class ReplDriver(settings: Array[String],
203205
interpret(ParseResult.complete(input))
204206
}
205207

206-
final def runQuietly(input: String)(using State): State = runBody {
207-
val parsed = ParseResult(input)
208-
interpret(parsed, quiet = true)
209-
}
210-
211208
protected def runBody(body: => State): State = rendering.classLoader()(using rootCtx).asContext(withRedirectedOutput(body))
212209

213210
// TODO: i5069
@@ -291,10 +288,10 @@ class ReplDriver(settings: Array[String],
291288
.getOrElse(Nil)
292289
end completionsWithSignatures
293290

294-
protected def interpret(res: ParseResult, quiet: Boolean = false)(using state: State): State = {
291+
protected def interpret(res: ParseResult)(using state: State): State = {
295292
res match {
296293
case parsed: Parsed if parsed.trees.nonEmpty =>
297-
compile(parsed, state, quiet)
294+
compile(parsed, state)
298295

299296
case SyntaxErrors(_, errs, _) =>
300297
displayErrors(errs)
@@ -312,7 +309,7 @@ class ReplDriver(settings: Array[String],
312309
}
313310

314311
/** Compile `parsed` trees and evolve `state` in accordance */
315-
private def compile(parsed: Parsed, istate: State, quiet: Boolean = false): State = {
312+
private def compile(parsed: Parsed, istate: State): State = {
316313
def extractNewestWrapper(tree: untpd.Tree): Name = tree match {
317314
case PackageDef(_, (obj: untpd.ModuleDef) :: Nil) => obj.name.moduleClassName
318315
case _ => nme.NO_NAME
@@ -363,11 +360,9 @@ class ReplDriver(settings: Array[String],
363360
given Ordering[Diagnostic] =
364361
Ordering[(Int, Int, Int)].on(d => (d.pos.line, -d.level, d.pos.column))
365362

366-
if (!quiet) {
367-
(definitions ++ warnings)
368-
.sorted
369-
.foreach(printDiagnostic)
370-
}
363+
(if istate.quiet then warnings else definitions ++ warnings)
364+
.sorted
365+
.foreach(printDiagnostic)
371366

372367
updatedState
373368
}
@@ -543,6 +538,8 @@ class ReplDriver(settings: Array[String],
543538
rootCtx = setupRootCtx(tokenize(arg).toArray, rootCtx)
544539
state.copy(context = rootCtx)
545540

541+
case Silent => state.copy(quiet = !state.quiet)
542+
546543
case Quit =>
547544
// end of the world!
548545
state

compiler/test-resources/repl/silent

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
scala>:silent
2+
scala> 1+1
3+
scala> case class A(x: Int)
4+
scala> A("string")
5+
-- [E007] Type Mismatch Error: -------------------------------------------------
6+
1 | A("string")
7+
| ^^^^^^^^
8+
| Found: ("string" : String)
9+
| Required: Int
10+
|
11+
| longer explanation available when compiling with `-explain`
12+
1 error found
13+
scala> Option[Int](2) match { case Some(x) => x }
14+
1 warning found
15+
-- [E029] Pattern Match Exhaustivity Warning: ----------------------------------
16+
1 | Option[Int](2) match { case Some(x) => x }
17+
| ^^^^^^^^^^^^^^
18+
| match may not be exhaustive.
19+
|
20+
| It would fail on pattern case: None
21+
|
22+
| longer explanation available when compiling with `-explain`
23+
scala>:silent
24+
scala> 1 + 2
25+
val res2: Int = 3

compiler/test/dotty/tools/repl/TabcompleteTests.scala

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class TabcompleteTests extends ReplTest {
220220
":quit",
221221
":reset",
222222
":settings",
223+
":silent",
223224
":type"
224225
),
225226
tabComplete(":")

0 commit comments

Comments
 (0)