Skip to content

Use the grouped diagnostics formatter in swift-parser-cli print-diags #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/SwiftDiagnostics/GroupedDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public struct GroupedDiagnostics {
/// absolute position within that parent source file.
///
/// - Returns: The unique ID for this source file.
@discardableResult
public mutating func addSourceFile(
tree: SourceFileSyntax,
displayName: String,
Expand Down Expand Up @@ -206,4 +207,13 @@ extension DiagnosticsFormatter {
group.annotateSource(rootSourceFileID, formatter: self, indentString: "")
}.joined(separator: "\n")
}

public static func annotateSources(
in group: GroupedDiagnostics,
contextSize: Int = 2,
colorize: Bool = false
) -> String {
let formatter = DiagnosticsFormatter(contextSize: contextSize, colorize: colorize)
return formatter.annotateSources(in: group)
}
}
8 changes: 5 additions & 3 deletions Sources/swift-parser-cli/swift-parser-cli.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ class PrintDiags: ParsableCommand {
if foldSequences {
diags += foldAllSequences(tree).1
}
let annotatedSource = DiagnosticsFormatter.annotatedSource(
tree: tree,
diags: diags,

var group = GroupedDiagnostics()
group.addSourceFile(tree: tree, displayName: sourceFile ?? "stdin", diagnostics: diags)
let annotatedSource = DiagnosticsFormatter.annotateSources(
in: group,
colorize: colorize || TerminalHelper.isConnectedToTerminal
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
]
)

let formatter = DiagnosticsFormatter()
let annotated = formatter.annotateSources(in: group)
print(annotated)
let annotated = DiagnosticsFormatter.annotateSources(in: group)
AssertStringsEqualWithDiff(
annotated,
"""
Expand Down Expand Up @@ -174,9 +172,7 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
]
)

let formatter = DiagnosticsFormatter()
let annotated = formatter.annotateSources(in: group)
print(annotated)
let annotated = DiagnosticsFormatter.annotateSources(in: group)
AssertStringsEqualWithDiff(
annotated,
"""
Expand Down