forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeReviewPanelView.swift
More file actions
448 lines (389 loc) · 13.7 KB
/
CodeReviewPanelView.swift
File metadata and controls
448 lines (389 loc) · 13.7 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import SwiftUI
import Combine
import XcodeInspector
import ComposableArchitecture
import ConversationServiceProvider
import LanguageServerProtocol
import ChatService
import SharedUIComponents
import ConversationTab
private typealias CodeReviewPanelViewStore = ViewStore<ViewState, CodeReviewPanelFeature.Action>
private struct ViewState: Equatable {
let reviewComments: [ReviewComment]
let currentSelectedComment: ReviewComment?
let currentIndex: Int
let operatedCommentIds: Set<String>
var hasNextComment: Bool
var hasPreviousComment: Bool
var commentsCount: Int { reviewComments.count }
init(state: CodeReviewPanelFeature.State) {
self.reviewComments = state.currentDocumentReview?.comments ?? []
self.currentSelectedComment = state.currentSelectedComment
self.currentIndex = state.currentIndex
self.operatedCommentIds = state.operatedCommentIds
self.hasNextComment = state.hasNextComment
self.hasPreviousComment = state.hasPreviousComment
}
}
struct CodeReviewPanelView: View {
let store: StoreOf<CodeReviewPanelFeature>
var body: some View {
WithViewStore(self.store, observe: ViewState.init) { viewStore in
WithPerceptionTracking {
VStack(spacing: 0) {
VStack(spacing: 0) {
HeaderView(viewStore: viewStore)
.padding(.bottom, 4)
Divider()
ContentView(
comment: viewStore.currentSelectedComment,
viewStore: viewStore
)
.padding(.top, 16)
}
.padding(.vertical, 10)
.padding(.horizontal, 20)
.frame(maxWidth: .infinity, maxHeight: Style.codeReviewPanelHeight, alignment: .top)
.fixedSize(horizontal: false, vertical: true)
.xcodeStyleFrame(cornerRadius: 10)
.onAppear { viewStore.send(.appear) }
Spacer()
}
}
}
}
}
// MARK: - Header View
private struct HeaderView: View {
let viewStore: CodeReviewPanelViewStore
var body: some View {
HStack(alignment: .center, spacing: 8) {
ZStack {
Circle()
.stroke(Color(nsColor: .separatorColor), lineWidth: 1)
.frame(width: 24, height: 24)
Image("CopilotLogo")
.resizable()
.renderingMode(.template)
.scaledToFit()
.frame(width: 12, height: 12)
}
Text("Code Review Comment")
.font(.system(size: 13, weight: .semibold))
.lineLimit(1)
if viewStore.commentsCount > 0 {
Text("(\(viewStore.currentIndex + 1) of \(viewStore.commentsCount))")
.font(.caption)
.foregroundColor(.secondary)
.lineLimit(1)
}
Spacer()
NavigationControls(viewStore: viewStore)
}
.fixedSize(horizontal: false, vertical: true)
}
}
// MARK: - Navigation Controls
private struct NavigationControls: View {
let viewStore: CodeReviewPanelViewStore
var body: some View {
HStack(spacing: 4) {
if viewStore.hasPreviousComment {
Button(action: {
viewStore.send(.previous)
}) {
Image(systemName: "arrow.up")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 13, height: 13)
}
.buttonStyle(HoverButtonStyle())
.buttonStyle(PlainButtonStyle())
.help("Previous")
}
if viewStore.hasNextComment {
Button(action: {
viewStore.send(.next)
}) {
Image(systemName: "arrow.down")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 13, height: 13)
}
.buttonStyle(HoverButtonStyle())
.buttonStyle(PlainButtonStyle())
.help("Next")
}
Button(action: {
if let id = viewStore.currentSelectedComment?.id {
viewStore.send(.close(commentId: id))
}
}) {
Image(systemName: "xmark")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 13, height: 13)
}
.buttonStyle(HoverButtonStyle())
.buttonStyle(PlainButtonStyle())
.help("Close")
}
}
}
// MARK: - Content View
private struct ContentView: View {
let comment: ReviewComment?
let viewStore: CodeReviewPanelViewStore
var body: some View {
if let comment = comment {
CommentDetailView(comment: comment, viewStore: viewStore)
} else {
EmptyView()
}
}
}
// MARK: - Comment Detail View
private struct CommentDetailView: View {
let comment: ReviewComment
let viewStore: CodeReviewPanelViewStore
@AppStorage(\.chatFontSize) var chatFontSize
var lineInfoContent: String {
let displayStartLine = comment.range.start.line + 1
let displayEndLine = comment.range.end.line + 1
if displayStartLine == displayEndLine {
return "Line \(displayStartLine)"
} else {
return "Line \(displayStartLine)-\(displayEndLine)"
}
}
var lineInfoView: some View {
Text(lineInfoContent)
.font(.system(size: chatFontSize))
}
var kindView: some View {
Text(comment.kind)
.font(.system(size: chatFontSize))
.padding(.horizontal, 6)
.frame(maxHeight: 20)
.background(
RoundedRectangle(cornerRadius: 4)
.foregroundColor(.hoverColor)
)
}
var messageView: some View {
ScrollView {
ThemedMarkdownText(
text: comment.message,
context: .init(supportInsert: false)
)
}
}
var dismissButton: some View {
Button(action: {
viewStore.send(.dismiss(commentId: comment.id))
}) {
Text("Dismiss")
}
.buttonStyle(.bordered)
.foregroundColor(.primary)
.help("Dismiss")
}
var acceptButton: some View {
Button(action: {
viewStore.send(.accept(commentId: comment.id))
}) {
Text("Accept")
}
.buttonStyle(.borderedProminent)
.help("Accept")
}
private var fileURL: URL? {
URL(string: comment.uri)
}
var fileNameView: some View {
HStack(spacing: 8) {
drawFileIcon(fileURL)
.resizable()
.scaledToFit()
.frame(width: 16, height: 16)
Text(fileURL?.lastPathComponent ?? comment.uri)
.fontWeight(.semibold)
.lineLimit(1)
.truncationMode(.middle)
}
}
var body: some View {
VStack(alignment: .leading, spacing: 16) {
// Compact header with range info and badges in one line
HStack(alignment: .center, spacing: 8) {
fileNameView
Spacer()
lineInfoView
kindView
}
messageView
.frame(maxHeight: 100)
.fixedSize(horizontal: false, vertical: true)
// Add suggested change view if suggestion exists
if let suggestion = comment.suggestion,
!suggestion.isEmpty,
let fileUrl = URL(string: comment.uri),
let content = try? String(contentsOf: fileUrl)
{
SuggestedChangeView(
suggestion: suggestion,
content: content,
range: comment.range,
chatFontSize: chatFontSize
)
if !viewStore.operatedCommentIds.contains(comment.id) {
HStack(spacing: 9) {
Spacer()
dismissButton
acceptButton
}
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
// MARK: - Suggested Change View
private struct SuggestedChangeView: View {
let suggestion: String
let content: String
let range: LSPRange
let chatFontSize: CGFloat
struct DiffLine {
let content: String
let lineNumber: Int
let type: DiffLineType
}
enum DiffLineType {
case removed
case added
}
var diffLines: [DiffLine] {
var lines: [DiffLine] = []
// Add removed lines
let contentLines = content.components(separatedBy: .newlines)
if range.start.line >= 0 && range.end.line < contentLines.count {
let removedLines = Array(contentLines[range.start.line...range.end.line])
for (index, lineContent) in removedLines.enumerated() {
lines.append(DiffLine(
content: lineContent,
lineNumber: range.start.line + index + 1,
type: .removed
))
}
}
// Add suggested lines
let suggestionLines = suggestion.components(separatedBy: .newlines)
for (index, lineContent) in suggestionLines.enumerated() {
lines.append(DiffLine(
content: lineContent,
lineNumber: range.start.line + index + 1,
type: .added
))
}
return lines
}
var body: some View {
VStack(alignment: .leading, spacing: 0) {
HStack {
Text("Suggested change")
.font(.system(size: chatFontSize, weight: .regular))
.foregroundColor(.secondary)
Spacer()
}
.padding(.leading, 8)
.padding(.vertical, 6)
.overlay(
RoundedRectangle(cornerRadius: 4)
.stroke(Color(NSColor.separatorColor), lineWidth: 0.5)
)
Rectangle()
.fill(.ultraThickMaterial)
.frame(height: 1)
ScrollView {
LazyVStack(spacing: 0) {
ForEach(diffLines.indices, id: \.self) { index in
DiffLineView(
line: diffLines[index],
chatFontSize: chatFontSize
)
}
}
}
.frame(maxHeight: 150)
.fixedSize(horizontal: false, vertical: true)
}
.frame(maxWidth: .infinity)
.background(
RoundedRectangle(cornerRadius: 4)
.fill(.ultraThickMaterial)
)
.clipShape(RoundedRectangle(cornerRadius: 4))
}
}
// MARK: - Diff Line View
private struct DiffLineView: View {
let line: SuggestedChangeView.DiffLine
let chatFontSize: CGFloat
@State private var contentHeight: CGFloat = 0
private var backgroundColor: SwiftUICore.Color {
switch line.type {
case .removed:
return Color("editorOverviewRuler.inlineChatRemoved")
case .added:
return Color("editor.focusedStackFrameHighlightBackground")
}
}
private var lineNumberBackgroundColor: SwiftUICore.Color {
switch line.type {
case .removed:
return Color("gitDecoration.deletedResourceForeground")
case .added:
return Color("gitDecoration.addedResourceForeground")
}
}
private var prefix: String {
switch line.type {
case .removed:
return "-"
case .added:
return "+"
}
}
var body: some View {
HStack(spacing: 0) {
HStack(alignment: .top, spacing: 0) {
HStack(spacing: 4) {
Text("\(line.lineNumber)")
Text(prefix)
}
}
.font(.system(size: chatFontSize))
.foregroundColor(.white)
.frame(width: 60, height: contentHeight) // TODO: dynamic set height by font size
.background(lineNumberBackgroundColor)
// Content section with text wrapping
VStack(alignment: .leading) {
Text(line.content)
.font(.system(size: chatFontSize))
.lineLimit(nil)
.multilineTextAlignment(.leading)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
}
.padding(.vertical, 4)
.padding(.leading, 8)
.background(backgroundColor)
.background(
GeometryReader { geometry in
Color.clear
.onAppear { contentHeight = geometry.size.height }
}
)
}
}
}