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

Highlight CodeView text on a background queue #2322

Merged
merged 1 commit into from
Oct 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Classes/Views/CodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ final class CodeView: UITextView {

// MARK: Public API
func set(code: String, language: String? = nil) {
if let language = language,
let highlighted = GithubHighlighting.highlight(code, as: language) {
set(attributedCode: highlighted)
} else {
// Automatic language detection
if let highlighted = GithubHighlighting.highlight(code) {
set(attributedCode: highlighted)
DispatchQueue.global().async {
let maybeHighlighted: NSAttributedString?
if let language = language {
maybeHighlighted = GithubHighlighting.highlight(code, as: language)
} else {
// Automatic language detection
maybeHighlighted = GithubHighlighting.highlight(code)
}
guard let highlighted = maybeHighlighted else { return }
DispatchQueue.main.async { [weak self] in
self?.set(attributedCode: highlighted)
}
}
}
Expand Down