Skip to content

Commit 7f3aa65

Browse files
committed
Clean up in ConsoleReporter & MessageRendering
1 parent 8ea5836 commit 7f3aa65

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package dotty.tools
22
package dotc
33
package reporting
44

5-
import util.SourcePosition
65
import core.Contexts._
76
import java.io.{ BufferedReader, PrintWriter }
87
import diagnostic.{ Message, MessageContainer }
9-
import diagnostic.messages._
8+
import diagnostic.messages.{ Error, Warning, ConditionalWarning }
109

1110
/**
1211
* This class implements a Reporter that displays messages on a text console
@@ -25,48 +24,40 @@ class ConsoleReporter(
2524
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
2625

2726
/** Prints the message with the given position indication. */
28-
def printMessageAndPos(msg: Message, pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): Boolean = {
29-
printMessage(messageAndPos(msg, pos, diagnosticLevel))
30-
true
31-
}
32-
33-
def printExplanation(m: Message)(implicit ctx: Context): Unit = {
34-
printMessage(explanation(m))
35-
}
36-
37-
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
27+
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
3828
val didPrint = m match {
3929
case m: Error =>
40-
val didPrint = printMessageAndPos(m.contained, m.pos, diagnosticLevel(m))
30+
printMessage(messageAndPos(m.contained, m.pos, diagnosticLevel(m)))
4131
if (ctx.settings.prompt.value) displayPrompt()
42-
didPrint
32+
true
4333
case m: ConditionalWarning if !m.enablingOption.value =>
4434
false
4535
case m =>
46-
printMessageAndPos(m.contained, m.pos, diagnosticLevel(m))
36+
printMessage(messageAndPos(m.contained, m.pos, diagnosticLevel(m)))
37+
true
4738
}
4839

4940
if (didPrint && ctx.shouldExplain(m))
50-
printExplanation(m.contained)
41+
printMessage(explanation(m.contained))
5142
else if (didPrint && m.contained.explanation.nonEmpty)
5243
printMessage("\nlonger explanation available when compiling with `-explain`")
5344
}
5445

55-
def displayPrompt(): Unit = {
56-
writer.print("\na)bort, s)tack, r)esume: ")
57-
writer.flush()
46+
/** Show prompt if `-Xprompt` is passed as a flag to the compiler */
47+
def displayPrompt()(implicit ctx: Context): Unit = {
48+
printMessage("\na)bort, s)tack, r)esume: ")
49+
flush()
5850
if (reader != null) {
5951
val response = reader.read().asInstanceOf[Char].toLower
6052
if (response == 'a' || response == 's') {
6153
Thread.dumpStack()
6254
if (response == 'a')
6355
sys.exit(1)
6456
}
65-
writer.print("\n")
66-
writer.flush()
57+
print("\n")
58+
flush()
6759
}
6860
}
6961

7062
override def flush()(implicit ctx: Context): Unit = { writer.flush() }
7163
}
72-

src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ trait MessageRendering {
5454
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context): String = {
5555
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
5656
val lineLength = stripColor(line).length
57-
val padding =
58-
math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
57+
val currPad = math.min(
58+
math.max(0, ctx.settings.pageWidth.value - offset - lineLength),
59+
offset + pos.startColumn
60+
)
5961

60-
if (padding < minPad) padding
61-
else minPad
62+
math.min(currPad, minPad)
6263
}
6364

6465
msg.lines
65-
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line }
66+
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line}
6667
.mkString(sys.props("line.separator"))
6768
}
6869

@@ -83,9 +84,11 @@ trait MessageRendering {
8384
}).show else ""
8485

8586
def explanation(m: Message)(implicit ctx: Context): String = {
86-
val sb = new StringBuilder(hl"""|
87-
|${Blue("Explanation")}
88-
|${Blue("===========")}""".stripMargin)
87+
val sb = new StringBuilder(
88+
hl"""|
89+
|${Blue("Explanation")}
90+
|${Blue("===========")}"""
91+
)
8992
sb.append('\n').append(m.explanation)
9093
if (m.explanation.lastOption != Some('\n')) sb.append('\n')
9194
sb.toString
@@ -103,7 +106,7 @@ trait MessageRendering {
103106
sb.toString
104107
}
105108

106-
def diagnosticLevel(cont: MessageContainer): String = {
109+
def diagnosticLevel(cont: MessageContainer): String =
107110
cont match {
108111
case m: Error => "Error"
109112
case m: FeatureWarning => "Feature Warning"
@@ -113,5 +116,4 @@ trait MessageRendering {
113116
case m: Warning => "Warning"
114117
case m: Info => "Info"
115118
}
116-
}
117119
}

0 commit comments

Comments
 (0)