Skip to content

Commit 4c39ad0

Browse files
committed
Add a shared widget data source
1 parent 406938a commit 4c39ad0

File tree

3 files changed

+83
-31
lines changed

3 files changed

+83
-31
lines changed

Core/Sources/Service/GUI/GraphicalUserInterfaceController.swift.swift

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,7 @@ public final class GraphicalUserInterfaceController {
99
nonisolated let suggestionWidget = SuggestionWidgetController()
1010
private nonisolated init() {
1111
Task { @MainActor in
12-
suggestionWidget.onNextButtonTapped = {
13-
Task { @ServiceActor in
14-
let handler = PseudoCommandHandler()
15-
await handler.presentNextSuggestion()
16-
}
17-
}
18-
19-
suggestionWidget.onPreviousButtonTapped = {
20-
Task { @ServiceActor in
21-
let handler = PseudoCommandHandler()
22-
await handler.presentPreviousSuggestion()
23-
}
24-
}
25-
26-
suggestionWidget.onRejectButtonTapped = {
27-
Task { @ServiceActor in
28-
let handler = PseudoCommandHandler()
29-
await handler.rejectSuggestions()
30-
}
31-
}
32-
33-
suggestionWidget.onAcceptButtonTapped = {
34-
Task { @ServiceActor in
35-
let handler = PseudoCommandHandler()
36-
await handler.acceptSuggestion()
37-
}
38-
}
12+
suggestionWidget.dataSource = WidgetDataSource.shared
3913
}
4014
}
4115
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import ChatService
2+
import Foundation
3+
import SuggestionWidget
4+
5+
final class WidgetDataSource: SuggestionWidgetDataSource {
6+
static let shared = WidgetDataSource()
7+
8+
private init() {}
9+
10+
func suggestionForFile(at url: URL) async -> SuggestionProvider? {
11+
for workspace in await workspaces.values {
12+
if let filespace = await workspace.filespaces[url],
13+
let suggestion = await filespace.presentingSuggestion
14+
{
15+
return .init(
16+
code: suggestion.text,
17+
language: await filespace.language,
18+
startLineIndex: suggestion.position.line,
19+
suggestionCount: await filespace.suggestions.count,
20+
currentSuggestionIndex: await filespace.suggestionIndex,
21+
onSelectPreviousSuggestionTapped: {
22+
Task { @ServiceActor in
23+
let handler = PseudoCommandHandler()
24+
await handler.presentPreviousSuggestion()
25+
}
26+
},
27+
onSelectNextSuggestionTapped: {
28+
Task { @ServiceActor in
29+
let handler = PseudoCommandHandler()
30+
await handler.presentNextSuggestion()
31+
}
32+
},
33+
onRejectSuggestionTapped: {
34+
Task { @ServiceActor in
35+
let handler = PseudoCommandHandler()
36+
await handler.rejectSuggestions()
37+
}
38+
},
39+
onAcceptSuggestionTapped: {
40+
Task { @ServiceActor in
41+
let handler = PseudoCommandHandler()
42+
await handler.acceptSuggestion()
43+
}
44+
}
45+
)
46+
}
47+
}
48+
return nil
49+
}
50+
51+
func chatForFile(at url: URL) async -> ChatProvider? {
52+
for workspace in await workspaces.values {
53+
if let filespace = await workspace.filespaces[url],
54+
let service = await filespace.chatService
55+
{
56+
return .init(
57+
service: service,
58+
fileURL: url,
59+
onCloseChat: { [weak filespace] in
60+
Task { @ServiceActor [weak filespace] in
61+
filespace?.chatService = nil
62+
}
63+
}
64+
)
65+
}
66+
}
67+
return nil
68+
}
69+
70+
func chatServiceForFile(at url: URL) async -> ChatService? {
71+
for workspace in await workspaces.values {
72+
if let filespace = await workspace.filespaces[url] {
73+
return await filespace.chatService
74+
}
75+
}
76+
return nil
77+
}
78+
}

Core/Sources/SuggestionWidget/SuggestionWidgetDataSource.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Foundation
22

33
public protocol SuggestionWidgetDataSource {
4-
func suggestionForFile(at path: URL) -> (SuggestionProvider)?
5-
func chatForFile(at path: URL) -> ChatProvider?
4+
func suggestionForFile(at url: URL) async -> (SuggestionProvider)?
5+
func chatForFile(at url: URL) async -> ChatProvider?
66
}
77

88
struct MockWidgetDataSource: SuggestionWidgetDataSource {
9-
func suggestionForFile(at path: URL) -> (SuggestionProvider)? {
9+
func suggestionForFile(at url: URL) async -> (SuggestionProvider)? {
1010
return SuggestionProvider(
1111
code: """
1212
func test() {
@@ -22,7 +22,7 @@ struct MockWidgetDataSource: SuggestionWidgetDataSource {
2222
)
2323
}
2424

25-
func chatForFile(at path: URL) -> ChatProvider? {
25+
func chatForFile(at url: URL) async -> ChatProvider? {
2626
return nil
2727
}
2828
}

0 commit comments

Comments
 (0)