Skip to content

[Diagnostic formatting] Add a category footnote printer #3007

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 10, 2025
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
2 changes: 1 addition & 1 deletion Release Notes/602.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## New APIs

- `DiagnosticMessage` has a new optional property, `category`, that providesa category name and documentation URL for a diagnostic.
- Description: Tools often have many different diagnostics. Diagnostic categories allow tools to group several diagnostics together with documentation that can help users understand what the diagnostics mean and how to address them. This API allows diagnostics to provide this category information. The diagnostic renderer will provide the category at the end of the diagnostic message in the form `[#CategoryName]`.
- Description: Tools often have many different diagnostics. Diagnostic categories allow tools to group several diagnostics together with documentation that can help users understand what the diagnostics mean and how to address them. This API allows diagnostics to provide this category information. The diagnostic renderer will provide the category at the end of the diagnostic message in the form `[#CategoryName]`, and can print categories as "footnotes" with its `categoryFootnotes` method.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2981
- Migration steps: None required. The new `category` property has optional type, and there is a default implementation that returns `nil`. Types that conform to `DiagnosticMessage` can choose to implement this property and provide a category when appropriate.

Expand Down
36 changes: 36 additions & 0 deletions Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,40 @@ public struct DiagnosticsFormatter {
suffixTexts: [:]
)
}

/// Produce a string containing "footnotes" for each of the diagnostic
/// category provided that has associated documentation. Each category
/// is printed in Markdown link format, e.g.,
///
/// ```
/// [#categoryName]: <categoryDocumentationURL>
/// ```
///
/// This function also deduplicates entries and alphabetizes the results.
Copy link
Contributor

@ktoso ktoso Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, worth doing that 👍

///
/// - Parameters:
/// - categories: the categories to print
/// - leadingText: text that is prefixed to the list of categories when
/// there is at least one category to print.
public func categoryFootnotes(
_ categories: [DiagnosticCategory],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we make this some Collection<DiagnosticCategory>, say if somebody collected the categories in a Set (which doesn’t seem too unreasonable to me).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! #3008

leadingText: String = "\n"
) -> String {
let categoriesInOrder = categories.compactMap { category in
if let documentationURL = category.documentationURL {
return (category.name, documentationURL)
} else {
return nil
}
}.sorted { $0.0.lowercased() < $1.0.lowercased() }

if categoriesInOrder.isEmpty {
return ""
}

return leadingText
+ categoriesInOrder.map { name, url in
"[#\(name)]: <\(url)>"
}.joined(separator: "\n")
}
}
26 changes: 26 additions & 0 deletions Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,30 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
"""
)
}

func testCategoryFootnotes() {
let categories = [
DiagnosticCategory(
name: "StrictMemorySafety",
documentationURL: "http://example.com/memory-safety"
),
DiagnosticCategory(
name: "deprecated",
documentationURL: "http://example.com/deprecated"
),
DiagnosticCategory(name: "nothing", documentationURL: nil),
]

assertStringsEqualWithDiff(
DiagnosticsFormatter().categoryFootnotes(
categories,
leadingText: "Footnotes:\n"
),
"""
Footnotes:
[#deprecated]: <http://example.com/deprecated>
[#StrictMemorySafety]: <http://example.com/memory-safety>
"""
)
}
}