Skip to content

Commit 18d63fe

Browse files
committed
Get rid of kind in MessageContainer
1 parent 7561db0 commit 18d63fe

File tree

6 files changed

+40
-44
lines changed

6 files changed

+40
-44
lines changed

interfaces/src/main/java/dotty/tools/dotc/interfaces/Diagnostic.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ public interface Diagnostic {
1414
public static final int WARNING = 1;
1515
public static final int INFO = 0;
1616

17-
/** @return The kind of message being reported */
18-
String kind();
19-
2017
/** @return The message to report */
2118
String message();
2219

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ object Formatting {
7575

7676
class SyntaxFormatter(sc: StringContext) extends StringFormatter(sc) {
7777
override protected def showArg(arg: Any)(implicit ctx: Context): String = {
78-
arg match {
78+
if (ctx.settings.color.value != "never") arg match {
7979
case arg: Showable =>
8080
val highlighted =
8181
SyntaxHighlighting(wrapNonSensical(arg, super.showArg(arg)))
@@ -84,8 +84,11 @@ object Formatting {
8484
hl.show
8585
case hb: HighlightBuffer =>
8686
hb.toString
87+
case str: String =>
88+
new String(SyntaxHighlighting(str).toArray)
8789
case _ => super.showArg(arg)
8890
}
91+
else super.showArg(arg)
8992
}
9093
}
9194

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ import diagnostic.{ Message, MessageContainer }
1414
import diagnostic.messages._
1515

1616
/**
17-
* This class implements a more Fancy version (with colors!) of the regular
18-
* `ConsoleReporter`
19-
*/
17+
* This class implements a Reporter that displays messages on a text console
18+
*/
2019
class ConsoleReporter(
2120
reader: BufferedReader = Console.in,
2221
writer: PrintWriter = new PrintWriter(Console.err, true)
@@ -66,18 +65,22 @@ class ConsoleReporter(
6665
.mkString(sys.props("line.separator"))
6766
}
6867

69-
def posStr(pos: SourcePosition, kind: String, errorId: String)(implicit ctx: Context) =
68+
def posStr(pos: SourcePosition, diagnosticLevel: String, message: Message)(implicit ctx: Context) =
7069
if (pos.exists) Blue({
7170
val file = pos.source.file.toString
72-
val errId = if (errorId != "") s"[$errorId] " else ""
71+
val errId = if (message.errorId != "") s"[${message.errorId}] " else ""
72+
val kind =
73+
if (message.kind == "") diagnosticLevel
74+
else s"${message.kind} $diagnosticLevel"
7375
val prefix = s"-- ${errId}${kind}: $file "
76+
7477
prefix +
7578
("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0))
7679
}).show else ""
7780

7881
/** Prints the message with the given position indication. */
79-
def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
80-
printMessage(posStr(pos, kind, msg.errorId))
82+
def printMessageAndPos(msg: Message, pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): Unit = {
83+
printMessage(posStr(pos, diagnosticLevel, msg))
8184
if (pos.exists) {
8285
val (src, offset) = sourceLine(pos)
8386
val marker = columnMarker(pos, offset)
@@ -97,15 +100,21 @@ class ConsoleReporter(
97100
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
98101
m match {
99102
case m: Error =>
100-
printMessageAndPos(m.contained, m.pos, m.kind)
103+
printMessageAndPos(m.contained, m.pos, "Error")
101104
if (ctx.settings.prompt.value) displayPrompt()
102105
case m: ConditionalWarning if !m.enablingOption.value =>
106+
case m: FeatureWarning =>
107+
printMessageAndPos(m.contained, m.pos, "Feature Warning")
108+
case m: DeprecationWarning =>
109+
printMessageAndPos(m.contained, m.pos, "Deprecation Warning")
110+
case m: UncheckedWarning =>
111+
printMessageAndPos(m.contained, m.pos, "Unchecked Warning")
103112
case m: MigrationWarning =>
104-
printMessageAndPos(m.contained, m.pos, m.kind)
113+
printMessageAndPos(m.contained, m.pos, "Migration Warning")
105114
case m: Warning =>
106-
printMessageAndPos(m.contained, m.pos, m.kind)
107-
case _ =>
108-
printMessageAndPos(m.contained, m.pos, m.kind)
115+
printMessageAndPos(m.contained, m.pos, "Warning")
116+
case m: Info =>
117+
printMessageAndPos(m.contained, m.pos, "Info")
109118
}
110119

111120
if (ctx.shouldExplain(m)) printExplanation(m.contained)

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,32 @@ abstract class Message(val errorId: String) { self =>
1818
def kind: String
1919
def explanation: String
2020

21-
def container(c: String) =
22-
if (kind == "") c
23-
else s"$kind $c"
24-
2521
def mapMsg(f: String => String) = new Message(errorId) {
2622
val msg = f(self.msg)
2723
val kind = self.kind
2824
val explanation = self.explanation
2925
}
3026

3127
def error(pos: SourcePosition) =
32-
new Error(self, pos, container("Error"), explanation)
28+
new Error(self, pos, explanation)
3329

3430
def warning(pos: SourcePosition) =
35-
new Warning(self, pos, container("Warning"), explanation)
31+
new Warning(self, pos, explanation)
3632

3733
def info(pos: SourcePosition) =
38-
new Info(self, pos, container("Info"), explanation)
34+
new Info(self, pos, explanation)
3935

4036
def featureWarning(pos: SourcePosition) =
41-
new FeatureWarning(self, pos, container("Feature Warning"), explanation)
37+
new FeatureWarning(self, pos, explanation)
4238

4339
def uncheckedWarning(pos: SourcePosition) =
44-
new UncheckedWarning(self, pos, container("Unchecked Warning"), explanation)
40+
new UncheckedWarning(self, pos, explanation)
4541

4642
def deprecationWarning(pos: SourcePosition) =
47-
new DeprecationWarning(self, pos, container("Deprecation Warning"), explanation)
43+
new DeprecationWarning(self, pos, explanation)
4844

4945
def migrationWarning(pos: SourcePosition) =
50-
new MigrationWarning(self, pos, container("Migration Warning"), explanation)
46+
new MigrationWarning(self, pos, explanation)
5147
}
5248

5349
class NoExplanation(val msg: String) extends Message("") {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class MessageContainer(
2727
msgFn: => Message,
2828
val pos: SourcePosition,
2929
val level: Int,
30-
val kind: String,
3130
val explanation: String
3231
) extends Exception with interfaces.Diagnostic {
3332
import MessageContainer._

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

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,66 +18,58 @@ object messages {
1818
class Error(
1919
msgFn: => Message,
2020
pos: SourcePosition,
21-
kind: String,
2221
explanation: String = ""
23-
) extends MessageContainer(msgFn, pos, ERROR, kind, explanation)
22+
) extends MessageContainer(msgFn, pos, ERROR, explanation)
2423

2524
class Warning(
2625
msgFn: => Message,
2726
pos: SourcePosition,
28-
kind: String,
2927
explanation: String = ""
30-
) extends MessageContainer(msgFn, pos, WARNING, kind, explanation)
28+
) extends MessageContainer(msgFn, pos, WARNING, explanation)
3129

3230
class Info(
3331
msgFn: => Message,
3432
pos: SourcePosition,
35-
kind: String,
3633
explanation: String = ""
37-
) extends MessageContainer(msgFn, pos, INFO, kind, explanation)
34+
) extends MessageContainer(msgFn, pos, INFO, explanation)
3835

3936
abstract class ConditionalWarning(
4037
msgFn: => Message,
4138
pos: SourcePosition,
42-
kind: String,
4339
explanation: String = ""
44-
) extends Warning(msgFn, pos, kind, explanation) {
40+
) extends Warning(msgFn, pos, explanation) {
4541
def enablingOption(implicit ctx: Context): Setting[Boolean]
4642
}
4743

4844
class FeatureWarning(
4945
msgFn: => Message,
5046
pos: SourcePosition,
51-
kind: String = "Feature Warning",
5247
explanation: String = ""
53-
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
48+
) extends ConditionalWarning(msgFn, pos, explanation) {
5449
def enablingOption(implicit ctx: Context) = ctx.settings.feature
5550
}
5651

5752
class UncheckedWarning(
5853
msgFn: => Message,
5954
pos: SourcePosition,
60-
kind: String = "Unchecked Warning",
6155
explanation: String = ""
62-
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
56+
) extends ConditionalWarning(msgFn, pos, explanation) {
6357
def enablingOption(implicit ctx: Context) = ctx.settings.unchecked
6458
}
6559

6660
class DeprecationWarning(
6761
msgFn: => Message,
6862
pos: SourcePosition,
69-
kind: String = "Deprecation Warning",
7063
explanation: String = ""
71-
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
64+
) extends ConditionalWarning(msgFn, pos, explanation) {
7265
def enablingOption(implicit ctx: Context) = ctx.settings.deprecation
7366
}
7467

7568
class MigrationWarning(
7669
msgFn: => Message,
7770
pos: SourcePosition,
78-
kind: String = "Migration Warning",
7971
explanation: String = ""
80-
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
72+
) extends ConditionalWarning(msgFn, pos, explanation) {
8173
def enablingOption(implicit ctx: Context) = ctx.settings.migration
8274
}
8375

0 commit comments

Comments
 (0)