From 787a2ceec02fe07fbb9efee673d3abb7cac2969e Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Fri, 16 Sep 2016 22:28:27 +0200 Subject: Add modifiers to highlighting --- src/dotty/tools/dotc/Compiler.scala | 4 -- src/dotty/tools/dotc/Driver.scala | 6 +- src/dotty/tools/dotc/printing/Highlighting.scala | 70 ++++++++++++---------- .../tools/dotc/reporting/ConsoleReporter.scala | 2 +- .../dotc/reporting/diagnostic/MessageCreator.scala | 2 +- src/dotty/tools/dotc/typer/ErrorReporting.scala | 6 +- 6 files changed, 49 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index d6e3cc89b..ea6254f5b 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -140,10 +140,6 @@ class Compiler { .setTyper(new Typer) .setMode(Mode.ImplicitsEnabled) .setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true)) - .setReporter( - if (ctx.settings.color.value == "never") new ConsoleReporter() - else new FancyConsoleReporter() - ) ctx.initialize()(start) // re-initialize the base context with start def addImport(ctx: Context, refFn: () => TermRef) = ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx)) diff --git a/src/dotty/tools/dotc/Driver.scala b/src/dotty/tools/dotc/Driver.scala index f54a23ad2..b2dc18d2a 100644 --- a/src/dotty/tools/dotc/Driver.scala +++ b/src/dotty/tools/dotc/Driver.scala @@ -22,7 +22,11 @@ abstract class Driver extends DotClass { protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter = if (fileNames.nonEmpty) try { - val run = compiler.newRun + val fresh = ctx.fresh.setReporter { + if (ctx.settings.color.value == "never") new ConsoleReporter() + else new FancyConsoleReporter() + } + val run = compiler.newRun(fresh) run.compile(fileNames) run.printSummary() } diff --git a/src/dotty/tools/dotc/printing/Highlighting.scala b/src/dotty/tools/dotc/printing/Highlighting.scala index a496976a1..75f83bf29 100644 --- a/src/dotty/tools/dotc/printing/Highlighting.scala +++ b/src/dotty/tools/dotc/printing/Highlighting.scala @@ -6,33 +6,38 @@ import scala.collection.mutable object Highlighting { - implicit def colorToString(c: Color): String = c.toString - implicit def cbufToString(cb: ColorBuffer): String = cb.toString + implicit def highlightToString(h: Highlight): String = h.toString + implicit def hbufToString(hb: HighlightBuffer): String = hb.toString - abstract class Color(private val color: String) { + abstract class Highlight(private val highlight: String) { def text: String - override def toString = color + text + Console.RESET + override def toString = highlight + text + Console.RESET - def +(other: Color): ColorBuffer = - new ColorBuffer(this) + other + def +(other: Highlight): HighlightBuffer = + new HighlightBuffer(this) + other - def +(other: String): ColorBuffer = - new ColorBuffer(this) + other + def +(other: String): HighlightBuffer = + new HighlightBuffer(this) + other } - case class ColorBuffer(color: Color) { + abstract class Modifier(private val mod: String, text: String) extends Highlight(Console.RESET) { + override def toString = + mod + super.toString + } + + case class HighlightBuffer(hl: Highlight) { val buffer = new mutable.ListBuffer[String] - buffer += color.toString + buffer += hl.toString - def +(color: Color): ColorBuffer = { - buffer += color.toString + def +(other: Highlight): HighlightBuffer = { + buffer += other.toString this } - def +(str: String): ColorBuffer = { - buffer += str + def +(other: String): HighlightBuffer = { + buffer += other this } @@ -40,21 +45,24 @@ object Highlighting { buffer.mkString } - case class Red(text: String) extends Color(Console.RED) - case class Blue(text: String) extends Color(Console.BLUE) - case class Cyan(text: String) extends Color(Console.CYAN) - case class Black(text: String) extends Color(Console.BLACK) - case class Green(text: String) extends Color(Console.GREEN) - case class White(text: String) extends Color(Console.WHITE) - case class Yellow(text: String) extends Color(Console.YELLOW) - case class Magenta(text: String) extends Color(Console.MAGENTA) - - case class RedB(text: String) extends Color(Console.RED_B) - case class BlueB(text: String) extends Color(Console.BLUE_B) - case class CyanB(text: String) extends Color(Console.CYAN_B) - case class BlackB(text: String) extends Color(Console.BLACK_B) - case class GreenB(text: String) extends Color(Console.GREEN_B) - case class WhiteB(text: String) extends Color(Console.WHITE_B) - case class YellowB(text: String) extends Color(Console.YELLOW_B) - case class MagentaB(text: String) extends Color(Console.MAGENTA_B) + 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) + case class Black(text: String) extends Highlight(Console.BLACK) + case class Green(text: String) extends Highlight(Console.GREEN) + case class White(text: String) extends Highlight(Console.WHITE) + case class Yellow(text: String) extends Highlight(Console.YELLOW) + case class Magenta(text: String) extends Highlight(Console.MAGENTA) + + case class RedB(text: String) extends Highlight(Console.RED_B) + case class BlueB(text: String) extends Highlight(Console.BLUE_B) + case class CyanB(text: String) extends Highlight(Console.CYAN_B) + case class BlackB(text: String) extends Highlight(Console.BLACK_B) + case class GreenB(text: String) extends Highlight(Console.GREEN_B) + case class WhiteB(text: String) extends Highlight(Console.WHITE_B) + case class YellowB(text: String) extends Highlight(Console.YELLOW_B) + case class MagentaB(text: String) extends Highlight(Console.MAGENTA_B) + + case class Bold(text: String) extends Modifier(Console.BOLD, text) + case class Underlined(text: String) extends Modifier(Console.UNDERLINED, text) } diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index a00813328..b532d05c2 100644 --- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -37,7 +37,7 @@ class ConsoleReporter( /** Prints the message with the given position indication. */ def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = { val posStr = if (pos.exists) s"$pos: " else "" - printMessage(posStr + kind + msg) + printMessage(s"${posStr}$kind: $msg") if (pos.exists) { printSourceLine(pos) printColumnMarker(pos) diff --git a/src/dotty/tools/dotc/reporting/diagnostic/MessageCreator.scala b/src/dotty/tools/dotc/reporting/diagnostic/MessageCreator.scala index 4be325b63..99ccca4cc 100644 --- a/src/dotty/tools/dotc/reporting/diagnostic/MessageCreator.scala +++ b/src/dotty/tools/dotc/reporting/diagnostic/MessageCreator.scala @@ -7,7 +7,7 @@ import util.{SourcePosition, NoSourcePosition} import core.Contexts.Context object MessageCreator { - implicit def toNoExplanation(str: String) = + implicit def toNoExplanation(str: String): MessageCreator = new NoExplanation(str) } diff --git a/src/dotty/tools/dotc/typer/ErrorReporting.scala b/src/dotty/tools/dotc/typer/ErrorReporting.scala index c8e76aa97..43c093510 100644 --- a/src/dotty/tools/dotc/typer/ErrorReporting.scala +++ b/src/dotty/tools/dotc/typer/ErrorReporting.scala @@ -131,9 +131,9 @@ object ErrorReporting { val found1 = reported(found) reported.setVariance(-1) val expected1 = reported(expected) - ex"""type mismatch: - | found : $found1 - | required: $expected1""" + whyNoMatchStr(found, expected) + ex"""|type mismatch: + |found: $found1 + |required: $expected1""".stripMargin + whyNoMatchStr(found, expected) } /** Format `raw` implicitNotFound argument, replacing all -- cgit v1.2.3