Skip to content

Make DiagnosticDecorator semi-public #2557

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

extension DiagnosticDecorator where Self == ANSIDiagnosticDecorator {
/// - SeeAlso: ``ANSIDiagnosticDecorator``
static var ANSI: Self {
public static var ANSI: Self {
Self()
}
}
Expand All @@ -21,9 +21,9 @@ extension DiagnosticDecorator where Self == ANSIDiagnosticDecorator {
/// buffer outlines, and code highlights—by applying severity-based prefixes and ANSI color codes.
///
/// This decorator uses ANSI codes—control characters specialized for text formatting in terminals—to provide visual cues.
@_spi(Testing) public struct ANSIDiagnosticDecorator: DiagnosticDecorator {
public struct ANSIDiagnosticDecorator: DiagnosticDecorator {

@_spi(Testing) public init() {}
public init() {}

/// Decorates a diagnostic message by appending a severity-based prefix and applying ANSI color codes.
///
Expand All @@ -46,7 +46,7 @@ extension DiagnosticDecorator where Self == ANSIDiagnosticDecorator {
/// ```bash
/// printf "\e[1;31merror: \e[1;39mFile not found\e[0;0m\n"
/// ```
@_spi(Testing) public func decorateMessage(
public func decorateMessage(
_ message: String,
basedOnSeverity severity: DiagnosticSeverity
) -> String {
Expand Down Expand Up @@ -85,7 +85,7 @@ extension DiagnosticDecorator where Self == ANSIDiagnosticDecorator {
/// - Parameter bufferOutline: The string representation of the source code buffer outline.
///
/// - Returns: A string featuring ANSI cyan color codes applied to the source code buffer outline.
@_spi(Testing) public func decorateBufferOutline(_ bufferOutline: String) -> String {
public func decorateBufferOutline(_ bufferOutline: String) -> String {
colorizeIfNotEmpty(bufferOutline, usingAnnotation: .bufferOutline)
}

Expand All @@ -109,7 +109,7 @@ extension DiagnosticDecorator where Self == ANSIDiagnosticDecorator {
/// ```bash
/// printf "\e[4;39mlet x = 10\e[0;0m\n"
/// ```
@_spi(Testing) public func decorateHighlight(_ highlight: String) -> (
public func _decorateHighlight(_ highlight: String) -> (
highlightedSourceCode: String, additionalHighlightedLine: String?
) {
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

extension DiagnosticDecorator where Self == BasicDiagnosticDecorator {
/// - Seealso: ``BasicDiagnosticDecorator``
static var basic: Self {
public static var basic: Self {
Self()
}
}
Expand All @@ -21,9 +21,9 @@ extension DiagnosticDecorator where Self == BasicDiagnosticDecorator {
/// buffer outlines, and code highlights—by appending severity-based prefixes.
///
/// Unlike `ANSIDiagnosticDecorator`, this decorator does not use ANSI color codes and solely relies on textual cues.
@_spi(Testing) public struct BasicDiagnosticDecorator: DiagnosticDecorator {
public struct BasicDiagnosticDecorator: DiagnosticDecorator {

@_spi(Testing) public init() {}
public init() {}

/// Decorates a diagnostic message by appending a severity-based prefix.
///
Expand All @@ -32,7 +32,7 @@ extension DiagnosticDecorator where Self == BasicDiagnosticDecorator {
/// - severity: The severity level associated with the diagnostic message.
///
/// - Returns: A string that combines the severity-specific prefix and the original diagnostic message.
@_spi(Testing) public func decorateMessage(
public func decorateMessage(
_ message: String,
basedOnSeverity severity: DiagnosticSeverity
) -> String {
Expand All @@ -57,7 +57,7 @@ extension DiagnosticDecorator where Self == BasicDiagnosticDecorator {
/// - Parameter bufferOutline: The string representation of the source code buffer outline.
///
/// - Returns: The original source code buffer outline.
@_spi(Testing) public func decorateBufferOutline(_ bufferOutline: String) -> String {
public func decorateBufferOutline(_ bufferOutline: String) -> String {
return bufferOutline
}

Expand All @@ -68,7 +68,7 @@ extension DiagnosticDecorator where Self == BasicDiagnosticDecorator {
/// - Returns: A tuple containing:
/// - `highlightedSourceCode`: The original text segment.
/// - `additionalHighlightedLine`: Always nil.
@_spi(Testing) public func decorateHighlight(_ highlight: String) -> (
public func _decorateHighlight(_ highlight: String) -> (
highlightedSourceCode: String, additionalHighlightedLine: String?
) {
return (highlightedSourceCode: highlight, additionalHighlightedLine: nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/// The protocol is designed to be easily customizable. Developers can create their own entities that conform
/// to `DiagnosticDecorator` to implement custom decorating logic. This allows for different visual representations,
/// such as using ANSI colors, underscores, emoji-based or other markers, for diagnostics in source code.
protocol DiagnosticDecorator {
public protocol DiagnosticDecorator {
/// Decorates a diagnostic message based on its severity level.
///
/// Implementations are expected to prepend a severity-specific prefix (e.g., "error: ", "warning: ") to the diagnostic message.
Expand Down Expand Up @@ -59,7 +59,12 @@ protocol DiagnosticDecorator {
/// - Note: The method returns a tuple to offer more flexibility in decorating highlights.
/// This allows for a variety of techniques to be used, such as ANSI codes for color
/// and additional lines for contextual emphasis, which will be combined during the rendering process.
func decorateHighlight(_ highlight: String) -> (highlightedSourceCode: String, additionalHighlightedLine: String?)
///
/// - Warning: This method is currently experimental and subject to change. It is marked with an underscore `_`
/// to indicate that it is not yet considered stable API. The interface, behavior, and existence
/// of this method may change in future releases without warning as we refine its implementation.
/// Please use with caution and avoid relying heavily on it in production code.
func _decorateHighlight(_ highlight: String) -> (highlightedSourceCode: String, additionalHighlightedLine: String?)
}

extension DiagnosticDecorator {
Expand All @@ -71,4 +76,8 @@ extension DiagnosticDecorator {
func decorateDiagnosticMessage(_ diagnosticMessage: DiagnosticMessage) -> String {
decorateMessage(diagnosticMessage.message, basedOnSeverity: diagnosticMessage.severity)
}

func _decorateHighlight(_ highlight: String) -> (highlightedSourceCode: String, additionalHighlightedLine: String?) {
return (highlightedSourceCode: highlight, additionalHighlightedLine: nil)
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftDiagnostics/DiagnosticsFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public struct DiagnosticsFormatter {

// Highlighted source text
let highlightString = String(sourceString[highlightRange])
resultSourceString += diagnosticDecorator.decorateHighlight(highlightString).highlightedSourceCode
resultSourceString += diagnosticDecorator._decorateHighlight(highlightString).highlightedSourceCode

sourceIndex = highlightRange.upperBound
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

@_spi(Testing) import SwiftDiagnostics
import SwiftDiagnostics
import XCTest
import _SwiftSyntaxTestSupport

Expand Down Expand Up @@ -64,14 +64,14 @@ final class ANSIDiagnosticDecoratorTests: XCTestCase {
func testDecorateHighlight() {
let highlightedText = "let x = 10"

let decoratedHighlight = decorator.decorateHighlight(highlightedText)
let decoratedHighlight = decorator._decorateHighlight(highlightedText)

assertStringsEqualWithDiff(decoratedHighlight.highlightedSourceCode, "\u{1B}[4;39mlet x = 10\u{1B}[0;0m")
XCTAssertNil(decoratedHighlight.additionalHighlightedLine)
}

func testDecorateHighlightWithEmptyString() {
let decoratedHighlight = decorator.decorateHighlight("")
let decoratedHighlight = decorator._decorateHighlight("")

assertStringsEqualWithDiff(decoratedHighlight.highlightedSourceCode, "")
XCTAssertNil(decoratedHighlight.additionalHighlightedLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

@_spi(Testing) import SwiftDiagnostics
import SwiftDiagnostics
import XCTest
import _SwiftSyntaxTestSupport

Expand Down Expand Up @@ -51,7 +51,7 @@ final class BasicDiagnosticDecoratorTests: XCTestCase {
func testDecorateHighlight() {
let highlightedText = "let x = 10"

let decoratedHighlight = decorator.decorateHighlight(highlightedText)
let decoratedHighlight = decorator._decorateHighlight(highlightedText)

assertStringsEqualWithDiff(decoratedHighlight.highlightedSourceCode, highlightedText)
XCTAssertNil(decoratedHighlight.additionalHighlightedLine)
Expand Down