@@ -9,11 +9,11 @@ import Foundation
99import Logger
1010import Preferences
1111import QuartzCore
12+ import Workspace
1213import XcodeInspector
1314
14- @ServiceActor
15+ @WorkspaceActor
1516public class RealtimeSuggestionController {
16- public nonisolated static let shared = RealtimeSuggestionController ( )
1717 var eventObserver : CGEventObserverType = CGEventObserver ( eventsOfInterest: [
1818 . keyUp,
1919 . keyDown,
@@ -28,12 +28,11 @@ public class RealtimeSuggestionController {
2828 private var focusedUIElement : AXUIElement ?
2929 private var sourceEditor : SourceEditor ?
3030
31- private nonisolated init ( ) {
31+ init ( ) {
3232 Task { [ weak self] in
33-
3433 if let app = ActiveApplicationMonitor . activeXcode {
35- await self ? . handleXcodeChanged ( app)
36- await self ? . startHIDObservation ( by: 1 )
34+ self ? . handleXcodeChanged ( app)
35+ self ? . startHIDObservation ( by: 1 )
3736 }
3837 var previousApp = ActiveApplicationMonitor . activeXcode
3938 for await app in ActiveApplicationMonitor . createStream ( ) {
@@ -42,13 +41,13 @@ public class RealtimeSuggestionController {
4241 defer { previousApp = app }
4342
4443 if let app = ActiveApplicationMonitor . activeXcode, app != previousApp {
45- await self . handleXcodeChanged ( app)
44+ self . handleXcodeChanged ( app)
4645 }
4746
4847 if ActiveApplicationMonitor . activeXcode != nil {
49- await startHIDObservation ( by: 1 )
48+ startHIDObservation ( by: 1 )
5049 } else {
51- await stopHIDObservation ( by: 1 )
50+ stopHIDObservation ( by: 1 )
5251 }
5352 }
5453 }
@@ -104,11 +103,12 @@ public class RealtimeSuggestionController {
104103 guard let focusElement = application. focusedElement else { return }
105104 let focusElementType = focusElement. description
106105 focusedUIElement = focusElement
107-
106+
108107 Task { // Notify suggestion service for open file.
109108 try await Task . sleep ( nanoseconds: 500_000_000 )
110109 let fileURL = try await Environment . fetchCurrentFileURL ( )
111- _ = try await Workspace . fetchOrCreateWorkspaceIfNeeded ( fileURL: fileURL)
110+ _ = try await Service . shared. workspacePool
111+ . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
112112 }
113113
114114 guard focusElementType == " Source Editor " else { return }
@@ -154,21 +154,21 @@ public class RealtimeSuggestionController {
154154 }
155155 }
156156
157- Task { // Get cache ready for real-time suggestions.
157+ Task { @ WorkspaceActor in // Get cache ready for real-time suggestions.
158158 guard UserDefaults . shared. value ( for: \. preCacheOnFileOpen) else { return }
159159 let fileURL = try await Environment . fetchCurrentFileURL ( )
160- let ( _, filespace) = try await Workspace
161- . fetchOrCreateWorkspaceIfNeeded ( fileURL: fileURL)
160+ let ( _, filespace) = try await Service . shared . workspacePool
161+ . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
162162
163- if filespace. uti == nil {
163+ if filespace. codeMetadata . uti == nil {
164164 Logger . service. info ( " Generate cache for file. " )
165165 // avoid the command get called twice
166- filespace. uti = " "
166+ filespace. codeMetadata . uti = " "
167167 do {
168168 try await Environment . triggerAction ( " Real-time Suggestions " )
169169 } catch {
170- if filespace. uti? . isEmpty ?? true {
171- filespace. uti = nil
170+ if filespace. codeMetadata . uti? . isEmpty ?? true {
171+ filespace. codeMetadata . uti = nil
172172 }
173173 }
174174 }
@@ -191,7 +191,7 @@ public class RealtimeSuggestionController {
191191 }
192192
193193 func triggerPrefetchDebounced( force: Bool = false ) {
194- inflightPrefetchTask = Task { @ServiceActor in
194+ inflightPrefetchTask = Task { @WorkspaceActor in
195195 try ? await Task . sleep ( nanoseconds: UInt64 ( (
196196 UserDefaults . shared. value ( for: \. realtimeSuggestionDebounce)
197197 ) * 1_000_000_000 ) )
@@ -201,8 +201,8 @@ public class RealtimeSuggestionController {
201201
202202 if UserDefaults . shared. value ( for: \. disableSuggestionFeatureGlobally) ,
203203 let fileURL = try ? await Environment . fetchCurrentFileURL ( ) ,
204- let ( workspace, _) = try ? await Workspace
205- . fetchOrCreateWorkspaceIfNeeded ( fileURL: fileURL)
204+ let ( workspace, _) = try ? await Service . shared . workspacePool
205+ . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
206206 {
207207 let isEnabled = workspace. isSuggestionFeatureEnabled
208208 if !isEnabled { return }
@@ -221,7 +221,7 @@ public class RealtimeSuggestionController {
221221
222222 // cancel in-flight tasks
223223 await withTaskGroup ( of: Void . self) { group in
224- for (_, workspace) in workspaces {
224+ for (_, workspace) in Service . shared . workspacePool . workspaces {
225225 group. addTask {
226226 await workspace. cancelInFlightRealtimeSuggestionRequests ( )
227227 }
@@ -240,10 +240,10 @@ public class RealtimeSuggestionController {
240240
241241 func notifyEditingFileChange( editor: AXUIElement ) async {
242242 guard let fileURL = try ? await Environment . fetchCurrentFileURL ( ) ,
243- let ( workspace, filespace) = try ? await Workspace
244- . fetchOrCreateWorkspaceIfNeeded ( fileURL: fileURL)
243+ let ( workspace, filespace) = try ? await Service . shared . workspacePool
244+ . fetchOrCreateWorkspaceAndFilespace ( fileURL: fileURL)
245245 else { return }
246- workspace. notifyUpdateFile ( filespace: filespace, content: editor. value)
246+ workspace. suggestionPlugin ? . notifyUpdateFile ( filespace: filespace, content: editor. value)
247247 }
248248}
249249
0 commit comments