diff --git a/Sources/Splash/Syntax/SyntaxHighlighter.swift b/Sources/Splash/Syntax/SyntaxHighlighter.swift index 5e55c36..ddfe67e 100644 --- a/Sources/Splash/Syntax/SyntaxHighlighter.swift +++ b/Sources/Splash/Syntax/SyntaxHighlighter.swift @@ -35,8 +35,9 @@ public struct SyntaxHighlighter { state = (token, type) return } - - builder.addToken(token, ofType: type) + if token != whitespace { + builder.addToken(token, ofType: type) + } builder.addWhitespace(whitespace) state = nil } diff --git a/Sources/Splash/Theming/Font.swift b/Sources/Splash/Theming/Font.swift index a06f5be..e0f69c5 100644 --- a/Sources/Splash/Theming/Font.swift +++ b/Sources/Splash/Theming/Font.swift @@ -31,6 +31,12 @@ public struct Font { resource = .system self.size = size } + + /// Initialize an instance with an existing, custom, font. + public init(_ font: Loaded) { + resource = .preloaded(font) + self.size = Double(font.pointSize) + } } public extension Font { diff --git a/Tests/SplashTests/Tests/AttributedStringTests.swift b/Tests/SplashTests/Tests/AttributedStringTests.swift new file mode 100644 index 0000000..d981b58 --- /dev/null +++ b/Tests/SplashTests/Tests/AttributedStringTests.swift @@ -0,0 +1,58 @@ +#if !os(Linux) + +import Foundation +import XCTest +import Splash + +final class AttributedStringOutputFormatTests: SplashTestCase { + private var highlighter: SyntaxHighlighter! + + override func setUp() { + super.setUp() + highlighter = SyntaxHighlighter(format: AttributedStringOutputFormat(theme: Theme.presentation(withFont: .init(size: 15)))) + } + + func testBasicAttributedStringOutputMatchesInput() { + let input = """ + public struct Test: SomeProtocol { + func hello() -> Int { return 7 } + } + """ + let output = highlighter.highlight(input) + XCTAssertEqual(input, output.string) + } + + func testNewlinesAttributedStringOutputMatchesInput() { + let input = """ + + typealias a = b + typealias b = c + + print("hello") + """ + let output = highlighter.highlight(input) + XCTAssertEqual(input, output.string) + } + + func testInnerNewlinesAttributedStringOutputMatchesInput() { + let input = """ + typealias a = b + typealias b = c + + print("hello") + + print("hello") + print("hello") + + + print("hello") + """ + let output = highlighter.highlight(input) + XCTAssertEqual(input, output.string) + } + + func testAllTestsRunOnLinux() { + } +} + +#endif