Skip to content

Commit fb1bcb4

Browse files
committed
Pre cache info for real-time suggestion
1 parent ea70a95 commit fb1bcb4

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,27 @@ public class RealtimeSuggestionController {
134134
}
135135
}
136136
}
137+
138+
Task { // Get cache ready for real-time suggestions.
139+
guard
140+
let fileURL = try? await Environment.fetchCurrentFileURL(),
141+
let (_, filespace) = try? await Workspace
142+
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
143+
else { return }
144+
145+
if filespace.uti == nil {
146+
Logger.service.info("Generate cache for file.")
147+
// avoid the command get called twice
148+
filespace.uti = ""
149+
do {
150+
try await Environment.triggerAction("Real-time Suggestions")
151+
} catch {
152+
if filespace.uti?.isEmpty ?? true {
153+
filespace.uti = nil
154+
}
155+
}
156+
}
157+
}
137158
}
138159

139160
func handleHIDEvent(event: CGEvent) async {

Core/Sources/Service/SuggestionCommandHandler/WindowBaseCommandHandler.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,20 @@ struct WindowBaseCommandHandler: SuggestionCommandHandler {
168168
}
169169

170170
func presentRealtimeSuggestions(editor: EditorContent) async throws -> UpdatedContent? {
171-
// not needed.
171+
Task {
172+
try? await prepareCache(editor: editor)
173+
}
174+
return nil
175+
}
176+
177+
func prepareCache(editor: EditorContent) async throws -> UpdatedContent? {
178+
let fileURL = try await Environment.fetchCurrentFileURL()
179+
let (_, filespace) = try await Workspace
180+
.fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
181+
filespace.uti = editor.uti
182+
filespace.tabSize = editor.tabSize
183+
filespace.indentSize = editor.indentSize
184+
filespace.usesTabsForIndentation = editor.usesTabsForIndentation
172185
return nil
173186
}
174187

0 commit comments

Comments
 (0)