Skip to content

Add a new API DiagnosticsFormatter.formattedMessage(_:) for messages without source location info #3059

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 2 commits into from
Apr 18, 2025
Merged
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
5 changes: 5 additions & 0 deletions Release Notes/602.md
Original file line number Diff line number Diff line change
@@ -12,6 +12,11 @@
- 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.

- `DiagnosticsFormatter` has a new method, `formattedMessage`, that formats a diagnostic message without any corresponding syntax node.
- 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.
- Pull Request: https://github.com/swiftlang/swift-syntax/pull/3059
- Migration steps: None required.

- `FixIt.Change` has a new case `replaceText` that performs a textual replacement of a range of text with another string.
- Description: The other `FixIt.Change` cases provide structured
modifications to syntax trees, such as replacing specific notes. Some
6 changes: 6 additions & 0 deletions Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
@@ -360,6 +360,12 @@ public struct DiagnosticsFormatter {
)
}

/// Produce a string that formats the given diagnostic message with any
/// source-location information.
public func formattedMessage(_ message: some DiagnosticMessage) -> String {
diagnosticDecorator.decorateDiagnosticMessage(message)
}

/// 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.,
31 changes: 31 additions & 0 deletions Tests/SwiftDiagnosticsTest/DiagnosticFormatterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftDiagnostics
import XCTest

final class DiagnosticFormatterTests: XCTestCase {
func testFormattedMessage() {
let message = SimpleDiagnosticMessage(
message: "something went wrong",
diagnosticID: MessageID(domain: "swift-syntax", id: "testing"),
severity: .error,
category: DiagnosticCategory(
name: "Testing",
documentationURL: "http://example.com"
)
)

let formattedText = DiagnosticsFormatter().formattedMessage(message)
XCTAssertEqual(formattedText, "error: something went wrong [#Testing]")
}
}