Skip to content

Commit 787a2ce

Browse files
committed
Add modifiers to highlighting
1 parent 5c24377 commit 787a2ce

File tree

11 files changed

+89
-78
lines changed

11 files changed

+89
-78
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,6 @@ class Compiler {
140140
.setTyper(new Typer)
141141
.setMode(Mode.ImplicitsEnabled)
142142
.setTyperState(new MutableTyperState(ctx.typerState, ctx.typerState.reporter, isCommittable = true))
143-
.setReporter(
144-
if (ctx.settings.color.value == "never") new ConsoleReporter()
145-
else new FancyConsoleReporter()
146-
)
147143
ctx.initialize()(start) // re-initialize the base context with start
148144
def addImport(ctx: Context, refFn: () => TermRef) =
149145
ctx.fresh.setImportInfo(ImportInfo.rootImport(refFn)(ctx))

src/dotty/tools/dotc/Driver.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ 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 run = compiler.newRun
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)
2630
run.compile(fileNames)
2731
run.printSummary()
2832
}

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,63 @@ import scala.collection.mutable
66

77
object Highlighting {
88

9-
implicit def colorToString(c: Color): String = c.toString
10-
implicit def cbufToString(cb: ColorBuffer): String = cb.toString
9+
implicit def highlightToString(h: Highlight): String = h.toString
10+
implicit def hbufToString(hb: HighlightBuffer): String = hb.toString
1111

12-
abstract class Color(private val color: String) {
12+
abstract class Highlight(private val highlight: String) {
1313
def text: String
1414

15-
override def toString = color + text + Console.RESET
15+
override def toString = highlight + text + Console.RESET
1616

17-
def +(other: Color): ColorBuffer =
18-
new ColorBuffer(this) + other
17+
def +(other: Highlight): HighlightBuffer =
18+
new HighlightBuffer(this) + other
1919

20-
def +(other: String): ColorBuffer =
21-
new ColorBuffer(this) + other
20+
def +(other: String): HighlightBuffer =
21+
new HighlightBuffer(this) + other
2222
}
2323

24-
case class ColorBuffer(color: Color) {
24+
abstract class Modifier(private val mod: String, text: String) extends Highlight(Console.RESET) {
25+
override def toString =
26+
mod + super.toString
27+
}
28+
29+
case class HighlightBuffer(hl: Highlight) {
2530
val buffer = new mutable.ListBuffer[String]
2631

27-
buffer += color.toString
32+
buffer += hl.toString
2833

29-
def +(color: Color): ColorBuffer = {
30-
buffer += color.toString
34+
def +(other: Highlight): HighlightBuffer = {
35+
buffer += other.toString
3136
this
3237
}
3338

34-
def +(str: String): ColorBuffer = {
35-
buffer += str
39+
def +(other: String): HighlightBuffer = {
40+
buffer += other
3641
this
3742
}
3843

3944
override def toString =
4045
buffer.mkString
4146
}
4247

43-
case class Red(text: String) extends Color(Console.RED)
44-
case class Blue(text: String) extends Color(Console.BLUE)
45-
case class Cyan(text: String) extends Color(Console.CYAN)
46-
case class Black(text: String) extends Color(Console.BLACK)
47-
case class Green(text: String) extends Color(Console.GREEN)
48-
case class White(text: String) extends Color(Console.WHITE)
49-
case class Yellow(text: String) extends Color(Console.YELLOW)
50-
case class Magenta(text: String) extends Color(Console.MAGENTA)
51-
52-
case class RedB(text: String) extends Color(Console.RED_B)
53-
case class BlueB(text: String) extends Color(Console.BLUE_B)
54-
case class CyanB(text: String) extends Color(Console.CYAN_B)
55-
case class BlackB(text: String) extends Color(Console.BLACK_B)
56-
case class GreenB(text: String) extends Color(Console.GREEN_B)
57-
case class WhiteB(text: String) extends Color(Console.WHITE_B)
58-
case class YellowB(text: String) extends Color(Console.YELLOW_B)
59-
case class MagentaB(text: String) extends Color(Console.MAGENTA_B)
48+
case class Red(text: String) extends Highlight(Console.RED)
49+
case class Blue(text: String) extends Highlight(Console.BLUE)
50+
case class Cyan(text: String) extends Highlight(Console.CYAN)
51+
case class Black(text: String) extends Highlight(Console.BLACK)
52+
case class Green(text: String) extends Highlight(Console.GREEN)
53+
case class White(text: String) extends Highlight(Console.WHITE)
54+
case class Yellow(text: String) extends Highlight(Console.YELLOW)
55+
case class Magenta(text: String) extends Highlight(Console.MAGENTA)
56+
57+
case class RedB(text: String) extends Highlight(Console.RED_B)
58+
case class BlueB(text: String) extends Highlight(Console.BLUE_B)
59+
case class CyanB(text: String) extends Highlight(Console.CYAN_B)
60+
case class BlackB(text: String) extends Highlight(Console.BLACK_B)
61+
case class GreenB(text: String) extends Highlight(Console.GREEN_B)
62+
case class WhiteB(text: String) extends Highlight(Console.WHITE_B)
63+
case class YellowB(text: String) extends Highlight(Console.YELLOW_B)
64+
case class MagentaB(text: String) extends Highlight(Console.MAGENTA_B)
65+
66+
case class Bold(text: String) extends Modifier(Console.BOLD, text)
67+
case class Underlined(text: String) extends Modifier(Console.UNDERLINED, text)
6068
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ConsoleReporter(
3737
/** Prints the message with the given position indication. */
3838
def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
3939
val posStr = if (pos.exists) s"$pos: " else ""
40-
printMessage(posStr + kind + msg)
40+
printMessage(s"${posStr}$kind: $msg")
4141
if (pos.exists) {
4242
printSourceLine(pos)
4343
printColumnMarker(pos)

src/dotty/tools/dotc/reporting/diagnostic/MessageCreator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import util.{SourcePosition, NoSourcePosition}
77
import core.Contexts.Context
88

99
object MessageCreator {
10-
implicit def toNoExplanation(str: String) =
10+
implicit def toNoExplanation(str: String): MessageCreator =
1111
new NoExplanation(str)
1212
}
1313

src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ object ErrorReporting {
131131
val found1 = reported(found)
132132
reported.setVariance(-1)
133133
val expected1 = reported(expected)
134-
ex"""type mismatch:
135-
| found : $found1
136-
| required: $expected1""" + whyNoMatchStr(found, expected)
134+
ex"""|type mismatch:
135+
|found: $found1
136+
|required: $expected1""".stripMargin + whyNoMatchStr(found, expected)
137137
}
138138

139139
/** Format `raw` implicitNotFound argument, replacing all

test/dotc/tests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class tests extends CompilerTest {
2323
val defaultOutputDir = "./out/"
2424

2525
implicit val defaultOptions = noCheckOptions ++ List(
26-
"-Yno-deep-subtypes", "-Yno-double-bindings", "-Yforce-sbt-phases", "-d", defaultOutputDir) ++ {
26+
"-Yno-deep-subtypes", "-Yno-double-bindings", "-Yforce-sbt-phases", "-color:never",
27+
"-d", defaultOutputDir) ++ {
2728
if (isRunByJenkins) List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef") // should be Ycheck:all, but #725
2829
else List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef")
2930
}

test/test/CompilerTest.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import dotty.partest.DPConfig
55
import dotty.tools.dotc.{Main, Bench, Driver}
66
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
77
import dotty.tools.dotc.reporting._
8+
import diagnostic.Message
89
import dotty.tools.dotc.util.SourcePosition
910
import dotty.tools.dotc.config.CompilerCommand
1011
import dotty.tools.io.PlainFile
@@ -237,13 +238,13 @@ abstract class CompilerTest {
237238
val storeReporter = new Reporter with UniqueMessagePositions with HideNonSensicalMessages {
238239
private val consoleReporter = new ConsoleReporter()
239240
private val innerStoreReporter = new StoreReporter(consoleReporter)
240-
def doReport(d: Diagnostic)(implicit ctx: Context): Unit = {
241-
if (d.level == ERROR) {
241+
def doReport(m: Message)(implicit ctx: Context): Unit = {
242+
if (m.level == ERROR) {
242243
innerStoreReporter.flush()
243-
consoleReporter.doReport(d)
244+
consoleReporter.doReport(m)
244245
}
245-
else if (errorCount > 0) consoleReporter.doReport(d)
246-
else innerStoreReporter.doReport(d)
246+
else if (errorCount > 0) consoleReporter.doReport(m)
247+
else innerStoreReporter.doReport(m)
247248
}
248249
}
249250
val reporter = processor.process(allArgs, storeReporter)

test/test/OtherEntryPointsTest.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.junit.Assert._
55
import dotty.tools.dotc.Main
66
import dotty.tools.dotc.interfaces.{CompilerCallback, SourceFile}
77
import dotty.tools.dotc.reporting._
8+
import dotty.tools.dotc.reporting.diagnostic.Message
89
import dotty.tools.dotc.core.Contexts._
910
import java.io.File
1011
import scala.collection.mutable.ListBuffer
@@ -50,7 +51,7 @@ class OtherEntryPointsTest {
5051
private class CustomReporter extends Reporter
5152
with UniqueMessagePositions
5253
with HideNonSensicalMessages {
53-
def doReport(d: Diagnostic)(implicit ctx: Context): Unit = {
54+
def doReport(m: Message)(implicit ctx: Context): Unit = {
5455
}
5556
}
5657

tests/repl/errmsgs.check

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
scala> class Inv[T](x: T)
22
defined class Inv
33
scala> val x: List[String] = List(1)
4-
<console>:4: error: type mismatch:
5-
found : Int(1)
6-
required: String
4+
<console>:4: Error: type mismatch:
5+
found: Int(1)
6+
required: String
77
val x: List[String] = List(1)
88
^
99
scala> val y: List[List[String]] = List(List(1))
10-
<console>:4: error: type mismatch:
11-
found : Int(1)
12-
required: String
10+
<console>:4: Error: type mismatch:
11+
found: Int(1)
12+
required: String
1313
val y: List[List[String]] = List(List(1))
1414
^
1515
scala> val z: (List[String], List[Int]) = (List(1), List("a"))
16-
<console>:4: error: type mismatch:
17-
found : Int(1)
18-
required: String
16+
<console>:4: Error: type mismatch:
17+
found: Int(1)
18+
required: String
1919
val z: (List[String], List[Int]) = (List(1), List("a"))
2020
^
21-
<console>:4: error: type mismatch:
22-
found : String("a")
23-
required: Int
21+
<console>:4: Error: type mismatch:
22+
found: String("a")
23+
required: Int
2424
val z: (List[String], List[Int]) = (List(1), List("a"))
2525
^
2626
scala> val a: Inv[String] = new Inv(new Inv(1))
27-
<console>:5: error: type mismatch:
28-
found : Inv[T]
29-
required: String
27+
<console>:5: Error: type mismatch:
28+
found: Inv[T]
29+
required: String
3030

3131
where T is a type variable with constraint >: Int(1)
3232

3333
val a: Inv[String] = new Inv(new Inv(1))
3434
^
3535
scala> val b: Inv[String] = new Inv(1)
36-
<console>:5: error: type mismatch:
37-
found : Int(1)
38-
required: String
36+
<console>:5: Error: type mismatch:
37+
found: Int(1)
38+
required: String
3939
val b: Inv[String] = new Inv(1)
4040
^
4141
scala> abstract class C {
@@ -53,18 +53,18 @@ scala> abstract class C {
5353
}
5454
}
5555
}
56-
<console>:9: error: type mismatch:
57-
found : C.this.T(C.this.x)
58-
required: T'
56+
<console>:9: Error: type mismatch:
57+
found: C.this.T(C.this.x)
58+
required: T'
5959

6060
where T is a type in class C
6161
T' is a type in the initalizer of value s which is an alias of String
6262

6363
var y: T = x
6464
^
65-
<console>:13: error: type mismatch:
66-
found : T(y)
67-
required: T'
65+
<console>:13: Error: type mismatch:
66+
found: T(y)
67+
required: T'
6868

6969
where T is a type in the initalizer of value s which is an alias of String
7070
T' is a type in method f which is an alias of Int

tests/repl/imports.check

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ defined module o
77
scala> import o._
88
import o._
99
scala> buf += xs
10-
<console>:11: error: type mismatch:
11-
found : scala.collection.immutable.List[Int](o.xs)
12-
required: String
10+
<console>:11: Error: type mismatch:
11+
found: scala.collection.immutable.List[Int](o.xs)
12+
required: String
1313
buf += xs
1414
^
15-
<console>:11: error: type mismatch:
16-
found : String
17-
required: scala.collection.mutable.ListBuffer[Int]
15+
<console>:11: Error: type mismatch:
16+
found: String
17+
required: scala.collection.mutable.ListBuffer[Int]
1818
buf += xs
1919
^
2020
scala> buf ++= xs

0 commit comments

Comments
 (0)