Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 397764c

Browse files
authored
Highlight CodeView text on a background queue (#2322)
1 parent 970ac9d commit 397764c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Classes/Views/CodeView.swift

+11-7
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ final class CodeView: UITextView {
3232

3333
// MARK: Public API
3434
func set(code: String, language: String? = nil) {
35-
if let language = language,
36-
let highlighted = GithubHighlighting.highlight(code, as: language) {
37-
set(attributedCode: highlighted)
38-
} else {
39-
// Automatic language detection
40-
if let highlighted = GithubHighlighting.highlight(code) {
41-
set(attributedCode: highlighted)
35+
DispatchQueue.global().async {
36+
let maybeHighlighted: NSAttributedString?
37+
if let language = language {
38+
maybeHighlighted = GithubHighlighting.highlight(code, as: language)
39+
} else {
40+
// Automatic language detection
41+
maybeHighlighted = GithubHighlighting.highlight(code)
42+
}
43+
guard let highlighted = maybeHighlighted else { return }
44+
DispatchQueue.main.async { [weak self] in
45+
self?.set(attributedCode: highlighted)
4246
}
4347
}
4448
}

0 commit comments

Comments
 (0)