Skip to content

Commit b28bef2

Browse files
committed
Address code review feedback
1 parent 0f4fabe commit b28bef2

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public struct DiagnosticsFormatter {
2020
var diagnostics: [Diagnostic]
2121
var sourceString: String
2222

23-
/// Non-diagnostic text.
23+
/// Non-diagnostic text that is appended after this source line.
24+
///
25+
/// Suffix text can be used to provide more information following a source
26+
/// line, such as to provide an inset source buffer for a macro expansion
27+
/// that occurs on that line.
2428
var suffixText: String
2529

2630
/// Whether this line is free of annotations.
27-
var isEmpty: Bool {
31+
var isFreeOfAnnotations: Bool {
2832
return diagnostics.isEmpty && suffixText.isEmpty
2933
}
3034
}
@@ -51,15 +55,19 @@ public struct DiagnosticsFormatter {
5155
}
5256

5357
/// Print given diagnostics for a given syntax tree on the command line
58+
///
59+
/// - Parameters:
60+
/// - suffixTexts: suffix text to be printed at the given absolute
61+
/// locations within the source file.
5462
func annotatedSource<SyntaxType: SyntaxProtocol>(
5563
fileName: String?,
5664
tree: SyntaxType,
5765
diags: [Diagnostic],
5866
indentString: String,
59-
suffixText: [(AbsolutePosition, String)],
67+
suffixTexts: [(AbsolutePosition, String)],
6068
sourceLocationConverter: SourceLocationConverter? = nil
6169
) -> String {
62-
let slc = sourceLocationConverter ?? SourceLocationConverter(file: "", tree: tree)
70+
let slc = sourceLocationConverter ?? SourceLocationConverter(file: fileName ?? "", tree: tree)
6371

6472
// First, we need to put each line and its diagnostics together
6573
var annotatedSourceLines = [AnnotatedSourceLine]()
@@ -68,7 +76,7 @@ public struct DiagnosticsFormatter {
6876
let diagsForLine = diags.filter { diag in
6977
return diag.location(converter: slc).line == (sourceLineIndex + 1)
7078
}
71-
let suffixText = suffixText.compactMap { (position, text) in
79+
let suffixText = suffixTexts.compactMap { (position, text) in
7280
if slc.location(for: position).line == (sourceLineIndex + 1) {
7381
return text
7482
}
@@ -82,7 +90,7 @@ public struct DiagnosticsFormatter {
8290
// Only lines with diagnostic messages should be printed, but including some context
8391
let rangesToPrint = annotatedSourceLines.enumerated().compactMap { (lineIndex, sourceLine) -> Range<Int>? in
8492
let lineNumber = lineIndex + 1
85-
if !sourceLine.isEmpty {
93+
if !sourceLine.isFreeOfAnnotations {
8694
return Range<Int>(uncheckedBounds: (lower: lineNumber - contextSize, upper: lineNumber + contextSize + 1))
8795
}
8896
return nil
@@ -178,7 +186,7 @@ public struct DiagnosticsFormatter {
178186
tree: tree,
179187
diags: diags,
180188
indentString: "",
181-
suffixText: []
189+
suffixTexts: []
182190
)
183191
}
184192

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ extension GroupedDiagnostics {
191191
tree: sourceFile.tree,
192192
diags: sourceFile.diagnostics,
193193
indentString: colorizeBufferOutline(indentString),
194-
suffixText: childSources,
194+
suffixTexts: childSources,
195195
sourceLocationConverter: slc
196196
) + suffixString
197197
}

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,27 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
8181
let (mainSourceID, mainSourceMarkers) = group.addTestFile(
8282
"""
8383
let pi = 3.14159
84-
0️⃣#myAssert(pi == 3)
84+
1️⃣#myAssert(pi == 3)
8585
print("hello"
8686
""",
8787
displayName: "main.swift",
88-
extraDiagnostics: ["0️⃣": ("in expansion of macro 'myAssert' here", .note)]
88+
extraDiagnostics: ["1️⃣": ("in expansion of macro 'myAssert' here", .note)]
8989
)
90-
let inExpansionNotePos = mainSourceMarkers["0️⃣"]!
90+
let inExpansionNotePos = mainSourceMarkers["1️⃣"]!
9191

9292
// Expansion source file
9393
_ = group.addTestFile(
9494
"""
9595
let __a = pi
9696
let __b = 3
97-
if !(__a 0️⃣== __b) {
97+
if !(__a 1️⃣== __b) {
9898
fatalError("assertion failed: pi != 3")
9999
}
100100
""",
101101
displayName: "#myAssert",
102102
parent: (mainSourceID, inExpansionNotePos),
103103
extraDiagnostics: [
104-
"0️⃣": ("no matching operator '==' for types 'Double' and 'Int'", .error)
104+
"1️⃣": ("no matching operator '==' for types 'Double' and 'Int'", .error)
105105
]
106106
)
107107

@@ -137,40 +137,40 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
137137
let (mainSourceID, mainSourceMarkers) = group.addTestFile(
138138
"""
139139
let pi = 3.14159
140-
0️⃣#myAssert(pi == 3)
140+
1️⃣#myAssert(pi == 3)
141141
print("hello"
142142
""",
143143
displayName: "main.swift",
144-
extraDiagnostics: ["0️⃣": ("in expansion of macro 'myAssert' here", .note)]
144+
extraDiagnostics: ["1️⃣": ("in expansion of macro 'myAssert' here", .note)]
145145
)
146-
let inExpansionNotePos = mainSourceMarkers["0️⃣"]!
146+
let inExpansionNotePos = mainSourceMarkers["1️⃣"]!
147147

148148
// Outer expansion source file
149149
let (outerExpansionSourceID, outerExpansionSourceMarkers) = group.addTestFile(
150150
"""
151151
let __a = pi
152152
let __b = 3
153-
if 0️⃣#invertedEqualityCheck(__a, __b) {
153+
if 1️⃣#invertedEqualityCheck(__a, __b) {
154154
fatalError("assertion failed: pi != 3")
155155
}
156156
""",
157157
displayName: "#myAssert",
158158
parent: (mainSourceID, inExpansionNotePos),
159159
extraDiagnostics: [
160-
"0️⃣": ("in expansion of macro 'invertedEqualityCheck' here", .note)
160+
"1️⃣": ("in expansion of macro 'invertedEqualityCheck' here", .note)
161161
]
162162
)
163-
let inInnerExpansionNotePos = outerExpansionSourceMarkers["0️⃣"]!
163+
let inInnerExpansionNotePos = outerExpansionSourceMarkers["1️⃣"]!
164164

165165
// Expansion source file
166166
_ = group.addTestFile(
167167
"""
168-
!(__a 0️⃣== __b)
168+
!(__a 1️⃣== __b)
169169
""",
170170
displayName: "#invertedEqualityCheck",
171171
parent: (outerExpansionSourceID, inInnerExpansionNotePos),
172172
extraDiagnostics: [
173-
"0️⃣": ("no matching operator '==' for types 'Double' and 'Int'", .error)
173+
"1️⃣": ("no matching operator '==' for types 'Double' and 'Int'", .error)
174174
]
175175
)
176176

0 commit comments

Comments
 (0)