forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncCodeBlock.swift
More file actions
270 lines (249 loc) · 9.04 KB
/
AsyncCodeBlock.swift
File metadata and controls
270 lines (249 loc) · 9.04 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import DebounceFunction
import Foundation
import Perception
import SwiftUI
public struct AsyncCodeBlock: View {
private struct Constants {
static let paddingLeading = 5.0
static let paddingBottom = 10.0
static let paddingTrailing = 10.0
}
@Environment(\.colorScheme) var colorScheme
@Binding var isExpanded: Bool
@State private var isHovering: Bool = false
@AppStorage(\.completionHintShown) var completionHintShown
let code: String
let language: String
let startLineIndex: Int
let scenario: String
let firstLineIndent: Double
let lineHeight: Double
let font: NSFont
let proposedForegroundColor: Color?
let proposedBackgroundColor: Color?
let currentLineBackgroundColor: Color?
let dimmedCharacterCount: Int
let droppingLeadingSpaces: Bool
let isPanelDisplayed: Bool
public init(
code: String,
language: String,
startLineIndex: Int,
scenario: String,
firstLineIndent: Double,
lineHeight: Double,
font: NSFont,
droppingLeadingSpaces: Bool,
proposedForegroundColor: Color?,
proposedBackgroundColor: Color?,
currentLineBackgroundColor: Color?,
dimmedCharacterCount: Int,
isExpanded: Binding<Bool>,
isPanelDisplayed: Bool
) {
self.code = code
self.startLineIndex = startLineIndex
self.language = language
self.scenario = scenario
self.firstLineIndent = firstLineIndent
self.lineHeight = lineHeight
self.font = font
self.proposedForegroundColor = proposedForegroundColor
self.proposedBackgroundColor = proposedBackgroundColor
self.currentLineBackgroundColor = currentLineBackgroundColor
self.dimmedCharacterCount = dimmedCharacterCount
self.droppingLeadingSpaces = droppingLeadingSpaces
self._isExpanded = isExpanded
self.isPanelDisplayed = isPanelDisplayed
}
var foregroundColor: Color {
if let proposedForegroundColor = proposedForegroundColor {
return proposedForegroundColor
}
return colorScheme == .light ? .black.opacity(0.85) : .white.opacity(0.85)
}
var foregroundTextColor: Color {
return foregroundColor.opacity(0.6)
}
var backgroundColor: Color {
if let proposedBackgroundColor = proposedBackgroundColor {
return proposedBackgroundColor
}
return colorScheme == .dark ? Color(red: 0.1216, green: 0.1216, blue: 0.1412) : .white
}
var fontHeight: Double {
(font.ascender + abs(font.descender)).rounded(.down)
}
var lineSpacing: Double {
lineHeight - fontHeight
}
var expandedIndent: Double {
let lines = code.splitByNewLine()
guard let firstLine = lines.first else { return 0 }
let existing = String(firstLine.prefix(dimmedCharacterCount))
let attr = NSAttributedString(string: existing, attributes: [.font: font])
return firstLineIndent - attr.size().width
}
var hintText: String {
if isExpanded {
return "Press ⌥⇥ to accept full suggestion"
}
return "Hold ⌥ for full suggestion"
}
@ScaledMetric var keyPadding: Double = 3.0
@ViewBuilder
func keyBackground(content: () -> some View) -> some View {
content()
.padding(.horizontal, keyPadding)
.background(
RoundedRectangle(cornerRadius: 2)
.stroke(foregroundColor, lineWidth: 1)
.foregroundColor(.clear)
.frame(
minWidth: fontHeight,
minHeight: fontHeight,
maxHeight: fontHeight
)
)
}
@ViewBuilder
var optionKey: some View {
keyBackground {
Image(systemName: "option")
.resizable()
.renderingMode(.template)
.scaledToFit()
.frame(height: font.capHeight)
}
}
@ViewBuilder
var popoverContent: some View {
HStack {
if isExpanded {
Text("Press")
optionKey
keyBackground {
Text("tab")
.font(.init(font))
}
Text("to accept full suggestion")
} else {
Text("Hold")
optionKey
Text("for full suggestion")
}
}
.padding(8)
.font(.body)
.fixedSize()
}
@ViewBuilder
func lineBackgroundShape(_ multiLine: Bool) -> some View {
let color = currentLineBackgroundColor ?? backgroundColor
switch multiLine {
case true: HalfCapsule().fill(color)
case false: Rectangle().fill(color)
}
}
@ScaledMetric var iconPadding: CGFloat = 9.0
@ScaledMetric var iconSpacing: CGFloat = 6.0
@ScaledMetric var optionPadding: CGFloat = 0.5
@ViewBuilder
var contentView: some View {
let lines = code.splitByNewLine()
if let firstLine = lines.first {
let firstLineTrimmed = firstLine
.dropFirst(dimmedCharacterCount)
HStack() {
HStack(alignment: .center, spacing: 10) {
Text(firstLineTrimmed)
.foregroundColor(foregroundTextColor)
.lineSpacing(lineSpacing) // This only has effect if a line wraps
if lines.count > 1 {
HStack(spacing: iconSpacing) {
Image("CopilotLogo")
.resizable()
.renderingMode(.template)
.scaledToFit()
Image(systemName: "option")
.resizable()
.renderingMode(.template)
.scaledToFit()
.padding(.vertical, optionPadding)
}
.frame(height: lineHeight * 0.7)
.padding(.horizontal, iconPadding)
.background(
Capsule()
.fill(foregroundColor.opacity(isExpanded ? 0.1 : 0.2))
.frame(height: lineHeight)
)
.frame(height: lineHeight) // Moves popover attachment
.popover(isPresented: $isHovering) {
popoverContent
}
.task {
isHovering = !completionHintShown
completionHintShown = true
}
}
}
.frame(height: lineHeight)
.background(lineBackgroundShape(lines.count > 1))
.padding(.leading, firstLineIndent)
.onHover { hovering in
guard hovering != isHovering else { return }
withAnimation {
isHovering = hovering
}
}
Spacer()
}
}
if isExpanded && lines.count > 1 {
HStack() {
CustomScrollView {
VStack(alignment: .leading, spacing: 0) {
ForEach(Array(lines.dropFirst()), id: \.self) { line in
HStack(alignment: .firstTextBaseline, spacing: 4) {
Text(line)
.foregroundColor(foregroundTextColor)
.lineSpacing(lineSpacing)
}
.frame(minHeight: lineHeight)
}
}
}
.padding(EdgeInsets(
top: 0,
leading: Constants.paddingLeading,
bottom: Constants.paddingBottom,
trailing: Constants.paddingTrailing
))
.background(backgroundColor)
.cornerRadius(10)
.overlay(RoundedRectangle(cornerRadius: 10).stroke(foregroundColor.opacity(0.2), lineWidth: 1)) // border
.shadow(color: Color.black.opacity(0.2), radius: 8.0, x: 1, y: 1)
.onHover { hovering in
guard hovering != isHovering else { return }
withAnimation {
isHovering = hovering
}
}
Spacer()
}
.padding(.leading, expandedIndent - Constants.paddingLeading)
}
}
public var body: some View {
if isPanelDisplayed {
WithPerceptionTracking {
VStack(spacing: 0) {
contentView
}
.font(.init(font))
.background(Color.clear)
}
}
}
}