forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptToCodeGroup.swift
More file actions
129 lines (117 loc) · 4.88 KB
/
Copy pathPromptToCodeGroup.swift
File metadata and controls
129 lines (117 loc) · 4.88 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
import ComposableArchitecture
import Foundation
import PromptToCodeService
import SuggestionBasic
import XcodeInspector
@Reducer
public struct PromptToCodeGroup {
@ObservableState
public struct State {
public var promptToCodes: IdentifiedArrayOf<PromptToCodePanel.State> = []
public var activeDocumentURL: PromptToCodePanel.State.ID? = XcodeInspector.shared
.realtimeActiveDocumentURL
public var activePromptToCode: PromptToCodePanel.State? {
get {
if let detached = promptToCodes
.first(where: { !$0.promptToCodeState.isAttachedToTarget })
{
return detached
}
guard let id = activeDocumentURL else { return nil }
return promptToCodes[id: id]
}
set {
if let id = newValue?.id {
promptToCodes[id: id] = newValue
}
}
}
}
public enum Action {
/// Activate the prompt to code if it exists or create it if it doesn't
case activateOrCreatePromptToCode(PromptToCodePanel.State)
case createPromptToCode(PromptToCodePanel.State, sendImmediately: Bool)
case updatePromptToCodeRange(
id: PromptToCodePanel.State.ID,
snippetId: UUID,
range: CursorRange
)
case discardAcceptedPromptToCodeIfNotContinuous(id: PromptToCodePanel.State.ID)
case updateActivePromptToCode(documentURL: URL)
case discardExpiredPromptToCode(documentURLs: [URL])
case promptToCode(PromptToCodePanel.State.ID, PromptToCodePanel.Action)
case activePromptToCode(PromptToCodePanel.Action)
}
@Dependency(\.activatePreviousActiveXcode) var activatePreviousActiveXcode
public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case let .activateOrCreatePromptToCode(s):
if let promptToCode = state.activePromptToCode {
return .run { send in
await send(.promptToCode(promptToCode.id, .focusOnTextField))
}
}
return .run { send in
await send(.createPromptToCode(s, sendImmediately: false))
}
case let .createPromptToCode(newPromptToCode, sendImmediately):
// insert at 0 so it has high priority then the other detached prompt to codes
state.promptToCodes.insert(newPromptToCode, at: 0)
return .run { send in
if sendImmediately,
!newPromptToCode.contextInputController.instruction.string.isEmpty
{
await send(.promptToCode(newPromptToCode.id, .modifyCodeButtonTapped))
}
}.cancellable(
id: PromptToCodePanel.CancellationKey.modifyCode(newPromptToCode.id),
cancelInFlight: true
)
case let .updatePromptToCodeRange(id, snippetId, range):
if let p = state.promptToCodes[id: id], p.promptToCodeState.isAttachedToTarget {
state.promptToCodes[id: id]?.promptToCodeState.snippets[id: snippetId]?
.attachedRange = range
}
return .none
case let .discardAcceptedPromptToCodeIfNotContinuous(id):
state.promptToCodes.removeAll { $0.id == id && $0.hasEnded }
return .none
case let .updateActivePromptToCode(documentURL):
state.activeDocumentURL = documentURL
return .none
case let .discardExpiredPromptToCode(documentURLs):
for url in documentURLs {
state.promptToCodes.remove(id: url)
}
return .none
case .promptToCode:
return .none
case .activePromptToCode:
return .none
}
}
.ifLet(\.activePromptToCode, action: \.activePromptToCode) {
PromptToCodePanel()
}
.forEach(\.promptToCodes, action: /Action.promptToCode, element: {
PromptToCodePanel()
})
Reduce { state, action in
switch action {
case let .promptToCode(id, .cancelButtonTapped):
state.promptToCodes.remove(id: id)
return .run { _ in
activatePreviousActiveXcode()
}
case .activePromptToCode(.cancelButtonTapped):
guard let id = state.activePromptToCode?.id else { return .none }
state.promptToCodes.remove(id: id)
return .run { _ in
activatePreviousActiveXcode()
}
default: return .none
}
}
}
}