Skip to content

Commit f3db36d

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 4c681d9 commit f3db36d

File tree

5 files changed

+53
-36
lines changed

5 files changed

+53
-36
lines changed

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: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@ 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 {
14-
def hl(args: Any*)(implicit ctx: Context): String =
15+
def hl(args: Any*)(implicit ctx: Context): String = {
1516
if (ctx.settings.color.value == "never") sc.s(args: _*)
16-
else sc.s(args.map(x => new String(apply(x.toString).toArray)): _*)
17+
else sc.s(args.map ({
18+
case hl: Highlight => hl.show
19+
case hb: HighlightBuffer => hb.toString
20+
case x => new String(apply(x.toString).toArray)
21+
}): _*)
22+
}
1723
}
1824

1925
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: 20 additions & 22 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

@@ -60,11 +60,10 @@ class FancyConsoleReporter(
6060
def posStr(pos: SourcePosition, kind: String)(implicit ctx: Context) =
6161
if (pos.exists) Blue({
6262
val file = pos.source.file.toString
63-
6463
val prefix = s"-- $kind: $file "
6564
prefix +
66-
("-" * math.max(ctx.settings.pageWidth.value - prefix.replaceAll("\u001B\\[[;\\d]*m", "").length, 0))
67-
}).toString else ""
65+
("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0))
66+
}).show else ""
6867

6968
/** Prints the message with the given position indication. */
7069
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
@@ -78,23 +77,22 @@ class FancyConsoleReporter(
7877
} else printMessage(msg)
7978
}
8079

81-
override def printExplanation(m: Message): Unit =
82-
printMessage(
83-
s"""|
84-
|${Blue("Explanation")}
85-
|${Blue("===========")}
86-
|${m.explanation}""".stripMargin
87-
)
80+
override def printExplanation(m: Message)(implicit ctx: Context): Unit = {
81+
printMessage(hl"""|
82+
|${Blue("Explanation")}
83+
|${Blue("===========")}""".stripMargin)
84+
printMessage(m.explanation)
85+
}
8886

8987

90-
override def summary: String = {
88+
override def summary(implicit ctx: Context): String = {
9189
val b = new mutable.ListBuffer[String]
9290
if (warningCount > 0)
93-
b += countString(warningCount, Yellow("warning")) + " found"
91+
b += countString(warningCount, Yellow("warning").show) + " found"
9492
if (errorCount > 0)
95-
b += countString(errorCount, Red("error")) + " found"
93+
b += countString(errorCount, Red("error").show) + " found"
9694
for ((settingName, count) <- unreportedWarnings)
97-
b += s"there were $count ${settingName.tail} ${Yellow("warning(s)")}; re-run with $settingName for details"
95+
b += s"there were $count ${settingName.tail} ${Yellow("warning(s)").show}; re-run with $settingName for details"
9896
b.mkString("\n")
9997
}
10098
}

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

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

244244

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

0 commit comments

Comments
 (0)