From 628b7f317756ce2c366359a7399b8dda9d0190b7 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Sun, 18 Sep 2016 08:45:29 +0200 Subject: Make `FancyConsoleReporter` and `Highlighting` obey color setting Fancy console reporter and the string interpolator for highlighting now obey the color setting - this means that the next step towards unifying the reporters is to make sure the tests work with `FancyConsoleReporter` under the `-color:never` flag. --- src/dotty/tools/dotc/Driver.scala | 6 +-- src/dotty/tools/dotc/printing/Highlighting.scala | 33 +++++++++---- .../tools/dotc/printing/SyntaxHighlighting.scala | 13 ++++- .../tools/dotc/reporting/ConsoleReporter.scala | 2 +- .../dotc/reporting/FancyConsoleReporter.scala | 55 +++++++++++----------- src/dotty/tools/dotc/reporting/Reporter.scala | 2 +- 6 files changed, 64 insertions(+), 47 deletions(-) diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala index b2dc18d2a..f54a23ad2 100644 --- a/src/dotty/tools/dotc/Driver.scala +++ b/src/dotty/tools/dotc/Driver.scala @@ -22,11 +22,7 @@ abstract class Driver extends DotClass { protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = if (fileNames.nonEmpty) try { - val fresh = ctx.fresh.setReporter { - if (ctx.settings.color.value == "never") new ConsoleReporter() - else new FancyConsoleReporter() - } - val run = compiler.newRun(fresh) + val run = compiler.newRun run.compile(fileNames) run.printSummary() } diff --git a/src/dotty/tools/dotc/printing/Highlighting.scala b/src/dotty/tools/dotc/printing/Highlighting.scala index 75f83bf29..13e55722f 100644 --- a/src/dotty/tools/dotc/printing/Highlighting.scala +++ b/src/dotty/tools/dotc/printing/Highlighting.scala @@ -3,36 +3,47 @@ package dotc package printing import scala.collection.mutable +import core.Contexts.Context object Highlighting { - implicit def highlightToString(h: Highlight): String = h.toString - implicit def hbufToString(hb: HighlightBuffer): String = hb.toString + implicit def highlightShow(h: Highlight)(implicit ctx: Context): String = + h.show + implicit def highlightToString(h: Highlight): String = + h.toString + implicit def hbufToString(hb: HighlightBuffer): String = + hb.toString abstract class Highlight(private val highlight: String) { def text: String - override def toString = highlight + text + Console.RESET + def show(implicit ctx: Context) = + if (ctx.settings.color.value == "never") text + else highlight + text + Console.RESET - def +(other: Highlight): HighlightBuffer = + override def toString = + highlight + text + Console.RESET + + def +(other: Highlight)(implicit ctx: Context): HighlightBuffer = new HighlightBuffer(this) + other - def +(other: String): HighlightBuffer = + def +(other: String)(implicit ctx: Context): HighlightBuffer = new HighlightBuffer(this) + other } abstract class Modifier(private val mod: String, text: String) extends Highlight(Console.RESET) { - override def toString = - mod + super.toString + override def show(implicit ctx: Context) = + if (ctx.settings.color.value == "never") "" + else mod + super.show } - case class HighlightBuffer(hl: Highlight) { + case class HighlightBuffer(hl: Highlight)(implicit ctx: Context) { val buffer = new mutable.ListBuffer[String] - buffer += hl.toString + buffer += hl.show def +(other: Highlight): HighlightBuffer = { - buffer += other.toString + buffer += other.show this } @@ -45,6 +56,8 @@ object Highlighting { buffer.mkString } + case class NoColor(text: String) extends Highlight(Console.RESET) + case class Red(text: String) extends Highlight(Console.RED) case class Blue(text: String) extends Highlight(Console.BLUE) case class Cyan(text: String) extends Highlight(Console.CYAN) diff --git a/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index d94f6796d..95e59ccf3 100644 --- a/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -6,14 +6,23 @@ import parsing.Tokens._ import scala.annotation.switch import scala.collection.mutable.StringBuilder import core.Contexts.Context +import Highlighting.{Highlight, HighlightBuffer} /** This object provides functions for syntax highlighting in the REPL */ object SyntaxHighlighting { implicit class SyntaxFormatting(val sc: StringContext) extends AnyVal { def hl(args: Any*)(implicit ctx: Context): String = - if (ctx.settings.color.value == "never") sc.s(args: _*) - else sc.s(args.map(x => new String(apply(x.toString).toArray)): _*) + sc.s(args.map ({ + case hl: Highlight => + hl.show + case hb: HighlightBuffer => + hb.toString + case x if ctx.settings.color.value != "never" => + new String(apply(x.toString).toArray) + case x => + x.toString + }): _*) } val NoColor = Console.RESET diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index b532d05c2..1ae6a1135 100644 --- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -44,7 +44,7 @@ class ConsoleReporter( } } - def printExplanation(m: Message): Unit = + def printExplanation(m: Message)(implicit ctx: Context): Unit = printMessage( s"""| |Explanation diff --git a/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala b/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala index 3fc9ab473..d69396f5f 100644 --- a/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/FancyConsoleReporter.scala @@ -21,29 +21,29 @@ class FancyConsoleReporter( writer: PrintWriter = new PrintWriter(Console.err, true) ) extends ConsoleReporter(reader, writer) { + def stripColor(str: String): String = + str.replaceAll("\u001B\\[[;\\d]*m", "") + def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = { val lineNum = s"${pos.line}:" (lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length) } - def columnMarker(pos: SourcePosition, offset: Int) = + def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context) = if (pos.startLine == pos.endLine) { val whitespace = " " * (pos.column + offset) val carets = - AnnotationColor + - ("^" * math.max(1, pos.endColumn - pos.startColumn)) + - NoColor + Red("^" * math.max(1, pos.endColumn - pos.startColumn)) - whitespace + carets + whitespace + carets.show } else { - " " * (pos.column + offset) + AnnotationColor + "^" + NoColor + Red(" " * (pos.column + offset) + "^").show } 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 lineLength = stripColor(line).length val padding = math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn) @@ -68,9 +68,9 @@ class FancyConsoleReporter( val prefix = s"-- $kind: $file " prefix + - ("-" * math.max(ctx.settings.pageWidth.value - prefix.replaceAll("\u001B\\[[;\\d]*m", "").length, 0)) + + ("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0)) + "\n" + outer - }).toString else "" + }).show else "" /** Prints the message with the given position indication. */ override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = { @@ -84,23 +84,22 @@ class FancyConsoleReporter( } else printMessage(msg) } - override def printExplanation(m: Message): Unit = - printMessage( - s"""| - |${Blue("Explanation")} - |${Blue("===========")} - |${m.explanation}""".stripMargin - ) - - - override def summary: String = { - val b = new mutable.ListBuffer[String] - if (warningCount > 0) - b += countString(warningCount, Yellow("warning")) + " found" - if (errorCount > 0) - b += countString(errorCount, Red("error")) + " found" - for ((settingName, count) <- unreportedWarnings) - b += s"there were $count ${settingName.tail} ${Yellow("warning(s)")}; re-run with $settingName for details" - b.mkString("\n") + override def printExplanation(m: Message)(implicit ctx: Context): Unit = { + printMessage(hl"""| + |${Blue("Explanation")} + |${Blue("===========")}""".stripMargin) + printMessage(m.explanation) } + + + //override def summary(implicit ctx: Context): String = { + // val b = new mutable.ListBuffer[String] + // if (warningCount > 0) + // b += countString(warningCount, Yellow("warning").show) + " found" + // if (errorCount > 0) + // b += countString(errorCount, Red("error").show) + " found" + // for ((settingName, count) <- unreportedWarnings) + // b += s"there were $count ${settingName.tail} ${Yellow("warning(s)").show}; re-run with $settingName for details" + // b.mkString("\n") + //} } diff --git a/src/dotty/tools/dotc/reporting/Reporter.scala b/src/dotty/tools/dotc/reporting/Reporter.scala index fe226b284..538464daa 100644 --- a/src/dotty/tools/dotc/reporting/Reporter.scala +++ b/src/dotty/tools/dotc/reporting/Reporter.scala @@ -247,7 +247,7 @@ abstract class Reporter extends interfaces.ReporterResult { /** Summary of warnings and errors */ - def summary: String = { + def summary/*(implicit ctx: Context)*/: String = { val b = new mutable.ListBuffer[String] if (warningCount > 0) b += countString(warningCount, "warning") + " found" -- cgit v1.2.3