Skip to content

Commit 628b7f3

Browse files
committed
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.
1 parent 787a2ce commit 628b7f3

File tree

6 files changed

+64
-47
lines changed

6 files changed

+64
-47
lines changed

src/dotty/tools/dotc/Driver.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ abstract class Driver extends DotClass {
2222
protected def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter =
2323
if (fileNames.nonEmpty)
2424
try {
25-
val fresh = ctx.fresh.setReporter {
26-
if (ctx.settings.color.value == "never") new ConsoleReporter()
27-
else new FancyConsoleReporter()
28-
}
29-
val run = compiler.newRun(fresh)
25+
val run = compiler.newRun
3026
run.compile(fileNames)
3127
run.printSummary()
3228
}

src/dotty/tools/dotc/printing/Highlighting.scala

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,47 @@ package dotc
33
package printing
44

55
import scala.collection.mutable
6+
import core.Contexts.Context
67

78
object Highlighting {
89

9-
implicit def highlightToString(h: Highlight): String = h.toString
10-
implicit def hbufToString(hb: HighlightBuffer): String = hb.toString
10+
implicit def highlightShow(h: Highlight)(implicit ctx: Context): String =
11+
h.show
12+
implicit def highlightToString(h: Highlight): String =
13+
h.toString
14+
implicit def hbufToString(hb: HighlightBuffer): String =
15+
hb.toString
1116

1217
abstract class Highlight(private val highlight: String) {
1318
def text: String
1419

15-
override def toString = highlight + text + Console.RESET
20+
def show(implicit ctx: Context) =
21+
if (ctx.settings.color.value == "never") text
22+
else highlight + text + Console.RESET
1623

17-
def +(other: Highlight): HighlightBuffer =
24+
override def toString =
25+
highlight + text + Console.RESET
26+
27+
def +(other: Highlight)(implicit ctx: Context): HighlightBuffer =
1828
new HighlightBuffer(this) + other
1929

20-
def +(other: String): HighlightBuffer =
30+
def +(other: String)(implicit ctx: Context): HighlightBuffer =
2131
new HighlightBuffer(this) + other
2232
}
2333

2434
abstract class Modifier(private val mod: String, text: String) extends Highlight(Console.RESET) {
25-
override def toString =
26-
mod + super.toString
35+
override def show(implicit ctx: Context) =
36+
if (ctx.settings.color.value == "never") ""
37+
else mod + super.show
2738
}
2839

29-
case class HighlightBuffer(hl: Highlight) {
40+
case class HighlightBuffer(hl: Highlight)(implicit ctx: Context) {
3041
val buffer = new mutable.ListBuffer[String]
3142

32-
buffer += hl.toString
43+
buffer += hl.show
3344

3445
def +(other: Highlight): HighlightBuffer = {
35-
buffer += other.toString
46+
buffer += other.show
3647
this
3748
}
3849

@@ -45,6 +56,8 @@ object Highlighting {
4556
buffer.mkString
4657
}
4758

59+
case class NoColor(text: String) extends Highlight(Console.RESET)
60+
4861
case class Red(text: String) extends Highlight(Console.RED)
4962
case class Blue(text: String) extends Highlight(Console.BLUE)
5063
case class Cyan(text: String) extends Highlight(Console.CYAN)

src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@ import parsing.Tokens._
66
import scala.annotation.switch
77
import scala.collection.mutable.StringBuilder
88
import core.Contexts.Context
9+
import Highlighting.{Highlight, HighlightBuffer}
910

1011
/** This object provides functions for syntax highlighting in the REPL */
1112
object SyntaxHighlighting {
1213

1314
implicit class SyntaxFormatting(val sc: StringContext) extends AnyVal {
1415
def hl(args: Any*)(implicit ctx: Context): String =
15-
if (ctx.settings.color.value == "never") sc.s(args: _*)
16-
else sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
16+
sc.s(args.map ({
17+
case hl: Highlight =>
18+
hl.show
19+
case hb: HighlightBuffer =>
20+
hb.toString
21+
case x if ctx.settings.color.value != "never" =>
22+
new String(apply(x.toString).toArray)
23+
case x =>
24+
x.toString
25+
}): _*)
1726
}
1827

1928
val NoColor = Console.RESET

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ConsoleReporter(
4444
}
4545
}
4646

47-
def printExplanation(m: Message): Unit =
47+
def printExplanation(m: Message)(implicit ctx: Context): Unit =
4848
printMessage(
4949
s"""|
5050
|Explanation

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

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,29 @@ class FancyConsoleReporter(
2121
writer: PrintWriter = new PrintWriter(Console.err, true)
2222
) extends ConsoleReporter(reader, writer) {
2323

24+
def stripColor(str: String): String =
25+
str.replaceAll("\u001B\\[[;\\d]*m", "")
26+
2427
def sourceLine(pos: SourcePosition)(implicit ctx: Context): (String, Int) = {
2528
val lineNum = s"${pos.line}:"
2629
(lineNum + hl"${pos.lineContent.stripLineEnd}", lineNum.length)
2730
}
2831

29-
def columnMarker(pos: SourcePosition, offset: Int) =
32+
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context) =
3033
if (pos.startLine == pos.endLine) {
3134
val whitespace = " " * (pos.column + offset)
3235
val carets =
33-
AnnotationColor +
34-
("^" * math.max(1, pos.endColumn - pos.startColumn)) +
35-
NoColor
36+
Red("^" * math.max(1, pos.endColumn - pos.startColumn))
3637

37-
whitespace + carets
38+
whitespace + carets.show
3839
} else {
39-
" " * (pos.column + offset) + AnnotationColor + "^" + NoColor
40+
Red(" " * (pos.column + offset) + "^").show
4041
}
4142

4243
def errorMsg(pos: SourcePosition, msg: String, offset: Int)(implicit ctx: Context) = {
4344
var hasLongLines = false
4445
val leastWhitespace = msg.lines.foldLeft(Int.MaxValue) { (minPad, line) =>
45-
val lineLength =
46-
line.replaceAll("\u001B\\[[;\\d]*m", "").length
46+
val lineLength = stripColor(line).length
4747
val padding =
4848
math.min(math.max(0, ctx.settings.pageWidth.value - offset - lineLength), offset + pos.startColumn)
4949

@@ -68,9 +68,9 @@ class FancyConsoleReporter(
6868

6969
val prefix = s"-- $kind: $file "
7070
prefix +
71-
("-" * math.max(ctx.settings.pageWidth.value - prefix.replaceAll("\u001B\\[[;\\d]*m", "").length, 0)) +
71+
("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0)) +
7272
"\n" + outer
73-
}).toString else ""
73+
}).show else ""
7474

7575
/** Prints the message with the given position indication. */
7676
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
@@ -84,23 +84,22 @@ class FancyConsoleReporter(
8484
} else printMessage(msg)
8585
}
8686

87-
override def printExplanation(m: Message): Unit =
88-
printMessage(
89-
s"""|
90-
|${Blue("Explanation")}
91-
|${Blue("===========")}
92-
|${m.explanation}""".stripMargin
93-
)
94-
95-
96-
override def summary: String = {
97-
val b = new mutable.ListBuffer[String]
98-
if (warningCount > 0)
99-
b += countString(warningCount, Yellow("warning")) + " found"
100-
if (errorCount > 0)
101-
b += countString(errorCount, Red("error")) + " found"
102-
for ((settingName, count) <- unreportedWarnings)
103-
b += s"there were $count ${settingName.tail} ${Yellow("warning(s)")}; re-run with $settingName for details"
104-
b.mkString("\n")
87+
override def printExplanation(m: Message)(implicit ctx: Context): Unit = {
88+
printMessage(hl"""|
89+
|${Blue("Explanation")}
90+
|${Blue("===========")}""".stripMargin)
91+
printMessage(m.explanation)
10592
}
93+
94+
95+
//override def summary(implicit ctx: Context): String = {
96+
// val b = new mutable.ListBuffer[String]
97+
// if (warningCount > 0)
98+
// b += countString(warningCount, Yellow("warning").show) + " found"
99+
// if (errorCount > 0)
100+
// b += countString(errorCount, Red("error").show) + " found"
101+
// for ((settingName, count) <- unreportedWarnings)
102+
// b += s"there were $count ${settingName.tail} ${Yellow("warning(s)").show}; re-run with $settingName for details"
103+
// b.mkString("\n")
104+
//}
106105
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ abstract class Reporter extends interfaces.ReporterResult {
247247

248248

249249
/** Summary of warnings and errors */
250-
def summary: String = {
250+
def summary/*(implicit ctx: Context)*/: String = {
251251
val b = new mutable.ListBuffer[String]
252252
if (warningCount > 0)
253253
b += countString(warningCount, "warning") + " found"

0 commit comments

Comments
 (0)