diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2016-09-15 19:20:53 +0200 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2016-10-10 13:25:33 +0200 |
commit | 15db5f6053badd49aae62a699aa59745288c932d (patch) | |
tree | a14f1ce47b6fab738fdc2feac7d36ccaa1c6a5cf /src/dotty/tools/dotc | |
parent | 11cfc30baf214d5d69d0d5d79b981ad1c131d0e6 (diff) | |
download | dotty-15db5f6053badd49aae62a699aa59745288c932d.tar.gz dotty-15db5f6053badd49aae62a699aa59745288c932d.tar.bz2 dotty-15db5f6053badd49aae62a699aa59745288c932d.zip |
Add smart comment formatting in ConsoleReporter
Diffstat (limited to 'src/dotty/tools/dotc')
-rw-r--r-- | src/dotty/tools/dotc/reporting/ConsoleReporter.scala | 72 |
1 files changed, 65 insertions, 7 deletions
diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index deb772db5..a681c1552 100644 --- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -8,6 +8,7 @@ import core.Contexts._ import Reporter._ import java.io.{ BufferedReader, IOException, PrintWriter } import scala.reflect.internal.util._ +import printing.SyntaxHighlighting._ /** * This class implements a Reporter that displays messages on a text @@ -21,6 +22,7 @@ class ConsoleReporter( /** maximal number of error messages to be printed */ protected def ErrorLimit = 100 +<<<<<<< HEAD def printPos(pos: SourcePosition): Unit = if (pos.exists) { printMessage(pos.lineContent.stripLineEnd) @@ -30,26 +32,82 @@ class ConsoleReporter( printPos(pos.outer) } } +======= + def sourceLine(pos: SourcePosition): (String, Int) = { + val lineNum = s"${pos.line}:" + (lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length) + } + + def columnMarker(pos: SourcePosition, offset: Int) = + if (pos.startLine == pos.endLine) { + val whitespace = " " * (pos.column + offset) + val carets = + AnnotationColor + + ("^" * math.max(1, pos.endColumn - pos.startColumn)) + + NoColor + + whitespace + carets + } else { + " " * (pos.column + offset) + AnnotationColor + "^" + NoColor + } + + def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context) = { + var hasLongLines = false + val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) => + val lineLength = + line.replaceAll("\u001B\\[[;\\d]*m", "").length + val padding = + math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn) + + if (padding < minPad) padding + else minPad + } + + msg + .lines + .map { line => " " * leastWhitespace + line } + .mkString(sys.props("line.separator")) + } + + def posStr(pos: SourcePosition, kind: String)(implicit ctx: Context) = + if (pos.exists) { + val file = pos.source.file.toString + + val outer = if (pos.outer.exists) { + s"This location is in code that was inlined at ${pos.outer}:\n" + + printStr(pos.outer) + "\n" + "-" * ctx.settings.pageWidth.value + } else "" + + s"${Console.CYAN}$kind: $file " + + "-" * math.max(ctx.settings.pageWidth.value - file.length - kind.length - 1, 0) + + "\n" + outer + NoColor + } + else "" /** Prints the message. */ def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() } /** Prints the message with the given position indication. */ - def printMessageAndPos(msg: String, pos: SourcePosition)(implicit ctx: Context): Unit = { - val posStr = if (pos.exists) s"$pos: " else "" - printMessage(posStr + msg) - printPos(pos) + def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = { + printMessage(posStr(pos, kind)) + if (pos.exists) { + val (src, offset) = sourceLine(pos) + val marker = columnMarker(pos, offset) + val err = errorMsg(pos, msg, offset) + + printMessage(List(src, marker, err).mkString("\n")) + } else printMessage(msg) } override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match { case d: Error => - printMessageAndPos(s"error: ${d.message}", d.pos) + printMessageAndPos(d.message, d.pos, "Error in") if (ctx.settings.prompt.value) displayPrompt() case d: ConditionalWarning if !d.enablingOption.value => case d: MigrationWarning => - printMessageAndPos(s"migration warning: ${d.message}", d.pos) + printMessageAndPos(d.message, d.pos, "Migration Warning in") case d: Warning => - printMessageAndPos(s"warning: ${d.message}", d.pos) + printMessageAndPos(d.message, d.pos, "Warning in") case _ => printMessageAndPos(d.message, d.pos) } |