|
| 1 | +import Combine |
| 2 | +import DebounceFunction |
1 | 3 | import Foundation |
2 | 4 | import MarkdownUI |
3 | 5 | import SharedUIComponents |
4 | 6 | import SwiftUI |
5 | 7 |
|
6 | | -final class CodeBlockHighlighterCacheController { |
7 | | - private var cache: [String: AttributedString] = [:] |
8 | | - |
9 | | - func get(_ key: String) -> AttributedString? { |
10 | | - cache[key] |
11 | | - } |
12 | | - |
13 | | - func set(_ key: String, _ value: AttributedString) { |
14 | | - cache[key] = value |
15 | | - } |
16 | | -} |
| 8 | +/// Use this instead of the built in ``CodeBlockView`` to highlight code blocks asynchronously, |
| 9 | +/// so that the UI doesn't freeze when rendering large code blocks. |
| 10 | +struct AsyncCodeBlockView: View { |
| 11 | + class Storage: ObservableObject { |
| 12 | + static let queue = DispatchQueue( |
| 13 | + label: "chat-code-block-highlight", |
| 14 | + qos: .userInteractive |
| 15 | + ) |
17 | 16 |
|
18 | | -struct CodeHighlightCacheEnvironmentKey: EnvironmentKey { |
19 | | - static var defaultValue: CodeBlockHighlighterCacheController = .init() |
20 | | -} |
| 17 | + @Published var highlighted: AttributedString? |
| 18 | + var debounceFunction: DebounceFunction<AsyncCodeBlockView>? |
| 19 | + |
| 20 | + init() { |
| 21 | + self.debounceFunction = .init(duration: 0.5, block: { [weak self] view in |
| 22 | + self?.highlight(for: view) |
| 23 | + }) |
| 24 | + } |
21 | 25 |
|
22 | | -extension EnvironmentValues { |
23 | | - var codeHighlightCacheController: CodeBlockHighlighterCacheController { |
24 | | - get { self[CodeHighlightCacheEnvironmentKey.self] } |
25 | | - set { self[CodeHighlightCacheEnvironmentKey.self] = newValue } |
| 26 | + func highlight(debounce: Bool, for view: AsyncCodeBlockView) { |
| 27 | + if debounce { |
| 28 | + Task { await debounceFunction?(view) } |
| 29 | + } else { |
| 30 | + highlight(for: view) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + func highlight(for view: AsyncCodeBlockView) { |
| 35 | + let content = view.content |
| 36 | + let language = view.fenceInfo ?? "" |
| 37 | + let brightMode = view.colorScheme != .dark |
| 38 | + let font = view.font |
| 39 | + Self.queue.async { |
| 40 | + let content = highlightedCodeBlock( |
| 41 | + code: content, |
| 42 | + language: language, |
| 43 | + scenario: "chat", |
| 44 | + brightMode: brightMode, |
| 45 | + font: font |
| 46 | + ) |
| 47 | + let string = AttributedString(content) |
| 48 | + DispatchQueue.main.async { |
| 49 | + self.highlighted = string |
| 50 | + } |
| 51 | + } |
| 52 | + } |
26 | 53 | } |
27 | | -} |
28 | 54 |
|
29 | | -struct ChatCodeSyntaxHighlighter: CodeSyntaxHighlighter { |
30 | | - let brightMode: Bool |
| 55 | + let fenceInfo: String? |
| 56 | + let content: String |
31 | 57 | let font: NSFont |
32 | | - let colorChange: Color? |
33 | | - var cacheController: CodeBlockHighlighterCacheController |
34 | 58 |
|
35 | | - init( |
36 | | - brightMode: Bool, |
37 | | - font: NSFont, |
38 | | - colorChange: Color?, |
39 | | - cacheController: CodeBlockHighlighterCacheController |
40 | | - ) { |
41 | | - self.brightMode = brightMode |
| 59 | + @Environment(\.colorScheme) var colorScheme |
| 60 | + @StateObject var storage = Storage() |
| 61 | + @AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme |
| 62 | + @AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight |
| 63 | + @AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight |
| 64 | + @AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark |
| 65 | + @AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark |
| 66 | + |
| 67 | + init(fenceInfo: String?, content: String, font: NSFont) { |
| 68 | + self.fenceInfo = fenceInfo |
| 69 | + self.content = content.hasSuffix("\n") ? String(content.dropLast()) : content |
42 | 70 | self.font = font |
43 | | - self.colorChange = colorChange |
44 | | - self.cacheController = cacheController |
45 | 71 | } |
46 | 72 |
|
47 | | - func highlightCode(_ code: String, language: String?) -> Text { |
48 | | - let key = "\(language ?? "unknown") - \(code)" |
49 | | - if let text = cacheController.get(key) { |
50 | | - return Text(text) |
| 73 | + var body: some View { |
| 74 | + Group { |
| 75 | + if let highlighted = storage.highlighted { |
| 76 | + Text(highlighted) |
| 77 | + } else { |
| 78 | + Text(content).font(.init(font)) |
| 79 | + } |
51 | 80 | } |
52 | | - |
53 | | - let content = highlightedCodeBlock( |
54 | | - code: code, |
55 | | - language: language ?? "", |
56 | | - scenario: "chat", |
57 | | - brightMode: brightMode, |
58 | | - font: font |
59 | | - ) |
60 | | - let string = AttributedString(content) |
61 | | - Task { @MainActor in |
62 | | - cacheController.set(key, string) |
| 81 | + .onAppear { |
| 82 | + storage.highlight(debounce: false, for: self) |
| 83 | + } |
| 84 | + .onChange(of: colorScheme) { _ in |
| 85 | + storage.highlight(debounce: false, for: self) |
| 86 | + } |
| 87 | + .onChange(of: syncCodeHighlightTheme) { _ in |
| 88 | + storage.highlight(debounce: true, for: self) |
| 89 | + } |
| 90 | + .onChange(of: codeForegroundColorLight) { _ in |
| 91 | + storage.highlight(debounce: true, for: self) |
| 92 | + } |
| 93 | + .onChange(of: codeBackgroundColorLight) { _ in |
| 94 | + storage.highlight(debounce: true, for: self) |
| 95 | + } |
| 96 | + .onChange(of: codeForegroundColorDark) { _ in |
| 97 | + storage.highlight(debounce: true, for: self) |
| 98 | + } |
| 99 | + .onChange(of: codeBackgroundColorDark) { _ in |
| 100 | + storage.highlight(debounce: true, for: self) |
63 | 101 | } |
64 | | - return Text(string) |
65 | 102 | } |
66 | 103 | } |
67 | 104 |
|
0 commit comments