@@ -6,7 +6,7 @@ import java.lang.System.{lineSeparator => EOL}
6
6
7
7
import core .Contexts .Context
8
8
import core .Decorators ._
9
- import printing .Highlighting .{Blue , Red }
9
+ import printing .Highlighting .{Blue , Red , Yellow }
10
10
import printing .SyntaxHighlighting
11
11
import diagnostic .{ErrorMessageID , Message , MessageContainer }
12
12
import diagnostic .messages ._
@@ -42,14 +42,14 @@ trait MessageRendering {
42
42
*
43
43
* @return (lines before error, lines after error, line numbers offset)
44
44
*/
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 ) = {
46
46
var maxLen = Int .MinValue
47
47
def render (offsetAndLine : (Int , String )): String = {
48
48
val (offset, line) = offsetAndLine
49
49
val lineNbr = pos.source.offsetToLine(offset)
50
50
val prefix = s " ${lineNbr + 1 } | "
51
51
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)
53
53
lnum + line.stripLineEnd
54
54
}
55
55
@@ -78,15 +78,15 @@ trait MessageRendering {
78
78
}
79
79
80
80
/** 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 = {
82
82
val prefix = " " * (offset - 1 )
83
83
val padding = pos.startColumnPadding
84
- val carets = Red {
84
+ val carets = hl(diagnosticLevel) {
85
85
if (pos.startLine == pos.endLine)
86
86
" ^" * math.max(1 , pos.endColumn - pos.startColumn)
87
87
else " ^"
88
88
}
89
- s " $prefix| $padding${ carets.show} "
89
+ s " $prefix| $padding$carets"
90
90
}
91
91
92
92
/** The error message (`msg`) aligned under `pos`
@@ -112,7 +112,7 @@ trait MessageRendering {
112
112
* @return separator containing error location and kind
113
113
*/
114
114
def posStr (pos : SourcePosition , diagnosticLevel : String , message : Message )(implicit ctx : Context ): String =
115
- if (pos.exists) Blue ({
115
+ if (pos.exists) hl(diagnosticLevel) ({
116
116
val file = s " ${pos.source.file.toString}: ${pos.line + 1 }: ${pos.column}"
117
117
val errId =
118
118
if (message.errorId ne ErrorMessageID .NoExplanationID ) {
@@ -126,7 +126,7 @@ trait MessageRendering {
126
126
127
127
prefix +
128
128
(" -" * math.max(ctx.settings.pageWidth.value - stripColor(prefix).length, 0 ))
129
- }).show else " "
129
+ }) else " "
130
130
131
131
/** Explanation rendered under "Explanation" header */
132
132
def explanation (m : Message )(implicit ctx : Context ): String = {
@@ -146,14 +146,22 @@ trait MessageRendering {
146
146
val posString = posStr(pos, diagnosticLevel, msg)
147
147
if (posString.nonEmpty) sb.append(posString).append(EOL )
148
148
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 )
151
151
val err = errorMsg(pos, msg.msg, offset)
152
152
sb.append((srcBefore ::: marker :: err :: outer(pos, " " * (offset - 1 )) ::: srcAfter).mkString(EOL ))
153
153
} else sb.append(msg.msg)
154
154
sb.toString
155
155
}
156
156
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
+
157
165
def diagnosticLevel (cont : MessageContainer ): String =
158
166
cont match {
159
167
case m : Error => " Error"
0 commit comments