Skip to content

Commit da20a9a

Browse files
committed
Merge branch 'feature/rollback-suggestion-caching-machanism' into develop
2 parents bf7d6c4 + 04e088b commit da20a9a

3 files changed

Lines changed: 18 additions & 30 deletions

File tree

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,6 @@ 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-
}
158137
}
159138

160139
func handleHIDEvent(event: CGEvent) async {

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,28 @@ struct PseudoCommandHandler {
3939
}
4040

4141
func generateRealtimeSuggestions() async {
42+
// Can't use handler directly if content is not available.
4243
guard let editor = await getEditorContent() else {
4344
try? await Environment.triggerAction("Prefetch Suggestions")
4445
return
4546
}
47+
48+
// If no cache is available, and completion panel is not displayed, try to get it with command.
49+
if editor.uti.isEmpty, await Environment.frontmostXcodeWindowIsEditor() {
50+
try? await Environment.triggerAction("Prefetch Suggestions")
51+
return
52+
}
53+
54+
// Otherwise, get it from pseudo handler directly.
4655
let mode = UserDefaults.shared.value(for: \.suggestionPresentationMode)
47-
let handler: SuggestionCommandHandler = {
48-
switch mode {
49-
case .comment:
50-
return CommentBaseCommandHandler()
51-
case .floatingWidget:
52-
return WindowBaseCommandHandler()
53-
}
54-
}()
55-
_ = try? await handler.generateRealtimeSuggestions(editor: editor)
56+
switch mode {
57+
case .comment:
58+
let handler = CommentBaseCommandHandler()
59+
_ = try? await handler.generateRealtimeSuggestions(editor: editor)
60+
case .floatingWidget:
61+
let handler = WindowBaseCommandHandler()
62+
_ = try? await handler.generateRealtimeSuggestions(editor: editor)
63+
}
5664
}
5765

5866
func rejectSuggestions() async {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fi
190190
- The first run of the extension will be slow. Be patient.
191191
- The extension uses some dirty tricks to get the file and project/workspace paths. It may fail, it may be incorrect, especially when you have multiple Xcode windows running, and maybe even worse when they are in different displays. I am not sure about that though.
192192
- The suggestions are presented as C-style comments in comment mode, they may break your code if you are editing a JSON file or something.
193+
- When a real-time suggestion request is triggered, there is a chance that it may briefly block the editor. This can occur at most once for each file after each restart of the extension because the extension needs to initiate real-time suggestion by clicking an item from the menu bar. However, once a command has been executed and some information is cached, the extension will be able to trigger real-time suggestion using a different method.
193194

194195
## License
195196

0 commit comments

Comments
 (0)