From 52aaa1ecff7dfa26ba6ff6b065892490baf022e8 Mon Sep 17 00:00:00 2001 From: Benedikt Terhechte Date: Sun, 7 Jun 2020 09:41:53 +0200 Subject: [PATCH 1/3] Add (failing) test for the newline insertion issue This adds a test which fails because Splash inserts additional whitespace at the beginning of code if the code has empty lines before it starts. --- .../Tests/AttributedStringTests.swift | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Tests/SplashTests/Tests/AttributedStringTests.swift 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 From 63c7a41c8b357e09bfedff133e7773119f807d6f Mon Sep 17 00:00:00 2001 From: Benedikt Terhechte Date: Sun, 7 Jun 2020 09:42:56 +0200 Subject: [PATCH 2/3] Fix the bug where whitespace is inserted. It seems that `\n` is identified as a valid token and then the same substring (`\n`) is both inserted as whitespace and as a token. This causes the insertion of additional whitespace at the beginning of the code. --- Sources/Splash/Syntax/SyntaxHighlighter.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 } From 61a6ca5681b9f5b12ff2a30a0a7b34ef7c428c4c Mon Sep 17 00:00:00 2001 From: Benedikt Terhechte Date: Sun, 7 Jun 2020 09:43:47 +0200 Subject: [PATCH 3/3] Add additional initializer for `Font` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This would allow using a custom monospace font for highlighting. This is especially useful if, say, the code is embedded into some sort of presentation 😊. An assert could be added to make sure the font is monospace, but I feel like that's not required. If somebody wants to display their code in Comic Sans, all the more power to them --- Sources/Splash/Theming/Font.swift | 6 ++++++ 1 file changed, 6 insertions(+) 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 {