Skip to content

Commit 68934ce

Browse files
committed
[Formatter] Colorize buffer outline and line numbers cyan
Help frame the formatter buffer for color terminals by making the buffer outline and line numbers cyan.
1 parent 5737da6 commit 68934ce

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public struct DiagnosticsFormatter {
9292

9393
// If there was a filename, add it first.
9494
if let fileName = fileName {
95-
annotatedSource.append(indentString)
96-
annotatedSource.append("=== \(fileName) ===\n")
95+
let header = colorizeBufferOutline("===")
96+
annotatedSource.append("\(indentString)\(header) \(fileName) \(header)\n")
9797
}
9898

9999
/// Keep track if a line missing char should be printed
@@ -115,11 +115,11 @@ public struct DiagnosticsFormatter {
115115
// line numbers should be right aligned
116116
let lineNumberString = String(lineNumber)
117117
let leadingSpaces = String(repeating: " ", count: maxNumberOfDigits - lineNumberString.count)
118-
let linePrefix = "\(leadingSpaces)\(lineNumberString)"
118+
let linePrefix = "\(leadingSpaces)\(colorizeBufferOutline("\(lineNumberString)")) "
119119

120120
// If necessary, print a line that indicates that there was lines skipped in the source code
121121
if hasLineBeenSkipped && !annotatedSource.isEmpty {
122-
let lineMissingInfoLine = indentString + String(repeating: " ", count: maxNumberOfDigits) + " "
122+
let lineMissingInfoLine = indentString + String(repeating: " ", count: maxNumberOfDigits) + " \(colorizeBufferOutline(""))"
123123
annotatedSource.append("\(lineMissingInfoLine)\n")
124124
}
125125
hasLineBeenSkipped = false
@@ -144,7 +144,7 @@ public struct DiagnosticsFormatter {
144144

145145
for (column, diags) in diagsPerColumn {
146146
// compute the string that is shown before each message
147-
var preMessage = indentString + String(repeating: " ", count: maxNumberOfDigits) + " "
147+
var preMessage = indentString + String(repeating: " ", count: maxNumberOfDigits) + " " + colorizeBufferOutline("")
148148
for c in 0..<column {
149149
if columnsWithDiagnostics.contains(c) {
150150
preMessage.append("")
@@ -201,6 +201,24 @@ public struct DiagnosticsFormatter {
201201
return message.message
202202
}
203203
}
204+
205+
/// Apply the given color and trait to the specified text, when we are
206+
/// supposed to color the output.
207+
private func colorizeIfRequested(
208+
_ text: String,
209+
annotation: ANSIAnnotation
210+
) -> String {
211+
guard colorize, !text.isEmpty else {
212+
return text
213+
}
214+
215+
return annotation.applied(to: text)
216+
}
217+
218+
/// Colorize for the buffer outline and line numbers.
219+
func colorizeBufferOutline(_ text: String) -> String {
220+
colorizeIfRequested(text, annotation: .bufferOutline)
221+
}
204222
}
205223

206224
struct ANSIAnnotation {
@@ -248,4 +266,9 @@ struct ANSIAnnotation {
248266
static var normal: ANSIAnnotation {
249267
self.init(color: .normal, trait: .normal)
250268
}
269+
270+
/// Annotation used for the outline and line numbers of a buffer.
271+
static var bufferOutline: ANSIAnnotation {
272+
ANSIAnnotation(color: .cyan, trait: .normal)
273+
}
251274
}

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ extension GroupedDiagnostics {
145145
tree: sourceFile.tree
146146
)
147147

148+
let colorizeBufferOutline = formatter.colorizeBufferOutline
149+
148150
let childPadding = String(slc.sourceLines.count + 1).count + 1;
149151

150152
let childSources: [(AbsolutePosition, String)] = sourceFiles[sourceFileID.id].children.map { childBufferID in
@@ -170,16 +172,16 @@ extension GroupedDiagnostics {
170172

171173
// Should we make this depend on the width of the snippet itself?
172174
let targetLineLength = 72
173-
let extraLengthNeeded = targetLineLength - padding.count - sourceFile.displayName.count - 5
175+
let extraLengthNeeded = targetLineLength - padding.count - sourceFile.displayName.count - 6
174176
let boxSuffix: String
175177
if extraLengthNeeded > 0 {
176-
boxSuffix = String(repeating: "", count: extraLengthNeeded)
178+
boxSuffix = colorizeBufferOutline(String(repeating: "", count: extraLengthNeeded))
177179
} else {
178180
boxSuffix = ""
179181
}
180182

181-
prefixString = padding + "╭-── " + sourceFile.displayName + boxSuffix + "\n"
182-
suffixString = padding + "╰-──" + String(repeating: "", count: sourceFile.displayName.count + 1) + boxSuffix + "\n"
183+
prefixString = colorizeBufferOutline(padding + "╭-── ") + sourceFile.displayName + " " + boxSuffix + "\n"
184+
suffixString = colorizeBufferOutline(padding + "╰-──" + String(repeating: "", count: sourceFile.displayName.count + 2)) + boxSuffix + "\n"
183185
}
184186

185187
// Render the buffer.
@@ -188,7 +190,7 @@ extension GroupedDiagnostics {
188190
fileName: isRoot ? sourceFile.displayName : nil,
189191
tree: sourceFile.tree,
190192
diags: sourceFile.diagnostics,
191-
indentString: indentString,
193+
indentString: colorizeBufferOutline(indentString),
192194
suffixText: childSources,
193195
sourceLocationConverter: slc
194196
) + suffixString

Tests/SwiftDiagnosticsTest/DiagnosticsFormatterTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ final class DiagnosticsFormatterTests: XCTestCase {
100100
"""
101101

102102
let expectedOutput = """
103-
1 │ var foo = bar +
104-
╰─ \u{001B}[1;31merror: expected expression after operator\u{001B}[0;0m
103+
\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
105105
106106
"""
107107
AssertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)
@@ -112,10 +112,10 @@ final class DiagnosticsFormatterTests: XCTestCase {
112112
foo.[].[].[]
113113
"""
114114
let expectedOutput = """
115-
1 │ foo.[].[].[]
116-
│ │ ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
117-
│ ╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
118-
╰─ \u{001B}[1;31merror: expected name in member access\u{001B}[0;0m
115+
\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
119119
120120
"""
121121
AssertStringsEqualWithDiff(annotate(source: source, colorize: true), expectedOutput)

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
115115
1 │ let pi = 3.14159
116116
2 │ #myAssert(pi == 3)
117117
∣ ╰─ in expansion of macro 'myAssert' here
118-
╭-── #myAssert───────────────────────────────────────────────────────
118+
╭-── #myAssert ───────────────────────────────────────────────────────
119119
│1 │ let __a = pi
120120
│2 │ let __b = 3
121121
│3 │ if !(__a == __b) {
@@ -184,12 +184,12 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
184184
1 │ let pi = 3.14159
185185
2 │ #myAssert(pi == 3)
186186
∣ ╰─ in expansion of macro 'myAssert' here
187-
╭-── #myAssert───────────────────────────────────────────────────────
187+
╭-── #myAssert ───────────────────────────────────────────────────────
188188
│1 │ let __a = pi
189189
│2 │ let __b = 3
190190
│3 │ if #invertedEqualityCheck(__a, __b) {
191191
│ ∣ ╰─ in expansion of macro 'invertedEqualityCheck' here
192-
│ ╭-── #invertedEqualityCheck───────────────────────────────────────
192+
│ ╭-── #invertedEqualityCheck ───────────────────────────────────────
193193
│ │1 │ !(__a == __b)
194194
│ │ ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
195195
│ ╰-─────────────────────────────────────────────────────────────────

0 commit comments

Comments
 (0)