Skip to content

[Diagnostics] Avoid adjacent ANSI sequences when not needed #2158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public struct DiagnosticsFormatter {
severityAnnotation = .remarkText
}

let prefix = colorizeIfRequested("\(severityText): ", annotation: severityAnnotation)
let prefix = colorizeIfRequested("\(severityText): ", annotation: severityAnnotation, resetAfter: false)

return prefix + colorizeIfRequested(message, annotation: .diagnosticText);
}
Expand All @@ -351,13 +351,14 @@ public struct DiagnosticsFormatter {
/// supposed to color the output.
private func colorizeIfRequested(
_ text: String,
annotation: ANSIAnnotation
annotation: ANSIAnnotation,
resetAfter: Bool = true
) -> String {
guard colorize, !text.isEmpty else {
return text
}

return annotation.applied(to: text)
return annotation.applied(to: text, resetAfter: resetAfter)
}

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

func applied(to message: String) -> String {
func applied(to message: String, resetAfter: Bool = true) -> String {
guard resetAfter else {
return "\(code)\(message)"
}

// Resetting after the message ensures that we don't color unintended lines in the output
return "\(code)\(message)\(ANSIAnnotation.normal.code)"
}
Expand Down
12 changes: 6 additions & 6 deletions Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ final class DiagnosticsFormatterTests: XCTestCase {

let expectedOutput = """
\u{001B}[0;36m1 │\u{001B}[0;0m var foo = bar +
\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
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \u{001B}[1;39mexpected expression after operator\u{001B}[0;0m

"""
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
Expand All @@ -113,9 +113,9 @@ final class DiagnosticsFormatterTests: XCTestCase {
"""
let expectedOutput = """
\u{001B}[0;36m1 │\u{001B}[0;0m foo.[].[].[]
\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
\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
\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
\u{001B}[0;36m│\u{001B}[0;0m │ │ ╰─ \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m
\u{001B}[0;36m│\u{001B}[0;0m ╰─ \u{001B}[1;31merror: \u{001B}[1;39mexpected name in member access\u{001B}[0;0m

"""
assertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
Expand All @@ -128,8 +128,8 @@ final class DiagnosticsFormatterTests: XCTestCase {

let expectedOutput = """
\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 { }
\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
\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
\u{001B}[0;36m│\u{001B}[0;0m │ ╰─ \u{001B}[1;31merror: \u{001B}[1;39mexpected ')' to end tuple pattern\u{001B}[0;0m
\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

"""

Expand Down