-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFixErrorPanelFeature.swift
More file actions
239 lines (197 loc) · 8.74 KB
/
FixErrorPanelFeature.swift
File metadata and controls
239 lines (197 loc) · 8.74 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
import ComposableArchitecture
import Foundation
import SuggestionBasic
import XcodeInspector
import ChatTab
import ConversationTab
@Reducer
public struct FixErrorPanelFeature {
@ObservableState
public struct State: Equatable {
public var focusedEditor: SourceEditor? = nil
public var editorContent: EditorInformation.SourceEditorContent? = nil
public var fixId: String? = nil
public var fixFailure: FixEditorErrorIssueFailure? = nil
public var cursorPosition: CursorPosition? {
editorContent?.cursorPosition
}
public var isPanelDisplayed: Bool = false
public var errorAnnotations: [EditorInformation.LineAnnotation] {
editorContent?.lineAnnotations.filter { $0.isError } ?? []
}
public var editorContentLines: [String] {
editorContent?.lines ?? []
}
public var errorAnnotationsAtCursorPosition: [EditorInformation.LineAnnotation] {
let errorAnnotations = errorAnnotations
guard !errorAnnotations.isEmpty, let cursorPosition = cursorPosition else {
return []
}
return errorAnnotations.filter { $0.line == cursorPosition.line + 1 }
}
public mutating func resetFailure() {
fixFailure = nil
fixId = nil
}
}
public enum Action: Equatable {
case onFocusedEditorChanged(SourceEditor?)
case onEditorContentChanged
case onScrollPositionChanged
case onCursorPositionChanged
case fixErrorIssue([EditorInformation.LineAnnotation])
case scheduleFixFailureReset
case appear
case onFailure(FixEditorErrorIssueFailure)
case checkDisplay
case resetFixFailure
// Annotation checking
case startAnnotationCheck
case onAnnotationCheckTimerFired
}
let id = UUID()
enum CancelID: Hashable {
case observeErrorNotification(UUID)
case annotationCheck(UUID)
case scheduleFixFailureReset(UUID)
}
public init() {}
@Dependency(\.suggestionWidgetControllerDependency) var suggestionWidgetControllerDependency
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .appear:
return .run { send in
let stream = AsyncStream<Void> { continuation in
let observer = NotificationCenter.default.addObserver(
forName: .fixEditorErrorIssueError,
object: nil,
queue: .main
) { notification in
guard let error = notification.userInfo?["error"] as? FixEditorErrorIssueFailure
else {
return
}
Task {
await send(.onFailure(error))
}
}
continuation.onTermination = { _ in
NotificationCenter.default.removeObserver(observer)
}
}
for await _ in stream {
// Stream continues until cancelled
}
}.cancellable(
id: CancelID.observeErrorNotification(id),
cancelInFlight: true
)
case .onFocusedEditorChanged(let editor):
state.focusedEditor = editor
return .merge(
.send(.startAnnotationCheck),
.send(.resetFixFailure)
)
case .onEditorContentChanged:
return .merge(
.send(.startAnnotationCheck),
.send(.resetFixFailure)
)
case .onScrollPositionChanged:
return .merge(
.send(.resetFixFailure),
// Force checking the annotation
.send(.onAnnotationCheckTimerFired),
.send(.checkDisplay)
)
case .onCursorPositionChanged:
return .merge(
.send(.resetFixFailure),
// Force checking the annotation
.send(.onAnnotationCheckTimerFired),
.send(.checkDisplay)
)
case .fixErrorIssue(let annotations):
guard let fileURL = state.focusedEditor?.realtimeDocumentURL ?? nil,
let workspaceURL = state.focusedEditor?.realtimeWorkspaceURL ?? nil
else {
return .none
}
let fixId = UUID().uuidString
state.fixId = fixId
state.fixFailure = nil
let editorErrorIssue: EditorErrorIssue = .init(
lineAnnotations: annotations,
fileURL: fileURL,
workspaceURL: workspaceURL,
id: fixId
)
let userInfo = [
"editorErrorIssue": editorErrorIssue
]
return .run { _ in
await MainActor.run {
suggestionWidgetControllerDependency.onOpenChatClicked()
NotificationCenter.default.post(
name: .fixEditorErrorIssue,
object: nil,
userInfo: userInfo
)
}
}
case .scheduleFixFailureReset:
return .run { send in
try await Task.sleep(nanoseconds: 3_000_000_000) // 3 seconds
await send(.resetFixFailure)
}
.cancellable(id: CancelID.scheduleFixFailureReset(id), cancelInFlight: true)
case .resetFixFailure:
state.resetFailure()
return .cancel(id: CancelID.scheduleFixFailureReset(id))
case .onFailure(let failure):
guard case let .isReceivingMessage(fixId) = failure,
fixId == state.fixId
else {
return .none
}
state.fixFailure = failure
return .run { send in await send(.scheduleFixFailureReset)}
case .checkDisplay:
state.isPanelDisplayed = !state.editorContentLines.isEmpty
&& !state.errorAnnotationsAtCursorPosition.isEmpty
return .none
// MARK: - Annotation Check
case .startAnnotationCheck:
return .run { send in
let startTime = Date()
let maxDuration: TimeInterval = 60 * 5
let interval: TimeInterval = 1
while Date().timeIntervalSince(startTime) < maxDuration {
try await Task.sleep(nanoseconds: UInt64(interval) * 1_000_000_000)
await send(.onAnnotationCheckTimerFired)
}
}.cancellable(id: CancelID.annotationCheck(id), cancelInFlight: true)
case .onAnnotationCheckTimerFired:
guard let editor = state.focusedEditor else {
return .cancel(id: CancelID.annotationCheck(id))
}
let newEditorContent = editor.getContent()
let newLineAnnotations = newEditorContent.lineAnnotations
let newErrorLineAnnotations = newLineAnnotations.filter { $0.isError }
let errorAnnotations = state.errorAnnotations
if state.editorContent != newEditorContent {
state.editorContent = newEditorContent
}
if errorAnnotations != newErrorLineAnnotations {
return .merge(
.send(.checkDisplay),
.cancel(id: CancelID.annotationCheck(id))
)
} else {
return .none
}
}
}
}
}