Skip to content

Commit e591684

Browse files
Merge pull request #6293 from dotty-staging/improve-warning-printing-format
Improve error/waring error format
2 parents 1578b88 + 2874e9b commit e591684

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.lang.System.{lineSeparator => EOL}
66

77
import core.Contexts.Context
88
import core.Decorators._
9-
import printing.Highlighting.{Blue, Red}
9+
import printing.Highlighting.{Blue, Red, Yellow}
1010
import printing.SyntaxHighlighting
1111
import diagnostic.{ErrorMessageID, Message, MessageContainer}
1212
import diagnostic.messages._
@@ -42,14 +42,14 @@ trait MessageRendering {
4242
*
4343
* @return (lines before error, lines after error, line numbers offset)
4444
*/
45-
def sourceLines(pos: SourcePosition)(implicit ctx: Context): (List[String], List[String], Int) = {
45+
def sourceLines(pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): (List[String], List[String], Int) = {
4646
var maxLen = Int.MinValue
4747
def render(offsetAndLine: (Int, String)): String = {
4848
val (offset, line) = offsetAndLine
4949
val lineNbr = pos.source.offsetToLine(offset)
5050
val prefix = s"${lineNbr + 1} |"
5151
maxLen = math.max(maxLen, prefix.length)
52-
val lnum = Red(" " * math.max(0, maxLen - prefix.length) + prefix).show
52+
val lnum = hl(diagnosticLevel)(" " * math.max(0, maxLen - prefix.length) + prefix)
5353
lnum + line.stripLineEnd
5454
}
5555

@@ -78,15 +78,15 @@ trait MessageRendering {
7878
}
7979

8080
/** The column markers aligned under the error */
81-
def columnMarker(pos: SourcePosition, offset: Int)(implicit ctx: Context): String = {
81+
def columnMarker(pos: SourcePosition, offset: Int, diagnosticLevel: String)(implicit ctx: Context): String = {
8282
val prefix = " " * (offset - 1)
8383
val padding = pos.startColumnPadding
84-
val carets = Red {
84+
val carets = hl(diagnosticLevel) {
8585
if (pos.startLine == pos.endLine)
8686
"^" * math.max(1, pos.endColumn - pos.startColumn)
8787
else "^"
8888
}
89-
s"$prefix|$padding${carets.show}"
89+
s"$prefix|$padding$carets"
9090
}
9191

9292
/** The error message (`msg`) aligned under `pos`
@@ -112,7 +112,7 @@ trait MessageRendering {
112112
* @return separator containing error location and kind
113113
*/
114114
def posStr(pos: SourcePosition, diagnosticLevel: String, message: Message)(implicit ctx: Context): String =
115-
if (pos.exists) Blue({
115+
if (pos.exists) hl(diagnosticLevel)({
116116
val file = s"${pos.source.file.toString}:${pos.line + 1}:${pos.column}"
117117
val errId =
118118
if (message.errorId ne ErrorMessageID.NoExplanationID) {
@@ -126,7 +126,7 @@ trait MessageRendering {
126126

127127
prefix +
128128
("-" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0))
129-
}).show else ""
129+
}) else ""
130130

131131
/** Explanation rendered under "Explanation" header */
132132
def explanation(m: Message)(implicit ctx: Context): String = {
@@ -146,14 +146,22 @@ trait MessageRendering {
146146
val posString = posStr(pos, diagnosticLevel, msg)
147147
if (posString.nonEmpty) sb.append(posString).append(EOL)
148148
if (pos.exists) {
149-
val (srcBefore, srcAfter, offset) = sourceLines(pos)
150-
val marker = columnMarker(pos, offset)
149+
val (srcBefore, srcAfter, offset) = sourceLines(pos, diagnosticLevel)
150+
val marker = columnMarker(pos, offset, diagnosticLevel)
151151
val err = errorMsg(pos, msg.msg, offset)
152152
sb.append((srcBefore ::: marker :: err :: outer(pos, " " * (offset - 1)) ::: srcAfter).mkString(EOL))
153153
} else sb.append(msg.msg)
154154
sb.toString
155155
}
156156

157+
def hl(diagnosticLevel: String)(str: String)(implicit ctx: Context): String = diagnosticLevel match {
158+
case "Info" => Blue(str).show
159+
case "Error" => Red(str).show
160+
case _ =>
161+
assert(diagnosticLevel.contains("Warning"))
162+
Yellow(str).show
163+
}
164+
157165
def diagnosticLevel(cont: MessageContainer): String =
158166
cont match {
159167
case m: Error => "Error"

0 commit comments

Comments
 (0)