@@ -321,26 +321,31 @@ public struct DiagnosticsFormatter {
321
321
/// Annotates the given ``DiagnosticMessage`` with an appropriate ANSI color code (if the value of the `colorize`
322
322
/// property is `true`) and returns the result as a printable string.
323
323
func colorizeIfRequested( _ message: DiagnosticMessage ) -> String {
324
- switch message. severity {
324
+ colorizeIfRequested ( severity: message. severity, message: message. message)
325
+ }
326
+
327
+ /// Annotates a diagnostic message with the given severity and text with an appropriate ANSI color code.
328
+ func colorizeIfRequested( severity: DiagnosticSeverity , message: String ) -> String {
329
+ let severityText : String
330
+ let severityAnnotation : ANSIAnnotation
331
+
332
+ switch severity {
325
333
case . error:
326
- let annotation = ANSIAnnotation ( color : . red , trait : . bold )
327
- return colorizeIfRequested ( " error: \( message . message ) " , annotation : annotation )
334
+ severityText = " error "
335
+ severityAnnotation = . errorText
328
336
329
337
case . warning:
330
- let color = ANSIAnnotation ( color : . yellow )
331
- let prefix = colorizeIfRequested ( " warning: " , annotation : color . withTrait ( . bold ) )
338
+ severityText = " warning "
339
+ severityAnnotation = . warningText
332
340
333
- return prefix + colorizeIfRequested( message. message, annotation: color) ;
334
341
case . note:
335
- return colorizeNoteIfRequested ( message. message)
342
+ severityText = " note "
343
+ severityAnnotation = . noteText
336
344
}
337
- }
338
345
339
- /// Annotate a note with an appropriate ANSI color code (if requested).
340
- func colorizeNoteIfRequested( _ message: String) - > String {
341
- let color = ANSIAnnotation ( color: . default, trait: . bold)
342
- let prefix = colorizeIfRequested ( " note: " , annotation: color)
343
- return prefix + message
346
+ let prefix = colorizeIfRequested ( " \( severityText) : " , annotation: severityAnnotation)
347
+
348
+ return prefix + colorizeIfRequested( message, annotation: . diagnosticText) ;
344
349
}
345
350
346
351
/// Apply the given color and trait to the specified text, when we are
@@ -418,4 +423,20 @@ struct ANSIAnnotation {
418
423
static var sourceHighlight : ANSIAnnotation {
419
424
ANSIAnnotation ( color: . default, trait: . underline)
420
425
}
426
+
427
+ static var diagnosticText : ANSIAnnotation {
428
+ ANSIAnnotation ( color: . default, trait: . bold)
429
+ }
430
+
431
+ static var errorText : ANSIAnnotation {
432
+ ANSIAnnotation ( color: . red, trait: . bold)
433
+ }
434
+
435
+ static var warningText : ANSIAnnotation {
436
+ ANSIAnnotation ( color: . yellow, trait: . bold)
437
+ }
438
+
439
+ static var noteText : ANSIAnnotation {
440
+ ANSIAnnotation ( color: . default, trait: . bold)
441
+ }
421
442
}
0 commit comments