forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeReviewService.swift
More file actions
48 lines (39 loc) · 1.54 KB
/
CodeReviewService.swift
File metadata and controls
48 lines (39 loc) · 1.54 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
import Collections
import ConversationServiceProvider
import Foundation
import LanguageServerProtocol
public struct DocumentReview: Equatable {
public var comments: [ReviewComment]
public let originalContent: String
}
public typealias DocumentReviewsByUri = OrderedDictionary<DocumentUri, DocumentReview>
@MainActor
public class CodeReviewService: ObservableObject {
@Published public private(set) var documentReviews: DocumentReviewsByUri = [:]
public static let shared = CodeReviewService()
private init() {}
public func updateComments(for uri: DocumentUri, comments: [ReviewComment], originalContent: String) {
if var existing = documentReviews[uri] {
existing.comments.append(contentsOf: comments)
existing.comments = sortedComments(existing.comments)
documentReviews[uri] = existing
} else {
documentReviews[uri] = .init(comments: comments, originalContent: originalContent)
}
}
public func updateComments(_ fileComments: [CodeReviewResponse.FileComment]) {
for fileComment in fileComments {
updateComments(
for: fileComment.uri,
comments: fileComment.comments,
originalContent: fileComment.originalContent
)
}
}
private func sortedComments(_ comments: [ReviewComment]) -> [ReviewComment] {
return comments.sorted { $0.range.end.line < $1.range.end.line }
}
public func resetComments() {
documentReviews = [:]
}
}