forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromptToCodeProvider+Service.swift
More file actions
81 lines (70 loc) · 2.51 KB
/
PromptToCodeProvider+Service.swift
File metadata and controls
81 lines (70 loc) · 2.51 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
import ActiveApplicationMonitor
import Combine
import PromptToCodeService
import SuggestionWidget
extension PromptToCodeProvider {
convenience init(
service: PromptToCodeService,
name: String?,
onClosePromptToCode: @escaping () -> Void
) {
self.init(
code: service.code,
language: service.language.rawValue,
description: "",
startLineIndex: service.selectionRange.start.line,
startLineColumn: service.selectionRange.start.character,
name: name
)
var cancellables = Set<AnyCancellable>()
service.$code.sink(receiveValue: set(\.code)).store(in: &cancellables)
service.$isResponding.sink(receiveValue: set(\.isResponding)).store(in: &cancellables)
service.$description.sink(receiveValue: set(\.description)).store(in: &cancellables)
service.$isContinuous.sink(receiveValue: set(\.isContinuous)).store(in: &cancellables)
service.$history.map { $0 != .empty }
.sink(receiveValue: set(\.canRevert)).store(in: &cancellables)
onCancelTapped = { [cancellables] in
_ = cancellables
service.stopResponding()
onClosePromptToCode()
}
onRevertTapped = {
service.revert()
}
onRequirementSent = { [weak self] requirement in
Task { [weak self] in
do {
try await service.modifyCode(prompt: requirement)
} catch is CancellationError {
return
} catch {
Task { @MainActor [weak self] in
self?.errorMessage = error.localizedDescription
}
}
}
}
onStopRespondingTap = {
service.stopResponding()
}
onAcceptSuggestionTapped = {
Task { @ServiceActor in
let handler = PseudoCommandHandler()
await handler.acceptSuggestion()
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
app.activate()
}
}
}
onContinuousToggleClick = {
service.isContinuous.toggle()
}
}
func set<T>(_ keyPath: WritableKeyPath<PromptToCodeProvider, T>) -> (T) -> Void {
return { [weak self] value in
Task { @MainActor [weak self] in
self?[keyPath: keyPath] = value
}
}
}
}