Skip to content

Commit 48e3619

Browse files
committed
Make generateRealtimeSuggestions almost always work
1 parent b140270 commit 48e3619

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

Core/Sources/AXExtension/AXUIElement.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public extension AXUIElement {
1919
var description: String {
2020
(try? copyValue(key: kAXDescriptionAttribute)) ?? ""
2121
}
22+
23+
var isSourceEditor: Bool {
24+
description == "Source Editor"
25+
}
2226

2327
var selectedTextRange: Range<Int>? {
2428
guard let value: AXValue = try? copyValue(key: kAXSelectedTextRangeAttribute)

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class RealtimeSuggestionController {
2525
private var activeApplicationMonitorTask: Task<Void, Error>?
2626
private var editorObservationTask: Task<Void, Error>?
2727
private var focusedUIElement: AXUIElement?
28+
private var sourceEditor: AXUIElement?
2829

2930
var isCommentMode: Bool {
3031
UserDefaults.shared.value(for: \.suggestionPresentationMode) == .comment
@@ -108,8 +109,9 @@ public class RealtimeSuggestionController {
108109
let application = AXUIElementCreateApplication(activeXcode.processIdentifier)
109110
guard let focusElement = application.focusedElement else { return }
110111
let focusElementType = focusElement.description
111-
guard focusElementType == "Source Editor" else { return }
112112
focusedUIElement = focusElement
113+
guard focusElementType == "Source Editor" else { return }
114+
sourceEditor = focusElement
113115

114116
editorObservationTask?.cancel()
115117
editorObservationTask = nil
@@ -216,7 +218,7 @@ public class RealtimeSuggestionController {
216218
}
217219

218220
// So the editor won't be blocked (after information are cached)!
219-
await PseudoCommandHandler().generateRealtimeSuggestions()
221+
await PseudoCommandHandler().generateRealtimeSuggestions(sourceEditor: sourceEditor)
220222
}
221223
}
222224

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,10 @@ struct PseudoCommandHandler {
3838
))
3939
}
4040

41-
func generateRealtimeSuggestions() async {
42-
// Can't use handler directly if content is not available.
43-
guard let editor = await getEditorContent() else {
44-
try? await Environment.triggerAction("Prefetch Suggestions")
45-
return
46-
}
47-
41+
func generateRealtimeSuggestions(sourceEditor: AXUIElement?) async {
42+
// Can't use handler if content is not available.
43+
guard let editor = await getEditorContent(sourceEditor: sourceEditor) else { return }
44+
4845
// Otherwise, get it from pseudo handler directly.
4946
let mode = UserDefaults.shared.value(for: \.suggestionPresentationMode)
5047
switch mode {
@@ -78,7 +75,8 @@ struct PseudoCommandHandler {
7875
guard let focusElement = application.focusedElement,
7976
focusElement.description == "Source Editor"
8077
else { return }
81-
guard let (content, lines, cursorPosition) = await getFileContent() else {
78+
guard let (content, lines, cursorPosition) = await getFileContent(sourceEditor: nil)
79+
else {
8280
PresentInWindowSuggestionPresenter()
8381
.presentErrorMessage("Unable to get file content.")
8482
return
@@ -147,13 +145,13 @@ struct PseudoCommandHandler {
147145
}
148146

149147
private extension PseudoCommandHandler {
150-
func getFileContent() async
148+
func getFileContent(sourceEditor: AXUIElement?) async
151149
-> (content: String, lines: [String], cursorPosition: CursorPosition)?
152150
{
153151
guard let xcode = ActiveApplicationMonitor.activeXcode
154152
?? ActiveApplicationMonitor.latestXcode else { return nil }
155153
let application = AXUIElementCreateApplication(xcode.processIdentifier)
156-
guard let focusElement = application.focusedElement,
154+
guard let focusElement = sourceEditor ?? application.focusedElement,
157155
focusElement.description == "Source Editor"
158156
else { return nil }
159157
guard let selectionRange = focusElement.selectedTextRange else { return nil }
@@ -190,10 +188,10 @@ private extension PseudoCommandHandler {
190188
}
191189

192190
@ServiceActor
193-
func getEditorContent() async -> EditorContent? {
191+
func getEditorContent(sourceEditor: AXUIElement?) async -> EditorContent? {
194192
guard
195193
let filespace = await getFilespace(),
196-
let content = await getFileContent()
194+
let content = await getFileContent(sourceEditor: sourceEditor)
197195
else { return nil }
198196
let uti = filespace.uti ?? ""
199197
let tabSize = filespace.tabSize ?? 4

0 commit comments

Comments
 (0)