Skip to content

Commit 1c0990d

Browse files
committed
Add universal real-time suggestions toggle
1 parent a81e385 commit 1c0990d

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

Core/Sources/Service/Environment.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,21 @@ enum Environment {
4343
}
4444
return !activeXcodes.isEmpty
4545
}
46+
47+
static var frontmostXcodeWindowIsEditor: () async -> Bool = {
48+
let appleScript = """
49+
tell application "Xcode"
50+
return path of document of the first window
51+
end tell
52+
"""
53+
do {
54+
let result = try await runAppleScript(appleScript)
55+
return !result.isEmpty
56+
}
57+
catch {
58+
return false
59+
}
60+
}
4661

4762
static var fetchCurrentProjectRootURL: (_ fileURL: URL?) async throws -> URL? = { fileURL in
4863
let appleScript = """
@@ -51,7 +66,7 @@ enum Environment {
5166
end tell
5267
"""
5368

54-
let path = try await runAppleScript(appleScript)
69+
let path = (try? await runAppleScript(appleScript)) ?? ""
5570
if !path.isEmpty {
5671
let trimmedNewLine = path.trimmingCharacters(in: .newlines)
5772
var url = URL(fileURLWithPath: trimmedNewLine)
@@ -101,19 +116,20 @@ enum Environment {
101116
key: kAXFocusedWindowAttribute,
102117
ofType: AXUIElement.self
103118
)
104-
var path = try frontmostWindow.copyValue(
119+
var path = try? frontmostWindow.copyValue(
105120
key: kAXDocumentAttribute,
106121
ofType: String?.self
107122
)
108123
if path == nil {
109-
if let firstWindow = try application.copyValue(
124+
for window in try application.copyValue(
110125
key: kAXWindowsAttribute,
111126
ofType: [AXUIElement].self
112-
).first {
113-
path = try firstWindow.copyValue(
127+
) {
128+
path = try? window.copyValue(
114129
key: kAXDocumentAttribute,
115-
ofType: String.self
130+
ofType: String?.self
116131
)
132+
if path != nil { break }
117133
}
118134
}
119135
if let path = path?.removingPercentEncoding {

Core/Sources/Service/Workspace.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ final class Workspace {
4646

4747
var filespaces = [URL: Filespace]()
4848
var isRealtimeSuggestionEnabled: Bool {
49-
(UserDefaults.shared.dictionary(
50-
forKey: SettingsKey.realtimeSuggestionState
51-
)?[projectRootURL.absoluteString]) as? Bool ?? false
49+
UserDefaults.shared.bool(forKey: SettingsKey.realtimeSuggestionToggle) as? Bool ?? false
5250
}
5351

5452
var realtimeSuggestionRequests = Set<Task<Void, Error>>()

Core/Sources/Service/XPCService.swift

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ public class XPCService: NSObject, XPCServiceProtocol {
8989
) {
9090
Task { @ServiceActor in
9191
do {
92-
throw CancellationError()
9392
let editor = try JSONDecoder().decode(EditorContent.self, from: editorContent)
9493
let fileURL = try await Environment.fetchCurrentFileURL()
9594
let workspace = try await fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
@@ -265,19 +264,10 @@ public class XPCService: NSObject, XPCServiceProtocol {
265264
return
266265
}
267266
Task { @ServiceActor in
268-
let fileURL = try await Environment.fetchCurrentFileURL()
269-
let workspace = try await fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
270-
if var state = UserDefaults.shared
271-
.dictionary(forKey: SettingsKey.realtimeSuggestionState)
272-
{
273-
state[workspace.projectRootURL.absoluteString] = enabled
274-
UserDefaults.shared.set(state, forKey: SettingsKey.realtimeSuggestionState)
275-
} else {
276-
UserDefaults.shared.set(
277-
[workspace.projectRootURL.absoluteString: enabled],
278-
forKey: SettingsKey.realtimeSuggestionState
279-
)
280-
}
267+
UserDefaults.shared.set(
268+
enabled,
269+
forKey: SettingsKey.realtimeSuggestionToggle
270+
)
281271
reply(nil)
282272
}
283273
}
@@ -290,8 +280,8 @@ public class XPCService: NSObject, XPCServiceProtocol {
290280
do {
291281
let editor = try JSONDecoder().decode(EditorContent.self, from: editorContent)
292282
let fileURL = try await Environment.fetchCurrentFileURL()
293-
try Task.checkCancellation()
294283
let workspace = try await fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
284+
try Task.checkCancellation()
295285
_ = workspace.getRealtimeSuggestedCode(
296286
forFileAt: fileURL,
297287
content: editor.content,

EditorExtension/TurnOffRealtimeSuggestionsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import XcodeKit
55

66
class TurnOffRealtimeSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
7-
var name: String { "Turn Off Real-time Suggestions for Workspace" }
7+
var name: String { "Turn Off Real-time Suggestions" }
88

99
func perform(
1010
with invocation: XCSourceEditorCommandInvocation,

EditorExtension/TurnOnRealtimeSuggestionsCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import XcodeKit
55

66
class TurnOnRealtimeSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
7-
var name: String { "Turn On Real-time Suggestions for Workspace" }
7+
var name: String { "Turn On Real-time Suggestions" }
88

99
func perform(
1010
with invocation: XCSourceEditorCommandInvocation,

0 commit comments

Comments
 (0)