Skip to content

Commit cb126ae

Browse files
committed
Prevent real-time suggestion when completion panel is open only in comment mode
1 parent 348ff4b commit cb126ae

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public class RealtimeSuggestionController {
2525
private var editorObservationTask: Task<Void, Error>?
2626
private var focusedUIElement: AXUIElement?
2727

28+
var isCommentMode: Bool {
29+
PresentationMode(
30+
rawValue: UserDefaults.shared
31+
.integer(forKey: SettingsKey.suggestionPresentationMode)
32+
) == .comment
33+
}
34+
2835
private nonisolated init() {
2936
Task { [weak self] in
3037

@@ -41,7 +48,7 @@ public class RealtimeSuggestionController {
4148
if let app = ActiveApplicationMonitor.activeXcode, app != previousApp {
4249
await self.handleXcodeChanged(app)
4350
}
44-
51+
4552
#warning(
4653
"TOOD: Is it possible to get rid of hid event observation with only AXObserver?"
4754
)
@@ -159,7 +166,7 @@ public class RealtimeSuggestionController {
159166
#warning(
160167
"TODO: Any method to avoid using AppleScript to check that completion panel is presented?"
161168
)
162-
if await Environment.frontmostXcodeWindowIsEditor() {
169+
if isCommentMode, await Environment.frontmostXcodeWindowIsEditor() {
163170
if Task.isCancelled { return }
164171
self.triggerPrefetchDebounced(force: true)
165172
}
@@ -184,11 +191,16 @@ public class RealtimeSuggestionController {
184191

185192
os_log(.info, "Prefetch suggestions.")
186193

187-
if !force, await !Environment.frontmostXcodeWindowIsEditor() {
194+
let isCommentMode = PresentationMode(
195+
rawValue: UserDefaults.shared
196+
.integer(forKey: SettingsKey.suggestionPresentationMode)
197+
) == .comment
198+
199+
if isCommentMode, !force, await !Environment.frontmostXcodeWindowIsEditor() {
188200
os_log(.info, "Completion panel is open, blocked.")
189201
return
190202
}
191-
203+
192204
// So the editor won't be blocked (after information are cached)!
193205
await PseudoCommandHandler().generateRealtimeSuggestions()
194206
}
@@ -233,11 +245,11 @@ extension AXUIElement {
233245
var identifier: String {
234246
(try? copyValue(key: kAXIdentifierAttribute)) ?? ""
235247
}
236-
248+
237249
var value: String {
238250
(try? copyValue(key: kAXValueAttribute)) ?? ""
239251
}
240-
252+
241253
var focusedElement: AXUIElement? {
242254
try? copyValue(key: kAXFocusedUIElementAttribute)
243255
}

0 commit comments

Comments
 (0)