Skip to content

Commit a4cfdd8

Browse files
committed
Use the grouped diagnostics formatter in swift-parser-cli print-diags
The grouped diagnostics formatter is what we use to render diagnsotics from the compiler, so I think that `swift-parser-cli print-diags` should match that formatting style.
1 parent 902ef0b commit a4cfdd8

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

Sources/SwiftDiagnostics/GroupedDiagnostics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public struct GroupedDiagnostics {
6161
/// absolute position within that parent source file.
6262
///
6363
/// - Returns: The unique ID for this source file.
64+
@discardableResult
6465
public mutating func addSourceFile(
6566
tree: SourceFileSyntax,
6667
displayName: String,
@@ -206,4 +207,13 @@ extension DiagnosticsFormatter {
206207
group.annotateSource(rootSourceFileID, formatter: self, indentString: "")
207208
}.joined(separator: "\n")
208209
}
210+
211+
public static func annotateSources(
212+
in group: GroupedDiagnostics,
213+
contextSize: Int = 2,
214+
colorize: Bool = false
215+
) -> String {
216+
let formatter = DiagnosticsFormatter(contextSize: contextSize, colorize: colorize)
217+
return formatter.annotateSources(in: group)
218+
}
209219
}

Sources/swift-parser-cli/swift-parser-cli.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ class PrintDiags: ParsableCommand {
214214
if foldSequences {
215215
diags += foldAllSequences(tree).1
216216
}
217-
let annotatedSource = DiagnosticsFormatter.annotatedSource(
218-
tree: tree,
219-
diags: diags,
217+
218+
var group = GroupedDiagnostics()
219+
group.addSourceFile(tree: tree, displayName: sourceFile ?? "stdin", diagnostics: diags)
220+
let annotatedSource = DiagnosticsFormatter.annotateSources(
221+
in: group,
220222
colorize: colorize || TerminalHelper.isConnectedToTerminal
221223
)
222224

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
105105
]
106106
)
107107

108-
let formatter = DiagnosticsFormatter()
109-
let annotated = formatter.annotateSources(in: group)
110-
print(annotated)
108+
let annotated = DiagnosticsFormatter.annotateSources(in: group)
111109
AssertStringsEqualWithDiff(
112110
annotated,
113111
"""
@@ -174,9 +172,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
174172
]
175173
)
176174

177-
let formatter = DiagnosticsFormatter()
178-
let annotated = formatter.annotateSources(in: group)
179-
print(annotated)
175+
let annotated = DiagnosticsFormatter.annotateSources(in: group)
180176
AssertStringsEqualWithDiff(
181177
annotated,
182178
"""

0 commit comments

Comments
 (0)