Skip to content

Commit f9328ba

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

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-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: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,30 @@ import util.SourcePosition
1212
import scala.collection.mutable
1313

1414
trait MessageRendering {
15+
/** Remove ANSI coloring from `str`, useful for getting real length of
16+
* strings
17+
*
18+
* @return string stripped of ANSI escape codes
19+
*/
1520
def stripColor(str: String): String =
1621
str.replaceAll("\u001B\\[[;\\d]*m", "")
1722

23+
/** When inlining a method call, if there's an error we'd like to get the
24+
* outer context and the `pos` at which the call was inlined.
25+
*
26+
* @return a list of strings with inline locations
27+
*/
1828
def outer(pos: SourcePosition, prefix: String)(implicit ctx: Context): List[String] =
1929
if (pos.outer.exists) {
2030
s"$prefix| This location is in code that was inlined at ${pos.outer}" ::
2131
outer(pos.outer, prefix)
2232
} else Nil
2333

34+
/** Get the sourcelines before and after the position, as well as the offset
35+
* for rendering line numbers
36+
*
37+
* @return (lines before error, lines after error, line numbers offset)
38+
*/
2439
def sourceLines(pos: SourcePosition)(implicit ctx: Context): (List[String], List[String], Int) = {
2540
var maxLen = Int.MinValue
2641
def render(xs: List[Int]) =
@@ -39,6 +54,7 @@ trait MessageRendering {
3954
(render(before), render(after), maxLen)
4055
}
4156

57+
/** The column markers aligned under the error */
4258
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context): String = {
4359
val prefix = " " * (offset - 1)
4460
val whitespace = " " * pos.startColumn
@@ -51,21 +67,30 @@ trait MessageRendering {
5167
s"$prefix|$whitespace${carets.show}"
5268
}
5369

70+
/** The error message (`msg`) aligned under `pos`
71+
*
72+
* @return aligned error message
73+
*/
5474
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context): String = {
5575
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
5676
val lineLength = stripColor(line).length
57-
val padding =
58-
math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
77+
val currPad = math.min(
78+
math.max(0, ctx.settings.pageWidth.value - offset - lineLength),
79+
offset + pos.startColumn
80+
)
5981

60-
if (padding < minPad) padding
61-
else minPad
82+
math.min(currPad, minPad)
6283
}
6384

6485
msg.lines
65-
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line }
86+
.map { line => " " * (offset - 1) + "|" + (" " * (leastWhitespace - offset)) + line}
6687
.mkString(sys.props("line.separator"))
6788
}
6889

90+
/** The separator between errors containing the source file and error type
91+
*
92+
* @return separator containing error location and kind
93+
*/
6994
def posStr(pos: SourcePosition, diagnosticLevel: String, message: Message)(implicit ctx: Context): String =
7095
if (pos.exists) Blue({
7196
val file = pos.source.file.toString
@@ -82,15 +107,19 @@ trait MessageRendering {
82107
("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0))
83108
}).show else ""
84109

110+
/** Explanation rendered under "Explanation" header */
85111
def explanation(m: Message)(implicit ctx: Context): String = {
86-
val sb = new StringBuilder(hl"""|
87-
|${Blue("Explanation")}
88-
|${Blue("===========")}""".stripMargin)
112+
val sb = new StringBuilder(
113+
hl"""|
114+
|${Blue("Explanation")}
115+
|${Blue("===========")}"""
116+
)
89117
sb.append('\n').append(m.explanation)
90118
if (m.explanation.lastOption != Some('\n')) sb.append('\n')
91119
sb.toString
92120
}
93121

122+
/** The whole message rendered from `msg` */
94123
def messageAndPos(msg: Message, pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): String = {
95124
val sb = mutable.StringBuilder.newBuilder
96125
sb.append(posStr(pos, diagnosticLevel, msg)).append('\n')
@@ -103,7 +132,7 @@ trait MessageRendering {
103132
sb.toString
104133
}
105134

106-
def diagnosticLevel(cont: MessageContainer): String = {
135+
def diagnosticLevel(cont: MessageContainer): String =
107136
cont match {
108137
case m: Error => "Error"
109138
case m: FeatureWarning => "Feature Warning"
@@ -113,5 +142,4 @@ trait MessageRendering {
113142
case m: Warning => "Warning"
114143
case m: Info => "Info"
115144
}
116-
}
117145
}

0 commit comments

Comments
 (0)