Skip to content

Commit 3635333

Browse files
committed
Add smart comment formatting in ConsoleReporter
1 parent 50c0c72 commit 3635333

File tree

1 file changed

+56
-13
lines changed

1 file changed

+56
-13
lines changed

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

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import core.Contexts._
88
import Reporter._
99
import java.io.{ BufferedReader, IOException, PrintWriter }
1010
import scala.reflect.internal.util._
11+
import printing.SyntaxHighlighting._
1112

1213
/**
1314
* This class implements a Reporter that displays messages on a text
@@ -21,34 +22,76 @@ class ConsoleReporter(
2122
/** maximal number of error messages to be printed */
2223
protected def ErrorLimit = 100
2324

24-
def printSourceLine(pos: SourcePosition) =
25-
printMessage(pos.lineContent.stripLineEnd)
25+
def sourceLine(pos: SourcePosition): (String, Int) = {
26+
val lineNum = s"${pos.line}:"
27+
(lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
28+
}
29+
30+
def columnMarker(pos: SourcePosition, offset: Int) =
31+
if (pos.startLine == pos.endLine) {
32+
val whitespace = " " * (pos.column + offset)
33+
val carets =
34+
AnnotationColor +
35+
("^" * math.max(1, pos.endColumn - pos.startColumn)) +
36+
NoColor
37+
38+
whitespace + carets
39+
} else {
40+
" " * (pos.column + offset) + AnnotationColor + "^" + NoColor
41+
}
42+
43+
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context) = {
44+
var hasLongLines = false
45+
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
46+
val lineLength =
47+
line.replaceAll("\u001B\\[[;\\d]*m", "").length
48+
val padding =
49+
math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
50+
51+
if (padding < minPad) padding
52+
else minPad
53+
}
2654

27-
def printColumnMarker(pos: SourcePosition) =
28-
if (pos.exists) { printMessage(" " * pos.column + "^") }
55+
msg
56+
.lines
57+
.map { line => " " * leastWhitespace + line }
58+
.mkString(sys.props("line.separator"))
59+
}
60+
61+
def posStr(pos: SourcePosition, kind: String)(implicit ctx: Context) =
62+
if (pos.exists) {
63+
val file = pos.source.file.toString
64+
65+
s"${Console.CYAN}$kind: $file " +
66+
"-" * math.max(ctx.settings.pageWidth.value - file.length - kind.length - 1, 0) +
67+
NoColor
68+
}
69+
else ""
2970

3071
/** Prints the message. */
3172
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
3273

3374
/** Prints the message with the given position indication. */
34-
def printMessageAndPos(msg: String, pos: SourcePosition)(implicit ctx: Context): Unit = {
35-
val posStr = if (pos.exists) s"$pos: " else ""
36-
printMessage(posStr + msg)
75+
def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
76+
printMessage(posStr(pos, kind))
3777
if (pos.exists) {
38-
printSourceLine(pos)
39-
printColumnMarker(pos)
40-
}
78+
val (src, offset) = sourceLine(pos)
79+
val marker = columnMarker(pos, offset)
80+
val err = errorMsg(pos, msg, offset)
81+
82+
printMessage(List(src, marker, err).mkString("\n"))
83+
} else printMessage(msg)
4184
}
4285

4386
override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
4487
case d: Error =>
45-
printMessageAndPos(s"error: ${d.message}", d.pos)
88+
printMessageAndPos(d.message, d.pos, "Error in")
4689
if (ctx.settings.prompt.value) displayPrompt()
4790
case d: ConditionalWarning if !d.enablingOption.value =>
4891
case d: MigrationWarning =>
49-
printMessageAndPos(s"migration warning: ${d.message}", d.pos)
92+
printMessageAndPos(d.message, d.pos, "Migration Warning in")
5093
case d: Warning =>
51-
printMessageAndPos(s"warning: ${d.message}", d.pos)
94+
printMessageAndPos(d.message, d.pos, "Warning in")
5295
case _ =>
5396
printMessageAndPos(d.message, d.pos)
5497
}

0 commit comments

Comments
 (0)