diff --git a/Sources/TextBuilder/TextBuilder.swift b/Sources/TextBuilder/TextBuilder.swift index a178a95..ee6b690 100644 --- a/Sources/TextBuilder/TextBuilder.swift +++ b/Sources/TextBuilder/TextBuilder.swift @@ -19,6 +19,52 @@ import SwiftUI /// Text("amet, consectetur") /// } /// +#if compiler(>=5.7) +@resultBuilder +public struct TextBuilder { + public static func buildPartialBlock(first: Text) -> Text { + first + } + + public static func buildPartialBlock(accumulated: Text, next: Text) -> Text { + if next.isNone { + return accumulated + } else if Separator.separator.isEmpty { + return accumulated + next + } else { + return accumulated + Text(Separator.separator) + next + } + } + + public static func buildArray(_ components: [Text]) -> Text { + components.joined(separator: Text(Separator.separator)) + } + + public static func buildEither(first component: Text) -> Text { + component + } + + public static func buildEither(second component: Text) -> Text { + component + } + + public static func buildExpression(_ string: some StringProtocol) -> Text { + Text(string) + } + + public static func buildExpression(_ component: Text) -> Text { + component + } + + public static func buildLimitedAvailability(_ component: Text) -> Text { + component + } + + public static func buildOptional(_ component: Text?) -> Text { + component ?? Text.none + } +} +#else @resultBuilder public struct TextBuilder { public static func buildArray(_ texts: [[Text]]) -> [Text] { @@ -57,3 +103,4 @@ public struct TextBuilder { texts.joined(separator: Text(Separator.separator)) } } +#endif diff --git a/Tests/TextBuilderTests/TextBuilderTests.swift b/Tests/TextBuilderTests/TextBuilderTests.swift index 2e1a311..9bf4de4 100644 --- a/Tests/TextBuilderTests/TextBuilderTests.swift +++ b/Tests/TextBuilderTests/TextBuilderTests.swift @@ -55,6 +55,24 @@ final class TextBuilderTests: XCTestCase { } func testComplexTextBuilder() { + #if compiler(>=5.7) + XCTAssertNoDifference( + complexTextBuilderText(), + Text(verbatim: "Lorem").underline().foregroundColor(.blue) + + Text(verbatim: " ") + + Text(verbatim: "sit").bold() + + Text(verbatim: " ") + + Text(verbatim: "amet, consectetur") + + Text(verbatim: " ") + + ( + Text(verbatim: "1") + + Text(verbatim: " ") + + Text(verbatim: "2") + + Text(verbatim: " ") + + Text(verbatim: "3") + ) + ) + #else XCTAssertNoDifference( complexTextBuilderText(), Text(verbatim: "Lorem").underline().foregroundColor(.blue) + @@ -69,6 +87,7 @@ final class TextBuilderTests: XCTestCase { Text(verbatim: " ") + Text(verbatim: "3") ) + #endif } }