From 0c3095a6d3ade81d562acfeefd87cd012739a3da Mon Sep 17 00:00:00 2001 From: andreweades <48011137+andreweades@users.noreply.github.com> Date: Fri, 3 Jan 2020 12:30:46 +0000 Subject: [PATCH 1/4] Added SwiftUITextOutputFormat to output a Text struct --- .../Output/SwiftUITextOutputFormat.swift | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 Sources/Splash/Output/SwiftUITextOutputFormat.swift diff --git a/Sources/Splash/Output/SwiftUITextOutputFormat.swift b/Sources/Splash/Output/SwiftUITextOutputFormat.swift new file mode 100644 index 0000000..4c6ba5b --- /dev/null +++ b/Sources/Splash/Output/SwiftUITextOutputFormat.swift @@ -0,0 +1,79 @@ +// +// SwiftUITextOutputFormat.swift +// +// Created by Andrew Eades on 02/01/2020. +// Copyright © 2020 Andrew Eades. All rights reserved. +// + +import Splash +import SwiftUI + +#if !os(Linux) + +import Foundation + +/// Output format to use to generate an NSAttributedString from the +/// highlighted code. A `Theme` is used to determine what fonts and +/// colors to use for the various tokens. +public struct SwiftUITextOutputFormat: OutputFormat { + public var theme: Theme + + public init(theme: Theme) { + self.theme = theme + } + + public func makeBuilder() -> Builder { + return Builder(theme: theme) + } +} + +public extension SwiftUITextOutputFormat { + struct Builder: OutputBuilder { + private let theme: Theme + private var texts = [Text]() + + fileprivate init(theme: Theme) { + self.theme = theme + } + + public mutating func addToken(_ token: String, ofType type: TokenType) { + let tokenColor = swiftUIColor(forType: type) + + texts.append(Text(token).foregroundColor(tokenColor)) + } + + public mutating func addPlainText(_ plainText: String) { + let tokenColor = SwiftUI.Color(theme.plainTextColor) + + texts.append(Text(plainText).foregroundColor(tokenColor)) + } + + public mutating func addWhitespace(_ whitespace: String) { + let tokenColor = SwiftUI.Color.white + + texts.append(Text(whitespace).foregroundColor(tokenColor)) + } + + public func build() -> Text { + + let highlightedText = texts.reduce(Text(""), +) + + return highlightedText + } + + private func swiftUIColor(forType type: TokenType) -> SwiftUI.Color { + var tokenColor: SwiftUI.Color + + if let color = theme.tokenColors[type] { + tokenColor = SwiftUI.Color(color) + } else { + tokenColor = SwiftUI.Color.white + } + + return tokenColor + } + + } +} + +#endif From ccccc6a7f3976dd82f4d2d486f94a52981ff14a8 Mon Sep 17 00:00:00 2001 From: andreweades <48011137+andreweades@users.noreply.github.com> Date: Fri, 3 Jan 2020 12:47:36 +0000 Subject: [PATCH 2/4] Removed import Splash --- Sources/Splash/Output/SwiftUITextOutputFormat.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/Splash/Output/SwiftUITextOutputFormat.swift b/Sources/Splash/Output/SwiftUITextOutputFormat.swift index 4c6ba5b..b8f929e 100644 --- a/Sources/Splash/Output/SwiftUITextOutputFormat.swift +++ b/Sources/Splash/Output/SwiftUITextOutputFormat.swift @@ -5,7 +5,6 @@ // Copyright © 2020 Andrew Eades. All rights reserved. // -import Splash import SwiftUI #if !os(Linux) From 4efb491edeb2720f2eed80635a9b95c617d33e74 Mon Sep 17 00:00:00 2001 From: andreweades <48011137+andreweades@users.noreply.github.com> Date: Fri, 3 Jan 2020 12:51:00 +0000 Subject: [PATCH 3/4] Conditioned on SwiftUI to pass build --- Sources/Splash/Output/AttributedStringOutputFormat.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Splash/Output/AttributedStringOutputFormat.swift b/Sources/Splash/Output/AttributedStringOutputFormat.swift index 4a54733..603eb80 100644 --- a/Sources/Splash/Output/AttributedStringOutputFormat.swift +++ b/Sources/Splash/Output/AttributedStringOutputFormat.swift @@ -5,6 +5,7 @@ */ #if !os(Linux) +#if canImport(SwiftUI) import Foundation @@ -65,3 +66,4 @@ private extension NSMutableAttributedString { } #endif +#endif From a45f551d628b49d2104f3017cf4fcdd564ef562a Mon Sep 17 00:00:00 2001 From: andreweades <48011137+andreweades@users.noreply.github.com> Date: Fri, 3 Jan 2020 12:59:20 +0000 Subject: [PATCH 4/4] Fixed CI build errors. Sorry. --- Sources/Splash/Output/SwiftUITextOutputFormat.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Output/SwiftUITextOutputFormat.swift b/Sources/Splash/Output/SwiftUITextOutputFormat.swift index b8f929e..ac8844f 100644 --- a/Sources/Splash/Output/SwiftUITextOutputFormat.swift +++ b/Sources/Splash/Output/SwiftUITextOutputFormat.swift @@ -5,9 +5,11 @@ // Copyright © 2020 Andrew Eades. All rights reserved. // -import SwiftUI #if !os(Linux) +#if canImport(SwiftUI) + +import SwiftUI import Foundation @@ -76,3 +78,4 @@ public extension SwiftUITextOutputFormat { } #endif +#endif