Skip to content

Commit 3e56ebf

Browse files
committed
Add a double-nested expansion and fix line-limit padding
1 parent 9e54b93 commit 3e56ebf

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,18 @@ extension GroupedDiagnostics {
158158
prefixString = ""
159159
suffixString = ""
160160
} else {
161+
let padding = indentString.dropLast(1)
162+
161163
// Should we make this depend on the width of the snippet itself?
162164
let targetLineLength = 72
163-
let extraLengthNeeded = targetLineLength - indentString.count - 5
165+
let extraLengthNeeded = targetLineLength - padding.count - sourceFile.displayName.count - 5
164166
let boxSuffix: String
165167
if extraLengthNeeded > 0 {
166168
boxSuffix = String(repeating: "", count: extraLengthNeeded)
167169
} else {
168170
boxSuffix = ""
169171
}
170172

171-
let padding = indentString.dropLast(1)
172173
prefixString = padding + "╭-── " + sourceFile.displayName + boxSuffix + "\n"
173174
suffixString = padding + "╰-──" + String(repeating: "", count: sourceFile.displayName.count + 1) + boxSuffix + "\n"
174175
}

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,85 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
113113
1 │ let pi = 3.14159
114114
2 │ #myAssert(pi == 3)
115115
∣ ╰─ in expansion of macro 'myAssert' here
116-
╭-── #myAssert───────────────────────────────────────────────────────────────
116+
╭-── #myAssert───────────────────────────────────────────────────────
117117
|1 │ let __a = pi
118118
|2 │ let __b = 3
119119
|3 │ if !(__a == __b) {
120120
| ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
121121
|4 │ fatalError("assertion failed: pi != 3")
122122
|5 │ }
123-
╰-───────────────────────────────────────────────────────────────────────────
123+
╰-───────────────────────────────────────────────────────────────────
124+
3 │ print("hello"
125+
∣ ╰─ expected ')' to end function call
126+
127+
"""
128+
)
129+
}
130+
131+
func testGroupingForDoubleNestedMacroExpansion() {
132+
var group = GroupedDiagnostics()
133+
134+
// Main source file.
135+
let (mainSourceID, mainSourceMarkers) = group.addTestFile(
136+
"""
137+
let pi = 3.14159
138+
0️⃣#myAssert(pi == 3)
139+
print("hello"
140+
""",
141+
displayName: "main.swift",
142+
extraDiagnostics: ["0️⃣" : ("in expansion of macro 'myAssert' here", .note)]
143+
)
144+
let inExpansionNotePos = mainSourceMarkers["0️⃣"]!
145+
146+
// Outer expansion source file
147+
let (outerExpansionSourceID, outerExpansionSourceMarkers) = group.addTestFile(
148+
"""
149+
let __a = pi
150+
let __b = 3
151+
if 0️⃣#invertedEqualityCheck(__a, __b) {
152+
fatalError("assertion failed: pi != 3")
153+
}
154+
""",
155+
displayName: "#myAssert",
156+
parent: (mainSourceID, inExpansionNotePos),
157+
extraDiagnostics: [
158+
"0️⃣" : ("in expansion of macro 'invertedEqualityCheck' here", .note)
159+
]
160+
)
161+
let inInnerExpansionNotePos = outerExpansionSourceMarkers["0️⃣"]!
162+
163+
// Expansion source file
164+
_ = group.addTestFile(
165+
"""
166+
!(__a 0️⃣== __b)
167+
""",
168+
displayName: "#invertedEqualityCheck",
169+
parent: (outerExpansionSourceID, inInnerExpansionNotePos),
170+
extraDiagnostics: [
171+
"0️⃣" : ("no matching operator '==' for types 'Double' and 'Int'", .error)
172+
]
173+
)
174+
175+
let formatter = DiagnosticsFormatter()
176+
let annotated = formatter.annotateSources(in: group)
177+
print(annotated)
178+
AssertStringsEqualWithDiff(annotated, """
179+
=== main.swift ===
180+
1 │ let pi = 3.14159
181+
2 │ #myAssert(pi == 3)
182+
∣ ╰─ in expansion of macro 'myAssert' here
183+
╭-── #myAssert───────────────────────────────────────────────────────
184+
|1 │ let __a = pi
185+
|2 │ let __b = 3
186+
|3 │ if #invertedEqualityCheck(__a, __b) {
187+
| ∣ ╰─ in expansion of macro 'invertedEqualityCheck' here
188+
| ╭-── #invertedEqualityCheck──────────────────────────────────────
189+
| |1 │ !(__a == __b)
190+
| | ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
191+
| ╰-───────────────────────────────────────────────────────────────
192+
|4 │ fatalError("assertion failed: pi != 3")
193+
|5 │ }
194+
╰-───────────────────────────────────────────────────────────────────
124195
3 │ print("hello"
125196
∣ ╰─ expected ')' to end function call
126197

0 commit comments

Comments
 (0)