|
| 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 | +} |
0 commit comments