forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetDataSource.swift
More file actions
147 lines (137 loc) · 5.28 KB
/
WidgetDataSource.swift
File metadata and controls
147 lines (137 loc) · 5.28 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
import ActiveApplicationMonitor
import ChatService
import ChatTab
import ComposableArchitecture
import Foundation
import GitHubCopilotService
import OpenAIService
import PromptToCodeService
import SuggestionModel
import SuggestionWidget
@MainActor
final class WidgetDataSource {
final class PromptToCode {
let promptToCodeService: PromptToCodeService
let provider: PromptToCodeProvider
public init(
promptToCodeService: PromptToCodeService,
provider: PromptToCodeProvider
) {
self.promptToCodeService = promptToCodeService
self.provider = provider
}
}
private(set) var promptToCodes = [URL: PromptToCode]()
init() {}
@discardableResult
func createPromptToCode(
for url: URL,
projectURL: URL,
selectedCode: String,
allCode: String,
selectionRange: CursorRange,
language: CodeLanguage,
identSize: Int = 4,
usesTabsForIndentation: Bool = false,
extraSystemPrompt: String?,
generateDescriptionRequirement: Bool?,
name: String?
) async -> PromptToCodeService {
let build = {
let service = PromptToCodeService(
code: selectedCode,
selectionRange: selectionRange,
language: language,
identSize: identSize,
usesTabsForIndentation: usesTabsForIndentation,
projectRootURL: projectURL,
fileURL: url,
allCode: allCode,
extraSystemPrompt: extraSystemPrompt,
generateDescriptionRequirement: generateDescriptionRequirement
)
let provider = PromptToCodeProvider(
service: service,
name: name,
onClosePromptToCode: { [weak self] in
self?.removePromptToCode(for: url)
let presenter = PresentInWindowSuggestionPresenter()
presenter.closePromptToCode(fileURL: url)
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
Task { @MainActor in
try await Task.sleep(nanoseconds: 200_000_000)
app.activate()
}
}
}
)
return PromptToCode(promptToCodeService: service, provider: provider)
}
let newPromptToCode = build()
promptToCodes[url] = newPromptToCode
return newPromptToCode.promptToCodeService
}
func removePromptToCode(for url: URL) {
promptToCodes[url] = nil
}
func cleanup(for url: URL) {
removePromptToCode(for: url)
}
}
extension WidgetDataSource: SuggestionWidgetDataSource {
func suggestionForFile(at url: URL) async -> SuggestionProvider? {
for workspace in await workspaces.values {
if let filespace = await workspace.filespaces[url],
let suggestion = await filespace.presentingSuggestion
{
return await .init(
code: suggestion.text,
language: filespace.language,
startLineIndex: suggestion.position.line,
suggestionCount: filespace.suggestions.count,
currentSuggestionIndex: filespace.suggestionIndex,
onSelectPreviousSuggestionTapped: {
Task { @ServiceActor in
let handler = PseudoCommandHandler()
await handler.presentPreviousSuggestion()
}
},
onSelectNextSuggestionTapped: {
Task { @ServiceActor in
let handler = PseudoCommandHandler()
await handler.presentNextSuggestion()
}
},
onRejectSuggestionTapped: {
Task { @ServiceActor in
let handler = PseudoCommandHandler()
await handler.rejectSuggestions()
if let app = ActiveApplicationMonitor.previousActiveApplication,
app.isXcode
{
try await Task.sleep(nanoseconds: 200_000_000)
app.activate()
}
}
},
onAcceptSuggestionTapped: {
Task { @ServiceActor in
let handler = PseudoCommandHandler()
await handler.acceptSuggestion()
if let app = ActiveApplicationMonitor.previousActiveApplication,
app.isXcode
{
try await Task.sleep(nanoseconds: 200_000_000)
app.activate()
}
}
}
)
}
}
return nil
}
func promptToCodeForFile(at url: URL) async -> PromptToCodeProvider? {
return promptToCodes[url]?.provider
}
}