Skip to content

Commit a0ece87

Browse files
committed
Adjust service
1 parent 045dd42 commit a0ece87

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public actor RealtimeSuggestionController {
163163

164164
func cancelInFlightTasks(excluding: Task<Void, Never>? = nil) async {
165165
inflightPrefetchTask?.cancel()
166-
166+
let workspaces = await Service.shared.workspacePool.workspaces
167167
// cancel in-flight tasks
168168
await withTaskGroup(of: Void.self) { group in
169-
for (_, workspace) in Service.shared.workspacePool.workspaces {
169+
for (_, workspace) in workspaces {
170170
group.addTask {
171171
await workspace.cancelInFlightRealtimeSuggestionRequests()
172172
}

Core/Sources/Service/Service.swift

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ import ProService
2525

2626
/// The running extension service.
2727
public final class Service {
28+
@MainActor
2829
public static let shared = Service()
2930

30-
let workspacePool = WorkspacePool()
31+
@Dependency(\.workspacePool) var workspacePool
3132
@MainActor
32-
public let guiController = GraphicalUserInterfaceController()
33-
public let realtimeSuggestionController = RealtimeSuggestionController()
33+
public let guiController: GraphicalUserInterfaceController
34+
public let commandHandler: CommandHandler
35+
public let realtimeSuggestionController: RealtimeSuggestionController
3436
public let scheduledCleaner: ScheduledCleaner
3537
let globalShortcutManager: GlobalShortcutManager
3638
let keyBindingManager: KeyBindingManager
@@ -43,15 +45,47 @@ public final class Service {
4345
@Dependency(\.toast) var toast
4446
var cancellable = Set<AnyCancellable>()
4547

48+
@MainActor
4649
private init() {
47-
WorkspacePoolDependencyKey.liveValue = workspacePool
48-
CommandHandlerDependencyKey.liveValue = PseudoCommandHandler()
50+
@Dependency(\.workspacePool) var workspacePool
51+
let commandHandler = PseudoCommandHandler()
52+
self.commandHandler = commandHandler
53+
54+
func setup<R>(_ operation: () -> R) -> R {
55+
withDependencies { values in
56+
values.commandHandler = commandHandler
57+
} operation: {
58+
operation() //
59+
}
60+
}
61+
62+
realtimeSuggestionController = setup { .init() }
63+
scheduledCleaner = setup { .init() }
64+
let guiController = setup { GraphicalUserInterfaceController() }
65+
self.guiController = guiController
66+
globalShortcutManager = setup { .init(guiController: guiController) }
67+
68+
keyBindingManager = setup {
69+
.init(
70+
workspacePool: workspacePool,
71+
acceptSuggestion: {
72+
Task { await PseudoCommandHandler().acceptSuggestion() }
73+
},
74+
dismissSuggestion: {
75+
Task { await PseudoCommandHandler().dismissSuggestion() }
76+
}
77+
)
78+
}
79+
80+
#if canImport(ProService)
81+
proService = setup { ProService() }
82+
#endif
4983

5084
BuiltinExtensionManager.shared.setupExtensions([
5185
GitHubCopilotExtension(workspacePool: workspacePool),
5286
CodeiumExtension(workspacePool: workspacePool),
5387
])
54-
scheduledCleaner = .init()
88+
5589
workspacePool.registerPlugin {
5690
SuggestionServiceWorkspacePlugin(workspace: $0) { SuggestionService.service() }
5791
}
@@ -64,21 +98,6 @@ public final class Service {
6498
workspacePool.registerPlugin {
6599
BuiltinExtensionWorkspacePlugin(workspace: $0)
66100
}
67-
68-
globalShortcutManager = .init(guiController: guiController)
69-
keyBindingManager = .init(
70-
workspacePool: workspacePool,
71-
acceptSuggestion: {
72-
Task { await PseudoCommandHandler().acceptSuggestion() }
73-
},
74-
dismissSuggestion: {
75-
Task { await PseudoCommandHandler().dismissSuggestion() }
76-
}
77-
)
78-
79-
#if canImport(ProService)
80-
proService = ProService()
81-
#endif
82101

83102
scheduledCleaner.service = self
84103
}
@@ -101,9 +120,10 @@ public final class Service {
101120
.removeDuplicates()
102121
.filter { $0 != .init(fileURLWithPath: "/") }
103122
.compactMap { $0 }
104-
.sink { [weak self] fileURL in
123+
.sink { fileURL in
105124
Task {
106-
try await self?.workspacePool
125+
@Dependency(\.workspacePool) var workspacePool
126+
return try await workspacePool
107127
.fetchOrCreateWorkspaceAndFilespace(fileURL: fileURL)
108128
}
109129
}.store(in: &cancellable)

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,17 @@ struct PseudoCommandHandler: CommandHandler {
326326

327327
func dismissSuggestion() async {
328328
guard let documentURL = await XcodeInspector.shared.safe.activeDocumentURL else { return }
329+
PresentInWindowSuggestionPresenter().discardSuggestion(fileURL: documentURL)
329330
guard let (_, filespace) = try? await Service.shared.workspacePool
330331
.fetchOrCreateWorkspaceAndFilespace(fileURL: documentURL) else { return }
331-
332332
await filespace.reset()
333-
PresentInWindowSuggestionPresenter().discardSuggestion(fileURL: documentURL)
334333
}
335334

336335
func openChat(forceDetach: Bool) {
337336
switch UserDefaults.shared.value(for: \.openChatMode) {
338337
case .chatPanel:
339-
let store = Service.shared.guiController.store
340338
Task { @MainActor in
339+
let store = Service.shared.guiController.store
341340
await store.send(.createAndSwitchToChatGPTChatTabIfNeeded).finish()
342341
store.send(.openChatPanel(forceDetach: forceDetach))
343342
}
@@ -360,8 +359,8 @@ struct PseudoCommandHandler: CommandHandler {
360359

361360
if openInApp {
362361
#if canImport(BrowserChatTab)
363-
let store = Service.shared.guiController.store
364362
Task { @MainActor in
363+
let store = Service.shared.guiController.store
365364
await store.send(.createAndSwitchToChatTabIfNeededMatching(
366365
check: {
367366
func match(_ tabURL: URL?) -> Bool {
@@ -386,8 +385,8 @@ struct PseudoCommandHandler: CommandHandler {
386385
}
387386
}
388387
case .codeiumChat:
389-
let store = Service.shared.guiController.store
390388
Task { @MainActor in
389+
let store = Service.shared.guiController.store
391390
await store.send(
392391
.createAndSwitchToChatTabIfNeededMatching(
393392
check: { $0 is CodeiumChatTab },
@@ -400,7 +399,7 @@ struct PseudoCommandHandler: CommandHandler {
400399
}
401400

402401
func sendChatMessage(_ message: String) async {
403-
let store = Service.shared.guiController.store
402+
let store = await Service.shared.guiController.store
404403
await store.send(.sendCustomCommandToActiveChat(CustomCommand(
405404
commandId: "",
406405
name: "",

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
178178
var cursorPosition = editor.cursorPosition
179179
var extraInfo = SuggestionInjector.ExtraInfo()
180180

181-
let store = Service.shared.guiController.store
181+
let store = await Service.shared.guiController.store
182182

183183
if let promptToCode = store.state.promptToCodeGroup.activePromptToCode {
184184
if promptToCode.isAttachedToSelectionRange, promptToCode.documentURL != fileURL {
@@ -388,7 +388,7 @@ extension WindowBaseCommandHandler {
388388
)
389389
}() as (String, CursorRange)
390390

391-
let store = Service.shared.guiController.store
391+
let store = await Service.shared.guiController.store
392392

393393
let customCommandTemplateProcessor = CustomCommandTemplateProcessor()
394394

0 commit comments

Comments
 (0)