Skip to content

Commit df54241

Browse files
committed
Use bold without colors for diagnostic text
Match the existing LLVM style used by the Swift compiler, where the diagnostic severity (warning, note, error) is colorized and bolded, but the diagnostic text is bolded but in the default color. Visually, this is less overwhelming, which is presumably why the compiler has been doing it.
1 parent 39182b6 commit df54241

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,26 +321,31 @@ public struct DiagnosticsFormatter {
321321
/// Annotates the given ``DiagnosticMessage`` with an appropriate ANSI color code (if the value of the `colorize`
322322
/// property is `true`) and returns the result as a printable string.
323323
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 {
325333
case .error:
326-
let annotation = ANSIAnnotation(color: .red, trait: .bold)
327-
return colorizeIfRequested("error: \(message.message)", annotation: annotation)
334+
severityText = "error"
335+
severityAnnotation = .errorText
328336

329337
case .warning:
330-
let color = ANSIAnnotation(color: .yellow)
331-
let prefix = colorizeIfRequested("warning: ", annotation: color.withTrait(.bold))
338+
severityText = "warning"
339+
severityAnnotation = .warningText
332340

333-
return prefix + colorizeIfRequested(message.message, annotation: color);
334341
case .note:
335-
return colorizeNoteIfRequested(message.message)
342+
severityText = "note"
343+
severityAnnotation = .noteText
336344
}
337-
}
338345

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);
344349
}
345350

346351
/// Apply the given color and trait to the specified text, when we are
@@ -418,4 +423,20 @@ struct ANSIAnnotation {
418423
static var sourceHighlight: ANSIAnnotation {
419424
ANSIAnnotation(color: .default, trait: .underline)
420425
}
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+
}
421442
}

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ extension GroupedDiagnostics {
214214

215215
if rootSourceID == sourceFileID {
216216
let bufferLoc = slc.location(for: rootPosition)
217-
prefixString += "╰─ \(bufferLoc.file):\(bufferLoc.line):\(bufferLoc.column): \(formatter.colorizeNoteIfRequested("expanded code originates here"))\n"
217+
let coloredMessage = formatter.colorizeIfRequested(severity: .note, message: "expanded code originates here")
218+
prefixString += "╰─ \(bufferLoc.file):\(bufferLoc.line):\(bufferLoc.column): \(coloredMessage)\n"
218219
}
219220
}
220221
} else {

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ final class DiagnosticsFormatterTests: XCTestCase {
101101

102102
let expectedOutput = """
103103
\u{001B}[0;36m1 │\u{001B}[0;0m var foo = bar +
104-
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: expected expression after operator\u{001B}[0;0m
104+
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mexpected expression after operator\u{001B}[0;0m
105105
106106
"""
107107
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
@@ -113,9 +113,9 @@ final class DiagnosticsFormatterTests: XCTestCase {
113113
"""
114114
let expectedOutput = """
115115
\u{001B}[0;36m1 │\u{001B}[0;0m foo.[].[].[]
116-
\u{001B}[0;36m│\u{001B}[0;0m │ │ ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
117-
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
118-
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
116+
\u{001B}[0;36m│\u{001B}[0;0m │ │ ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mexpected name in member access\u{001B}[0;0m
117+
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mexpected name in member access\u{001B}[0;0m
118+
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mexpected name in member access\u{001B}[0;0m
119119
120120
"""
121121
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
@@ -128,8 +128,8 @@ final class DiagnosticsFormatterTests: XCTestCase {
128128

129129
let expectedOutput = """
130130
\u{001B}[0;36m1 │\u{001B}[0;0m for \u{001B}[4;39m(i\u{001B}[0;0m \u{001B}[4;39m= 🐮; i != 👩‍👩‍👦‍👦; i += 1)\u{001B}[0;0m { }
131-
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: expected ')' to end tuple pattern\u{001B}[0;0m
132-
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: C-style for statement has been removed in Swift 3\u{001B}[0;0m
131+
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mexpected ')' to end tuple pattern\u{001B}[0;0m
132+
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \u{001B}[0;0m\u{001B}[1;39mC-style for statement has been removed in Swift 3\u{001B}[0;0m
133133
134134
"""
135135

0 commit comments

Comments
 (0)