forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBlockHighlighter.swift
More file actions
112 lines (102 loc) · 3.84 KB
/
CodeBlockHighlighter.swift
File metadata and controls
112 lines (102 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import Combine
import DebounceFunction
import Foundation
import MarkdownUI
import SharedUIComponents
import SwiftUI
/// Use this instead of the built in ``CodeBlockView`` to highlight code blocks asynchronously,
/// so that the UI doesn't freeze when rendering large code blocks.
struct AsyncCodeBlockView: View {
class Storage: ObservableObject {
static let queue = DispatchQueue(
label: "chat-code-block-highlight",
qos: .userInteractive,
attributes: .concurrent
)
@Published var highlighted: AttributedString?
var debounceFunction: DebounceFunction<AsyncCodeBlockView>?
private var highlightTask: Task<Void, Error>?
init() {
self.debounceFunction = .init(duration: 0.5, block: { [weak self] view in
self?.highlight(for: view)
})
}
func highlight(debounce: Bool, for view: AsyncCodeBlockView) {
if debounce {
Task { await debounceFunction?(view) }
} else {
highlight(for: view)
}
}
func highlight(for view: AsyncCodeBlockView) {
highlightTask?.cancel()
let content = view.content
let language = view.fenceInfo ?? ""
let brightMode = view.colorScheme != .dark
let font = view.font
highlightTask = Task {
let string = await withUnsafeContinuation { continuation in
Self.queue.async {
let content = highlightedCodeBlock(
code: content,
language: language,
scenario: "chat",
brightMode: brightMode,
font: font
)
continuation.resume(returning: AttributedString(content))
}
}
try Task.checkCancellation()
await MainActor.run {
self.highlighted = string
}
}
}
}
let fenceInfo: String?
let content: String
let font: NSFont
@Environment(\.colorScheme) var colorScheme
@StateObject var storage = Storage()
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
init(fenceInfo: String?, content: String, font: NSFont) {
self.fenceInfo = fenceInfo
self.content = content.hasSuffix("\n") ? String(content.dropLast()) : content
self.font = font
}
var body: some View {
Group {
if let highlighted = storage.highlighted {
Text(highlighted)
} else {
Text(content).font(.init(font))
}
}
.onAppear {
storage.highlight(debounce: false, for: self)
}
.onChange(of: colorScheme) { _ in
storage.highlight(debounce: false, for: self)
}
.onChange(of: syncCodeHighlightTheme) { _ in
storage.highlight(debounce: true, for: self)
}
.onChange(of: codeForegroundColorLight) { _ in
storage.highlight(debounce: true, for: self)
}
.onChange(of: codeBackgroundColorLight) { _ in
storage.highlight(debounce: true, for: self)
}
.onChange(of: codeForegroundColorDark) { _ in
storage.highlight(debounce: true, for: self)
}
.onChange(of: codeBackgroundColorDark) { _ in
storage.highlight(debounce: true, for: self)
}
}
}