Skip to content

Commit c23ed8e

Browse files
author
aterzhang
committed
Fix Demo Crash in Syntax Highlighting with long code
1 parent a9c7615 commit c23ed8e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

Examples/Demo/Demo/SyntaxHighlighter/TextOutputFormat.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct TextOutputFormat: OutputFormat {
1616
extension TextOutputFormat {
1717
struct Builder: OutputBuilder {
1818
private let theme: Theme
19-
private var accumulatedText: [Text]
19+
private var accumulatedText: [AttributedString]
2020

2121
fileprivate init(theme: Theme) {
2222
self.theme = theme
@@ -25,21 +25,24 @@ extension TextOutputFormat {
2525

2626
mutating func addToken(_ token: String, ofType type: TokenType) {
2727
let color = self.theme.tokenColors[type] ?? self.theme.plainTextColor
28-
self.accumulatedText.append(Text(token).foregroundColor(.init(uiColor: color)))
28+
var aster = AttributedString(token)
29+
aster.foregroundColor = .init(uiColor: color)
30+
self.accumulatedText.append(aster)
2931
}
3032

3133
mutating func addPlainText(_ text: String) {
32-
self.accumulatedText.append(
33-
Text(text).foregroundColor(.init(uiColor: self.theme.plainTextColor))
34-
)
34+
var aster = AttributedString(text)
35+
aster.foregroundColor = .init(uiColor: self.theme.plainTextColor)
36+
self.accumulatedText.append(aster)
3537
}
3638

3739
mutating func addWhitespace(_ whitespace: String) {
38-
self.accumulatedText.append(Text(whitespace))
40+
self.accumulatedText.append(AttributedString(whitespace))
3941
}
4042

4143
func build() -> Text {
42-
self.accumulatedText.reduce(Text(""), +)
44+
let text = self.accumulatedText.reduce(AttributedString(""), +)
45+
return Text(text)
4346
}
4447
}
4548
}

0 commit comments

Comments
 (0)