Skip to content

Commit 1506543

Browse files
committed
Factor out explanation header to Reporter
1 parent bf76d2f commit 1506543

File tree

7 files changed

+77
-42
lines changed

7 files changed

+77
-42
lines changed

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

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ConsoleReporter(
2020
writer: PrintWriter = new PrintWriter(Console.err, true))
2121
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
2222

23+
import Message._
24+
2325
/** maximal number of error messages to be printed */
2426
protected def ErrorLimit = 100
2527

@@ -42,17 +44,29 @@ class ConsoleReporter(
4244
}
4345
}
4446

45-
override def doReport(m: Message)(implicit ctx: Context): Unit = m match {
46-
case m: Error =>
47-
printMessageAndPos(m.message, m.pos, m.kind)
48-
if (ctx.settings.prompt.value) displayPrompt()
49-
case m: ConditionalWarning if !m.enablingOption.value =>
50-
case m: MigrationWarning =>
51-
printMessageAndPos(m.message, m.pos, m.kind)
52-
case m: Warning =>
53-
printMessageAndPos(m.message, m.pos, m.kind)
54-
case _ =>
55-
printMessageAndPos(m.message, m.pos, m.kind)
47+
def printExplanation(m: Message): Unit =
48+
printMessage(
49+
s"""|
50+
|Explanation
51+
|===========
52+
|${m.explanation}""".stripMargin
53+
)
54+
55+
override def doReport(m: Message)(implicit ctx: Context): Unit = {
56+
m match {
57+
case m: Error =>
58+
printMessageAndPos(m.message, m.pos, m.kind)
59+
if (ctx.settings.prompt.value) displayPrompt()
60+
case m: ConditionalWarning if !m.enablingOption.value =>
61+
case m: MigrationWarning =>
62+
printMessageAndPos(m.message, m.pos, m.kind)
63+
case m: Warning =>
64+
printMessageAndPos(m.message, m.pos, m.kind)
65+
case _ =>
66+
printMessageAndPos(m.message, m.pos, m.kind)
67+
}
68+
69+
if (ctx.shouldExplain(m)) printExplanation(m)
5670
}
5771

5872
def displayPrompt(): Unit = {

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import Reporter._
99
import java.io.{ BufferedReader, IOException, PrintWriter }
1010
import scala.reflect.internal.util._
1111
import printing.SyntaxHighlighting._
12+
import printing.Highlighting._
13+
import diagnostic.Message
1214

1315
/**
1416
* This class implements a more Fancy version (with colors!) of the regular
@@ -56,14 +58,13 @@ class FancyConsoleReporter(
5658
}
5759

5860
def posStr(pos: SourcePosition, kind: String)(implicit ctx: Context) =
59-
if (pos.exists) {
61+
if (pos.exists) Blue({
6062
val file = pos.source.file.toString
6163

62-
val prefix = s"${Console.CYAN}-- $kind: $file "
64+
val prefix = s"-- $kind: $file "
6365
prefix +
64-
("-" * math.max(ctx.settings.pageWidth.value - prefix.replaceAll("\u001B\\[[;\\d]*m", "").length, 0)) +
65-
NoColor
66-
} else ""
66+
("-" * math.max(ctx.settings.pageWidth.value - prefix.replaceAll("\u001B\\[[;\\d]*m", "").length, 0))
67+
}).toString else ""
6768

6869
/** Prints the message with the given position indication. */
6970
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
@@ -76,4 +77,24 @@ class FancyConsoleReporter(
7677
printMessage(List(src, marker, err).mkString("\n"))
7778
} else printMessage(msg)
7879
}
80+
81+
override def printExplanation(m: Message): Unit =
82+
printMessage(
83+
s"""|
84+
|${Blue("Explanation")}
85+
|${Blue("===========")}
86+
|${m.explanation}""".stripMargin
87+
)
88+
89+
90+
override def summary: String = {
91+
val b = new mutable.ListBuffer[String]
92+
if (warningCount > 0)
93+
b += countString(warningCount, Yellow("warning")) + " found"
94+
if (errorCount > 0)
95+
b += countString(errorCount, Red("error")) + " found"
96+
for ((settingName, count) <- unreportedWarnings)
97+
b += s"there were $count ${settingName.tail} ${Yellow("warning(s)")}; re-run with $settingName for details"
98+
b.mkString("\n")
99+
}
79100
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,8 @@ trait Reporting { this: Context =>
7575
def warning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
7676
reporter.report(new Warning(msg, pos))
7777

78-
def explainWarning(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit = {
78+
def explainWarning(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit =
7979
reporter.report(msg.warning(pos))
80-
if (this.shouldExplain(msg))
81-
reporter.report(new Info(msg.explanation, NoSourcePosition))
82-
}
8380

8481
def strictWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
8582
if (this.settings.strict.value) error(msg, pos)
@@ -90,11 +87,8 @@ trait Reporting { this: Context =>
9087
reporter.report(new Error(msg, pos))
9188
}
9289

93-
def explainError(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit = {
90+
def explainError(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit =
9491
reporter.report(msg.error(pos))
95-
if (this.shouldExplain(msg))
96-
reporter.report(new Info(msg.explanation, NoSourcePosition))
97-
}
9892

9993
def errorOrMigrationWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
10094
if (ctx.scala2Mode) migrationWarning(msg, pos) else error(msg, pos)
@@ -267,7 +261,7 @@ abstract class Reporter extends interfaces.ReporterResult {
267261
}
268262

269263
/** Returns a string meaning "n elements". */
270-
private def countString(n: Int, elements: String): String = n match {
264+
protected def countString(n: Int, elements: String): String = n match {
271265
case 0 => "no " + elements + "s"
272266
case 1 => "one " + elements
273267
case 2 => "two " + elements + "s"

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import java.util.Optional
1111
object Message {
1212
val nonSensicalStartTag = "<nonsensical>"
1313
val nonSensicalEndTag = "</nonsensical>"
14+
15+
implicit class MessageContext(val c: Context) extends AnyVal {
16+
def shouldExplain(msg: Message): Boolean = {
17+
implicit val ctx: Context = c
18+
msg.explanation match {
19+
case "" => false
20+
case _ => ctx.settings.explain.value
21+
}
22+
}
23+
}
1424
}
1525

1626
class Message(

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ import util.{SourcePosition, NoSourcePosition}
77
import core.Contexts.Context
88

99
object MessageCreator {
10-
implicit class DiagnosticContext(val c: Context) extends AnyVal {
11-
def shouldExplain(msg: MessageCreator): Boolean = {
12-
implicit val ctx: Context = c
13-
msg match {
14-
case NoExplanation(_) => false
15-
case _ => ctx.settings.explain.value
16-
}
17-
}
18-
}
19-
2010
implicit def toNoExplanation(str: String) =
2111
new NoExplanation(str)
2212
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ object syntax {
2929
| // perform your cleanup here!
3030
|}""".stripMargin
3131

32-
hl"""|Explanation:
33-
|============
34-
|A ${"try"} expression should be followed by some mechanism to handle any exceptions
32+
hl"""|A ${"try"} expression should be followed by some mechanism to handle any exceptions
3533
|thrown. Typically a ${"catch"} expression follows the ${"try"} and pattern matches
3634
|on any expected exceptions. For example:
3735
|

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package reporting
44
package diagnostic
55

66
import dotc.core._
7-
import Contexts.Context, Decorators._, Symbols._
7+
import Contexts.Context, Decorators._, Symbols._, Names._
88
import dotc.printing.SyntaxHighlighting._
99
import util.{SourcePosition, NoSourcePosition}
1010

@@ -35,13 +35,21 @@ object tpe {
3535

3636
val caseDef = s"case $pat$guard => $body"
3737

38-
hl"""|Explanation
39-
|===========
40-
|For each ${"case"} bound variable names have to be unique. In:
38+
hl"""|For each ${"case"} bound variable names have to be unique. In:
4139
|
4240
|$caseDef
4341
|
4442
|`${bind.name}` is not unique. Rename one of the bound variables!""".stripMargin
4543
}
4644
}
45+
46+
class MissingIdent(tree: untpd.Ident, treeKind: String, name: Name)(implicit ctx: Context) extends MessageCreator {
47+
val kind = "Missing identifier"
48+
val msg = em"not found: $treeKind$name"
49+
50+
val explanation = {
51+
hl"""|An identifier for `${name.show}` is missing. This means that something
52+
|has either been misspelt or you're forgetting an import""".stripMargin
53+
}
54+
}
4755
}

0 commit comments

Comments
 (0)