Skip to content

Commit 14d802f

Browse files
committed
Notify file open whenever focused element changes
1 parent 0f04557 commit 14d802f

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ public class RealtimeSuggestionController {
110110
guard let focusElement = application.focusedElement else { return }
111111
let focusElementType = focusElement.description
112112
focusedUIElement = focusElement
113+
114+
Task { // Notify suggestion service for open file.
115+
try await Task.sleep(nanoseconds: 500_000_000)
116+
let fileURL = try await Environment.fetchCurrentFileURL()
117+
_ = try await Workspace.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
118+
}
119+
113120
guard focusElementType == "Source Editor" else { return }
114121
sourceEditor = focusElement
115122

@@ -140,11 +147,9 @@ public class RealtimeSuggestionController {
140147

141148
Task { // Get cache ready for real-time suggestions.
142149
guard UserDefaults.shared.value(for: \.preCacheOnFileOpen) else { return }
143-
guard
144-
let fileURL = try? await Environment.fetchCurrentFileURL(),
145-
let (_, filespace) = try? await Workspace
150+
let fileURL = try await Environment.fetchCurrentFileURL()
151+
let (_, filespace) = try await Workspace
146152
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
147-
else { return }
148153

149154
if filespace.uti == nil {
150155
Logger.service.info("Generate cache for file.")
@@ -268,9 +273,10 @@ public class RealtimeSuggestionController {
268273

269274
func notifyEditingFileChange(editor: AXUIElement) async {
270275
guard let fileURL = try? await Environment.fetchCurrentFileURL(),
271-
let (workspace, filespace) = try? await Workspace
272-
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
276+
let (workspace, filespace) = try? await Workspace
277+
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
273278
else { return }
274279
workspace.notifyUpdateFile(filespace: filespace, content: editor.value)
275280
}
276281
}
282+

0 commit comments

Comments
 (0)