Skip to content

Commit e4bdcbf

Browse files
authored
Merge pull request #3059 from DougGregor/formatted-message
Add a new API DiagnosticsFormatter.formattedMessage(_:) for messages without source location info
2 parents fb65fcb + 8ced3b6 commit e4bdcbf

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

Release Notes/602.md

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/2981
1313
- 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.
1414

15+
- `DiagnosticsFormatter` has a new method, `formattedMessage`, that formats a diagnostic message without any corresponding syntax node.
16+
- Description: Some tools want to use the diagnostics formatter to produce diagnostics that don't relate to source code, or for which the source code isn't available. This API allows them to do so while maintaining consistent presentation.
17+
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/3059
18+
- Migration steps: None required.
19+
1520
- `FixIt.Change` has a new case `replaceText` that performs a textual replacement of a range of text with another string.
1621
- Description: The other `FixIt.Change` cases provide structured
1722
modifications to syntax trees, such as replacing specific notes. Some

Sources/SwiftDiagnostics/DiagnosticsFormatter.swift

+6
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ public struct DiagnosticsFormatter {
360360
)
361361
}
362362

363+
/// Produce a string that formats the given diagnostic message with any
364+
/// source-location information.
365+
public func formattedMessage(_ message: some DiagnosticMessage) -> String {
366+
diagnosticDecorator.decorateDiagnosticMessage(message)
367+
}
368+
363369
/// Produce a string containing "footnotes" for each of the diagnostic
364370
/// category provided that has associated documentation. Each category
365371
/// is printed in Markdown link format, e.g.,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftDiagnostics
14+
import XCTest
15+
16+
final class DiagnosticFormatterTests: XCTestCase {
17+
func testFormattedMessage() {
18+
let message = SimpleDiagnosticMessage(
19+
message: "something went wrong",
20+
diagnosticID: MessageID(domain: "swift-syntax", id: "testing"),
21+
severity: .error,
22+
category: DiagnosticCategory(
23+
name: "Testing",
24+
documentationURL: "http://example.com"
25+
)
26+
)
27+
28+
let formattedText = DiagnosticsFormatter().formattedMessage(message)
29+
XCTAssertEqual(formattedText, "error: something went wrong [#Testing]")
30+
}
31+
}

0 commit comments

Comments
 (0)