Skip to content

Commit bce106b

Browse files
committed
Add command Prefetch Suggestions, lower debounce to 2 seconds
1 parent 87898c2 commit bce106b

File tree

7 files changed

+69
-3
lines changed

7 files changed

+69
-3
lines changed

Copilot for Xcode.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
C8009BFF2941C551007AA7E8 /* TurnOnRealtimeSuggestionsCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8009BFE2941C551007AA7E8 /* TurnOnRealtimeSuggestionsCommand.swift */; };
1111
C8009C012941C56C007AA7E8 /* TurnOffRealtimeSuggestionsCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8009C002941C56C007AA7E8 /* TurnOffRealtimeSuggestionsCommand.swift */; };
1212
C8009C032941C576007AA7E8 /* RealtimeSuggestionCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8009C022941C576007AA7E8 /* RealtimeSuggestionCommand.swift */; };
13+
C800DBB1294C624D00B04CAC /* PrefetchSuggestionsCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C800DBB0294C624D00B04CAC /* PrefetchSuggestionsCommand.swift */; };
1314
C814588F2939EFDC00135263 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C814588E2939EFDC00135263 /* Cocoa.framework */; };
1415
C81458942939EFDC00135263 /* SourceEditorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C81458932939EFDC00135263 /* SourceEditorExtension.swift */; };
1516
C81458962939EFDC00135263 /* GetSuggestionsCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = C81458952939EFDC00135263 /* GetSuggestionsCommand.swift */; };
@@ -129,6 +130,7 @@
129130
C8009BFE2941C551007AA7E8 /* TurnOnRealtimeSuggestionsCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnOnRealtimeSuggestionsCommand.swift; sourceTree = "<group>"; };
130131
C8009C002941C56C007AA7E8 /* TurnOffRealtimeSuggestionsCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnOffRealtimeSuggestionsCommand.swift; sourceTree = "<group>"; };
131132
C8009C022941C576007AA7E8 /* RealtimeSuggestionCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RealtimeSuggestionCommand.swift; sourceTree = "<group>"; };
133+
C800DBB0294C624D00B04CAC /* PrefetchSuggestionsCommand.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefetchSuggestionsCommand.swift; sourceTree = "<group>"; };
132134
C814588C2939EFDC00135263 /* Copilot.appex */ = {isa = PBXFileReference; explicitFileType = "wrapper.app-extension"; includeInIndex = 0; path = Copilot.appex; sourceTree = BUILT_PRODUCTS_DIR; };
133135
C814588E2939EFDC00135263 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
134136
C81458902939EFDC00135263 /* XcodeKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XcodeKit.framework; path = Library/Frameworks/XcodeKit.framework; sourceTree = DEVELOPER_DIR; };
@@ -219,6 +221,7 @@
219221
C8009BFE2941C551007AA7E8 /* TurnOnRealtimeSuggestionsCommand.swift */,
220222
C8009C002941C56C007AA7E8 /* TurnOffRealtimeSuggestionsCommand.swift */,
221223
C8009C022941C576007AA7E8 /* RealtimeSuggestionCommand.swift */,
224+
C800DBB0294C624D00B04CAC /* PrefetchSuggestionsCommand.swift */,
222225
C81458972939EFDC00135263 /* Info.plist */,
223226
C81458982939EFDC00135263 /* EditorExtension.entitlements */,
224227
);
@@ -434,6 +437,7 @@
434437
C87B03AB293B262E00C77EAE /* PreviousSuggestionCommand.swift in Sources */,
435438
C87B03A7293B261900C77EAE /* RejectSuggestionCommand.swift in Sources */,
436439
C8009C032941C576007AA7E8 /* RealtimeSuggestionCommand.swift in Sources */,
440+
C800DBB1294C624D00B04CAC /* PrefetchSuggestionsCommand.swift in Sources */,
437441
C81458962939EFDC00135263 /* GetSuggestionsCommand.swift in Sources */,
438442
);
439443
runOnlyForDeploymentPostprocessing = 0;

Core/Sources/Client/AsyncXPCService.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ public struct AsyncXPCService {
140140
}
141141
}
142142
}
143+
144+
public func prefetchRealtimeSuggestions(editorContent: EditorContent) async {
145+
guard let data = try? JSONEncoder().encode(editorContent) else { return }
146+
try? await withXPCServiceConnected(connection: connection) { service, continuation in
147+
service.prefetchRealtimeSuggestions(editorContent: data) {
148+
continuation.resume(())
149+
}
150+
}
151+
}
143152
}
144153

145154
struct AutoFinishContinuation<T> {

Core/Sources/Service/AutoTrigger.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ actor AutoTrigger {
2525

2626
func start(by listener: ObjectIdentifier) {
2727
listeners.insert(listener)
28+
2829
if task == nil {
2930
task = Task { [stream = eventObserver.stream] in
3031
var triggerTask: Task<Void, Error>?
31-
try? await Environment.triggerAction("Real-time Suggestions")
32+
try? await Environment.triggerAction("Prefetch Suggestions")
3233
for await _ in stream {
3334
triggerTask?.cancel()
3435
if Task.isCancelled { break }
@@ -43,15 +44,15 @@ actor AutoTrigger {
4344
}
4445

4546
triggerTask = Task { @ServiceActor in
46-
try? await Task.sleep(nanoseconds: 3_000_000_000)
47+
try? await Task.sleep(nanoseconds: 2_000_000_000)
4748
if Task.isCancelled { return }
4849
let fileURL = try? await Environment.fetchCurrentFileURL()
4950
guard let folderURL = try? await Environment.fetchCurrentProjectRootURL(fileURL),
5051
let workspace = workspaces[folderURL],
5152
workspace.isRealtimeSuggestionEnabled
5253
else { return }
5354
if Task.isCancelled { return }
54-
try? await Environment.triggerAction("Real-time Suggestions")
55+
try? await Environment.triggerAction("Prefetch Suggestions")
5556
}
5657
}
5758
}

Core/Sources/Service/XPCService.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,30 @@ public class XPCService: NSObject, XPCServiceProtocol {
258258
reply(nil)
259259
}
260260
}
261+
262+
public func prefetchRealtimeSuggestions(
263+
editorContent: Data,
264+
withReply reply: @escaping () -> Void
265+
) {
266+
Task { @ServiceActor in
267+
do {
268+
let editor = try JSONDecoder().decode(EditorContent.self, from: editorContent)
269+
let fileURL = try await Environment.fetchCurrentFileURL()
270+
let workspace = try await fetchOrCreateWorkspaceIfNeeded(fileURL: fileURL)
271+
_ = workspace.getRealtimeSuggestedCode(
272+
forFileAt: fileURL,
273+
content: editor.content,
274+
lines: editor.lines,
275+
cursorPosition: editor.cursorPosition,
276+
tabSize: editor.tabSize,
277+
indentSize: editor.indentSize,
278+
usesTabsForIndentation: editor.usesTabsForIndentation
279+
)
280+
reply()
281+
} catch {
282+
print(error)
283+
reply()
284+
}
285+
}
286+
}
261287
}

Core/Sources/XPCShared/XPCServiceProtocol.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ public protocol XPCServiceProtocol {
3535
)
3636

3737
func setAutoSuggestion(enabled: Bool, withReply reply: @escaping (Error?) -> Void)
38+
39+
func prefetchRealtimeSuggestions(
40+
editorContent: Data,
41+
withReply reply: @escaping () -> Void
42+
)
3843
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Client
2+
import CopilotModel
3+
import Foundation
4+
import XcodeKit
5+
6+
class PrefetchSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
7+
var name: String { "Prefetch Suggestions" }
8+
9+
func perform(
10+
with invocation: XCSourceEditorCommandInvocation,
11+
completionHandler: @escaping (Error?) -> Void
12+
) {
13+
completionHandler(nil)
14+
15+
Task {
16+
let service = try getService()
17+
await service.prefetchRealtimeSuggestions(editorContent: .init(invocation))
18+
}
19+
}
20+
}

EditorExtension/SourceEditorExtension.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class SourceEditorExtension: NSObject, XCSourceEditorExtension {
1212
TurnOnRealtimeSuggestionsCommand(),
1313
TurnOffRealtimeSuggestionsCommand(),
1414
RealtimeSuggestionsCommand(),
15+
PrefetchSuggestionsCommand(),
1516
].map(makeCommandDefinition)
1617
}
1718
}

0 commit comments

Comments
 (0)