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
157 lines (145 loc) · 6.18 KB
/
PromptToCodeGroup.swift
File metadata and controls
157 lines (145 loc) · 6.18 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
import ComposableArchitecture
import Foundation
import PromptToCodeService
import SuggestionModel
import Environment
public struct PromptToCodeGroup: ReducerProtocol {
public struct State: Equatable {
public var promptToCodes: IdentifiedArrayOf<PromptToCode.State> = []
public var activeDocumentURL: PromptToCode.State.ID?
public var activePromptToCode: PromptToCode.State? {
get {
if let detached = promptToCodes.first(where: { !$0.isAttachedToSelectionRange }) {
return detached
}
guard let id = activeDocumentURL else { return nil }
return promptToCodes[id: id]
}
set { activeDocumentURL = newValue?.id }
}
}
public struct PromptToCodeInitialState: Equatable {
public var code: String
public var selectionRange: CursorRange?
public var language: CodeLanguage
public var identSize: Int
public var usesTabsForIndentation: Bool
public var documentURL: URL
public var projectRootURL: URL
public var allCode: String
public var isContinuous: Bool
public var commandName: String?
public var defaultPrompt: String
public var extraSystemPrompt: String?
public var generateDescriptionRequirement: Bool?
public init(
code: String,
selectionRange: CursorRange?,
language: CodeLanguage,
identSize: Int,
usesTabsForIndentation: Bool,
documentURL: URL,
projectRootURL: URL,
allCode: String,
isContinuous: Bool,
commandName: String?,
defaultPrompt: String,
extraSystemPrompt: String?,
generateDescriptionRequirement: Bool?
) {
self.code = code
self.selectionRange = selectionRange
self.language = language
self.identSize = identSize
self.usesTabsForIndentation = usesTabsForIndentation
self.documentURL = documentURL
self.projectRootURL = projectRootURL
self.allCode = allCode
self.isContinuous = isContinuous
self.commandName = commandName
self.defaultPrompt = defaultPrompt
self.extraSystemPrompt = extraSystemPrompt
self.generateDescriptionRequirement = generateDescriptionRequirement
}
}
public enum Action: Equatable {
/// Activate the prompt to code if it exists or create it if it doesn't
case activateOrCreatePromptToCode(PromptToCodeInitialState)
case createPromptToCode(PromptToCodeInitialState)
case updatePromptToCodeRange(id: PromptToCode.State.ID, range: CursorRange)
case discardAcceptedPromptToCodeIfNotContinuous(id: PromptToCode.State.ID)
case updateActivePromptToCode(documentURL: URL)
case discardExpiredPromptToCode(documentURLs: [URL])
case promptToCode(PromptToCode.State.ID, PromptToCode.Action)
}
@Dependency(\.promptToCodeServiceFactory) var promptToCodeServiceFactory
public var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case let .activateOrCreatePromptToCode(s):
guard state.activePromptToCode == nil else {
return .none
}
return .run { send in
await send(.createPromptToCode(s))
}
case let .createPromptToCode(s):
let newPromptToCode = PromptToCode.State(
code: s.code,
prompt: s.defaultPrompt,
language: s.language,
indentSize: s.identSize,
usesTabsForIndentation: s.usesTabsForIndentation,
projectRootURL: s.projectRootURL,
documentURL: s.documentURL,
allCode: s.allCode,
commandName: s.commandName,
isContinuous: s.isContinuous,
selectionRange: s.selectionRange,
extraSystemPrompt: s.extraSystemPrompt,
generateDescriptionRequirement: s.generateDescriptionRequirement
)
// 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 !newPromptToCode.prompt.isEmpty {
await send(.promptToCode(newPromptToCode.id, .modifyCodeButtonTapped))
}
}.cancellable(
id: PromptToCode.CancellationKey.modifyCode(newPromptToCode.id),
cancelInFlight: true
)
case let .updatePromptToCodeRange(id, range):
if let p = state.promptToCodes[id: id], p.isAttachedToSelectionRange {
state.promptToCodes[id: id]?.selectionRange = range
}
return .none
case let .discardAcceptedPromptToCodeIfNotContinuous(id):
state.promptToCodes.removeAll { $0.id == id && !$0.isContinuous }
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 let .promptToCode(id, action):
switch action {
case .cancelButtonTapped:
state.promptToCodes.remove(id: id)
return .run { _ in
try await Environment.makeXcodeActive()
}
default:
return .none
}
}
}
.forEach(\.promptToCodes, action: /Action.promptToCode, element: {
PromptToCode()
.dependency(\.promptToCodeService, promptToCodeServiceFactory())
})
}
}