Skip to content

Commit 20d5fb9

Browse files
authored
Merge pull request #1391 from ahoppen/ahoppen/grouped-formatter
Use the grouped diagnostics formatter in `swift-parser-cli print-diags`
2 parents b6adf00 + a4cfdd8 commit 20d5fb9

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
@@ -108,9 +108,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
108108
]
109109
)
110110

111-
let formatter = DiagnosticsFormatter()
112-
let annotated = formatter.annotateSources(in: group)
113-
print(annotated)
111+
let annotated = DiagnosticsFormatter.annotateSources(in: group)
114112
AssertStringsEqualWithDiff(
115113
annotated,
116114
"""
@@ -179,9 +177,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
179177
]
180178
)
181179

182-
let formatter = DiagnosticsFormatter()
183-
let annotated = formatter.annotateSources(in: group)
184-
print(annotated)
180+
let annotated = DiagnosticsFormatter.annotateSources(in: group)
185181
AssertStringsEqualWithDiff(
186182
annotated,
187183
"""

0 commit comments

Comments
 (0)