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
200 lines (185 loc) · 7.04 KB
/
AsyncCodeBlock.swift
File metadata and controls
200 lines (185 loc) · 7.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
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 ellipsisPadding: CGFloat = 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 {
Image(systemName: "ellipsis")
.renderingMode(.template)
.foregroundColor(foregroundTextColor)
.padding(.horizontal, ellipsisPadding)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color.gray.opacity(isExpanded ? 0.1 : 0.4))
.frame(height: fontHeight * 0.75)
)
.popover(isPresented: $isHovering) {
Text(hintText)
.font(.body)
.padding(8)
.fixedSize()
}
.task {
isHovering = !completionHintShown
completionHintShown = true
}
}
}
.background(currentLineBackgroundColor ?? backgroundColor)
.padding(.leading, firstLineIndent)
.frame(minHeight: lineHeight)
.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)
}
}
}
}