-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathWorkingSetView.swift
More file actions
192 lines (167 loc) · 7.01 KB
/
WorkingSetView.swift
File metadata and controls
192 lines (167 loc) · 7.01 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
import SwiftUI
import ChatService
import Perception
import ComposableArchitecture
import GitHubCopilotService
import JSONRPC
import SharedUIComponents
import OrderedCollections
import ConversationServiceProvider
struct WorkingSetView: View {
let chat: StoreOf<Chat>
private let r: Double = 8
var body: some View {
WithPerceptionTracking {
VStack(alignment: .leading, spacing: 4) {
WorkingSetHeader(chat: chat)
ForEach(chat.fileEditMap.elements, id: \.key.path) { element in
FileEditView(chat: chat, fileEdit: element.value)
}
}
.padding(.vertical, 8)
.padding(.horizontal, 16)
.frame(maxWidth: .infinity)
.background(
RoundedCorners(tl: r, tr: r, bl: 0, br: 0)
.fill(.ultraThickMaterial)
)
.overlay(
RoundedCorners(tl: r, tr: r, bl: 0, br: 0)
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
)
}
}
}
struct WorkingSetHeader: View {
let chat: StoreOf<Chat>
func getTitle() -> String {
return chat.fileEditMap.count > 1 ? "\(chat.fileEditMap.count) files changed" : "1 file changed"
}
var body: some View {
WithPerceptionTracking {
HStack {
Text(getTitle())
.foregroundColor(.secondary)
.font(.system(size: 12))
Spacer()
if chat.fileEditMap.contains(where: {_, fileEdit in
return fileEdit.status == .none
}) {
/// Undo all edits
Button("Undo") {
chat.send(.undoEdits(fileURLs: chat.fileEditMap.values.map { $0.fileURL }))
}
.help("Undo All Edits")
Button("Keep") {
chat.send(.keepEdits(fileURLs: chat.fileEditMap.values.map { $0.fileURL }))
}
.buttonStyle(.borderedProminent)
.help("Keep All Edits")
} else {
Button("Done") {
chat.send(.resetEdits)
}
.help("Done")
}
}
}
}
}
struct FileEditView: View {
let chat: StoreOf<Chat>
let fileEdit: FileEdit
@State private var isHovering = false
var body: some View {
ZStack(alignment: .trailing) {
HStack(spacing: 4) {
Button(action: {
chat.send(.openDiffViewWindow(fileURL: fileEdit.fileURL))
}) {
drawFileIcon(fileEdit.fileURL)
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
.foregroundColor(.secondary)
Text(fileEdit.fileURL.lastPathComponent)
.bold()
.font(.system(size: 14))
}
.buttonStyle(HoverButtonStyle())
Spacer()
}
if isHovering {
HStack(spacing: 4) {
Spacer()
if fileEdit.status == .none {
Button {
chat.send(.undoEdits(fileURLs: [fileEdit.fileURL]))
} label: {
Image(systemName: "arrow.uturn.backward")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.foregroundColor(.secondary)
}
.buttonStyle(HoverButtonStyle(padding: 0))
.help("Undo")
Button {
chat.send(.keepEdits(fileURLs: [fileEdit.fileURL]))
} label: {
Image(systemName: "checkmark")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.foregroundColor(.secondary)
}
.buttonStyle(HoverButtonStyle(padding: 0))
.help("Keep")
Button {
chat.send(.openDiffViewWindow(fileURL: fileEdit.fileURL))
} label: {
Image(systemName: "pencil.and.list.clipboard")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.foregroundColor(.secondary)
}
.buttonStyle(HoverButtonStyle(padding: 0))
.help("Open changes in Diff Editor")
}
Button {
/// User directly close this edit. undo and remove it
chat.send(.discardFileEdits(fileURLs: [fileEdit.fileURL]))
} label: {
Image(systemName: "xmark")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 14, height: 14)
.foregroundColor(.secondary)
}
.buttonStyle(HoverButtonStyle(padding: 0))
.help("Remove file")
}
}
}
.onHover { hovering in
isHovering = hovering
}
}
}
struct WorkingSetView_Previews: PreviewProvider {
static let fileEditMap: OrderedDictionary<URL, FileEdit> = [
URL(fileURLWithPath: "file:///f1.swift"): FileEdit(fileURL: URL(fileURLWithPath: "file:///f1.swift"), originalContent: "single line", modifiedContent: "single line 1", toolName: ToolName.insertEditIntoFile),
URL(fileURLWithPath: "file:///f2.swift"): FileEdit(fileURL: URL(fileURLWithPath: "file:///f2.swift"), originalContent: "multi \n line \n end", modifiedContent: "another \n mut \n li \n", status: .kept, toolName: ToolName.insertEditIntoFile)
]
static var previews: some View {
WorkingSetView(
chat: .init(
initialState: .init(
history: ChatPanel_Preview.history,
isReceivingMessage: true,
fileEditMap: fileEditMap
),
reducer: { Chat(service: ChatService.service(for: ChatPanel_Preview.chatTabInfo)) }
)
)
}
}