Skip to content

Commit 335d341

Browse files
authored
Merge pull request #2158 from DougGregor/colorize-less-often
[Diagnostics] Avoid adjacent ANSI sequences when not needed
2 parents 2444350 + 81c5285 commit 335d341

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public struct DiagnosticsFormatter {
342342
severityAnnotation = .remarkText
343343
}
344344

345-
let prefix = colorizeIfRequested("\(severityText): ", annotation: severityAnnotation)
345+
let prefix = colorizeIfRequested("\(severityText): ", annotation: severityAnnotation, resetAfter: false)
346346

347347
return prefix + colorizeIfRequested(message, annotation: .diagnosticText);
348348
}
@@ -351,13 +351,14 @@ public struct DiagnosticsFormatter {
351351
/// supposed to color the output.
352352
private func colorizeIfRequested(
353353
_ text: String,
354-
annotation: ANSIAnnotation
354+
annotation: ANSIAnnotation,
355+
resetAfter: Bool = true
355356
) -> String {
356357
guard colorize, !text.isEmpty else {
357358
return text
358359
}
359360

360-
return annotation.applied(to: text)
361+
return annotation.applied(to: text, resetAfter: resetAfter)
361362
}
362363

363364
/// Colorize for the buffer outline and line numbers.
@@ -403,7 +404,11 @@ struct ANSIAnnotation {
403404
return ANSIAnnotation(color: self.color, trait: trait)
404405
}
405406

406-
func applied(to message: String) -> String {
407+
func applied(to message: String, resetAfter: Bool = true) -> String {
408+
guard resetAfter else {
409+
return "\(code)\(message)"
410+
}
411+
407412
// Resetting after the message ensures that we don't color unintended lines in the output
408413
return "\(code)\(message)\(ANSIAnnotation.normal.code)"
409414
}

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: \u{001B}[0;0m\u{001B}[1;39mexpected expression after operator\u{001B}[0;0m
104+
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \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: \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
116+
\u{001B}[0;36m│\u{001B}[0;0m │ │ ╰─ \u{001B}[1;31merror: \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}[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}[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: \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
131+
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: \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}[1;39mC-style for statement has been removed in Swift 3\u{001B}[0;0m
133133
134134
"""
135135

0 commit comments

Comments
 (0)