Skip to content

Commit 624db95

Browse files
committed
Dedupe ConsoleReporter & ReplConsoleReporter
1 parent 1632cb9 commit 624db95

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ import Diagnostic.{ Error, ConditionalWarning }
1212
class ConsoleReporter(
1313
reader: BufferedReader = Console.in,
1414
writer: PrintWriter = new PrintWriter(Console.err, true)
15-
) extends AbstractReporter {
15+
) extends ConsoleReporter.AbstractConsoleReporter {
16+
override def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
17+
override def flush()(using Context): Unit = writer.flush()
1618

17-
import Diagnostic._
18-
19-
/** Prints the message. */
20-
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
21-
22-
/** Prints the message with the given position indication. */
23-
def doReport(dia: Diagnostic)(using Context): Unit = {
19+
override def doReport(dia: Diagnostic)(using Context): Unit = {
20+
super.doReport(dia)
2421
dia match
25-
case dia: Error =>
26-
printMessage(messageAndPos(dia))
27-
if (ctx.settings.Xprompt.value) Reporter.displayPrompt(reader, writer)
28-
case dia =>
29-
printMessage(messageAndPos(dia))
30-
31-
if shouldExplain(dia) then
32-
printMessage(explanation(dia.msg))
33-
else if dia.msg.canExplain then
34-
printMessage("\nlonger explanation available when compiling with `-explain`")
22+
case dia: Error if ctx.settings.Xprompt.value => Reporter.displayPrompt(reader, writer)
23+
case _ =>
3524
}
25+
}
26+
27+
object ConsoleReporter {
28+
abstract class AbstractConsoleReporter extends AbstractReporter {
29+
/** Prints the message. */
30+
def printMessage(msg: String): Unit
3631

37-
override def flush()(using Context): Unit = { writer.flush() }
32+
/** Prints the message with the given position indication. */
33+
def doReport(dia: Diagnostic)(using Context): Unit = {
34+
printMessage(messageAndPos(dia))
35+
if Diagnostic.shouldExplain(dia) then
36+
printMessage(explanation(dia.msg))
37+
else if dia.msg.canExplain then
38+
printMessage("\nlonger explanation available when compiling with `-explain`")
39+
}
40+
}
3841
}

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import dotty.tools.dotc.core.Symbols.{Symbol, defn}
2020
import dotty.tools.dotc.interfaces
2121
import dotty.tools.dotc.interactive.Completion
2222
import dotty.tools.dotc.printing.SyntaxHighlighting
23-
import dotty.tools.dotc.reporting.{AbstractReporter, MessageRendering, StoreReporter}
23+
import dotty.tools.dotc.reporting.{ConsoleReporter, MessageRendering, StoreReporter}
2424
import dotty.tools.dotc.reporting.{Message, Diagnostic}
2525
import dotty.tools.dotc.util.Spans.Span
2626
import dotty.tools.dotc.util.{SourceFile, SourcePosition}
@@ -425,20 +425,12 @@ class ReplDriver(settings: Array[String],
425425
state
426426
}
427427

428-
/** Like ConsoleReporter, but without file paths or real -Xprompt'ing */
429-
private object ReplConsoleReporter extends AbstractReporter {
430-
def printMessage(msg: String): Unit = out.println(msg)
431-
432-
def doReport(dia: Diagnostic)(using Context): Unit = {
433-
printMessage(messageAndPos(dia))
434-
435-
if Diagnostic.shouldExplain(dia) then
436-
printMessage(explanation(dia.msg))
437-
else if dia.msg.canExplain then
438-
printMessage("\nlonger explanation available when compiling with `-explain`")
439-
}
440-
428+
/** Like ConsoleReporter, but without file paths, -Xprompt displaying,
429+
* and using a PrintStream rather than a PrintWriter so messages aren't re-encoded. */
430+
private object ReplConsoleReporter extends ConsoleReporter.AbstractConsoleReporter {
441431
override def posFileStr(pos: SourcePosition) = "" // omit file paths
432+
override def printMessage(msg: String): Unit = out.println(msg)
433+
override def flush()(using Context): Unit = out.flush()
442434
}
443435

444436
/** Print warnings & errors using ReplConsoleReporter, and info straight to out */

0 commit comments

Comments
 (0)