Skip to content

Commit 344158c

Browse files
committed
Align child boxes with parent line.
1 parent 5ebcebd commit 344158c

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ public struct DiagnosticsFormatter {
5656
tree: SyntaxType,
5757
diags: [Diagnostic],
5858
indentString: String,
59-
suffixText: [(AbsolutePosition, String)]
59+
suffixText: [(AbsolutePosition, String)],
60+
sourceLocationConverter: SourceLocationConverter? = nil
6061
) -> String {
61-
let slc = SourceLocationConverter(file: "", tree: tree)
62+
let slc = sourceLocationConverter ?? SourceLocationConverter(file: "", tree: tree)
6263

6364
// First, we need to put each line and its diagnostics together
6465
var annotatedSourceLines = [AnnotatedSourceLine]()

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,25 @@ extension GroupedDiagnostics {
139139
formatter: DiagnosticsFormatter,
140140
indentString: String
141141
) -> String {
142+
let sourceFile = sourceFiles[sourceFileID.id]
143+
let slc = SourceLocationConverter(
144+
file: sourceFile.displayName,
145+
tree: sourceFile.tree
146+
)
147+
148+
let childPadding = String(slc.sourceLines.count + 1).count + 1;
149+
142150
let childSources: [(AbsolutePosition, String)] = sourceFiles[sourceFileID.id].children.map { childBufferID in
143151
let childSource = annotateSource(
144152
childBufferID,
145153
formatter: formatter,
146-
indentString: indentString + " |"
154+
indentString: indentString + String(repeating: " ", count: childPadding) + "|"
147155
)
148156

149157
return (sourceFiles[childBufferID.id].parent!.1, childSource)
150158
}
151159

152160
// If this is a nested source file, draw a box around it.
153-
let sourceFile = sourceFiles[sourceFileID.id]
154161
let isRoot = sourceFile.parent == nil
155162
let prefixString: String
156163
let suffixString: String
@@ -182,7 +189,8 @@ extension GroupedDiagnostics {
182189
tree: sourceFile.tree,
183190
diags: sourceFile.diagnostics,
184191
indentString: indentString,
185-
suffixText: childSources
192+
suffixText: childSources,
193+
sourceLocationConverter: slc
186194
) + suffixString
187195
}
188196
}

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,14 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
115115
1 │ let pi = 3.14159
116116
2 │ #myAssert(pi == 3)
117117
∣ ╰─ in expansion of macro 'myAssert' here
118-
╭-── #myAssert───────────────────────────────────────────────────────
119-
|1 │ let __a = pi
120-
|2 │ let __b = 3
121-
|3 │ if !(__a == __b) {
122-
| ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
123-
|4 │ fatalError("assertion failed: pi != 3")
124-
|5 │ }
125-
╰-───────────────────────────────────────────────────────────────────
118+
╭-── #myAssert───────────────────────────────────────────────────────
119+
|1 │ let __a = pi
120+
|2 │ let __b = 3
121+
|3 │ if !(__a == __b) {
122+
| ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
123+
|4 │ fatalError("assertion failed: pi != 3")
124+
|5 │ }
125+
╰-────────────────────────────────────────────────────────────────────
126126
3 │ print("hello"
127127
∣ ╰─ expected ')' to end function call
128128
@@ -184,18 +184,18 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
184184
1 │ let pi = 3.14159
185185
2 │ #myAssert(pi == 3)
186186
∣ ╰─ in expansion of macro 'myAssert' here
187-
╭-── #myAssert───────────────────────────────────────────────────────
188-
|1 │ let __a = pi
189-
|2 │ let __b = 3
190-
|3 │ if #invertedEqualityCheck(__a, __b) {
191-
| ∣ ╰─ in expansion of macro 'invertedEqualityCheck' here
192-
| ╭-── #invertedEqualityCheck──────────────────────────────────────
193-
| |1 │ !(__a == __b)
194-
| | ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
195-
| ╰-───────────────────────────────────────────────────────────────
196-
|4 │ fatalError("assertion failed: pi != 3")
197-
|5 │ }
198-
╰-───────────────────────────────────────────────────────────────────
187+
╭-── #myAssert───────────────────────────────────────────────────────
188+
|1 │ let __a = pi
189+
|2 │ let __b = 3
190+
|3 │ if #invertedEqualityCheck(__a, __b) {
191+
| ∣ ╰─ in expansion of macro 'invertedEqualityCheck' here
192+
| ╭-── #invertedEqualityCheck────────────────────────────────────────
193+
| |1 │ !(__a == __b)
194+
| | ∣ ╰─ no matching operator '==' for types 'Double' and 'Int'
195+
| ╰-─────────────────────────────────────────────────────────────────
196+
|4 │ fatalError("assertion failed: pi != 3")
197+
|5 │ }
198+
╰-────────────────────────────────────────────────────────────────────
199199
3 │ print("hello"
200200
∣ ╰─ expected ')' to end function call
201201

0 commit comments

Comments
 (0)