Skip to content

Commit b9ae3b7

Browse files
authored
Merge pull request #3007 from DougGregor/category-footnotes
[Diagnostic formatting] Add a category footnote printer
2 parents ae69dd5 + 006d266 commit b9ae3b7

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

Release Notes/602.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## New APIs
44

55
- `DiagnosticMessage` has a new optional property, `category`, that providesa category name and documentation URL for a diagnostic.
6-
- 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]`.
6+
- 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.
77
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2981
88
- 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.
99

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

+36
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,40 @@ public struct DiagnosticsFormatter {
351351
suffixTexts: [:]
352352
)
353353
}
354+
355+
/// Produce a string containing "footnotes" for each of the diagnostic
356+
/// category provided that has associated documentation. Each category
357+
/// is printed in Markdown link format, e.g.,
358+
///
359+
/// ```
360+
/// [#categoryName]: <categoryDocumentationURL>
361+
/// ```
362+
///
363+
/// This function also deduplicates entries and alphabetizes the results.
364+
///
365+
/// - Parameters:
366+
/// - categories: the categories to print
367+
/// - leadingText: text that is prefixed to the list of categories when
368+
/// there is at least one category to print.
369+
public func categoryFootnotes(
370+
_ categories: [DiagnosticCategory],
371+
leadingText: String = "\n"
372+
) -> String {
373+
let categoriesInOrder = categories.compactMap { category in
374+
if let documentationURL = category.documentationURL {
375+
return (category.name, documentationURL)
376+
} else {
377+
return nil
378+
}
379+
}.sorted { $0.0.lowercased() < $1.0.lowercased() }
380+
381+
if categoriesInOrder.isEmpty {
382+
return ""
383+
}
384+
385+
return leadingText
386+
+ categoriesInOrder.map { name, url in
387+
"[#\(name)]: <\(url)>"
388+
}.joined(separator: "\n")
389+
}
354390
}

Tests/SwiftDiagnosticsTest/GroupDiagnosticsFormatterTests.swift

+26
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,30 @@ final class GroupedDiagnosticsFormatterTests: XCTestCase {
233233
"""
234234
)
235235
}
236+
237+
func testCategoryFootnotes() {
238+
let categories = [
239+
DiagnosticCategory(
240+
name: "StrictMemorySafety",
241+
documentationURL: "http://example.com/memory-safety"
242+
),
243+
DiagnosticCategory(
244+
name: "deprecated",
245+
documentationURL: "http://example.com/deprecated"
246+
),
247+
DiagnosticCategory(name: "nothing", documentationURL: nil),
248+
]
249+
250+
assertStringsEqualWithDiff(
251+
DiagnosticsFormatter().categoryFootnotes(
252+
categories,
253+
leadingText: "Footnotes:\n"
254+
),
255+
"""
256+
Footnotes:
257+
[#deprecated]: <http://example.com/deprecated>
258+
[#StrictMemorySafety]: <http://example.com/memory-safety>
259+
"""
260+
)
261+
}
236262
}

0 commit comments

Comments
 (0)