Skip to content

Commit 6e5fd5c

Browse files
committed
Make commands return immediately if possible
1 parent ac72b53 commit 6e5fd5c

7 files changed

Lines changed: 104 additions & 69 deletions
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Client
2-
import SuggestionModel
32
import Foundation
3+
import SuggestionModel
44
import XcodeKit
55

66
class CustomCommand: NSObject, XCSourceEditorCommand, CommandType {
@@ -10,21 +10,14 @@ class CustomCommand: NSObject, XCSourceEditorCommand, CommandType {
1010
with invocation: XCSourceEditorCommandInvocation,
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
13+
completionHandler(nil)
1314
Task {
14-
do {
15-
let service = try getService()
16-
if let content = try await service.customCommand(
17-
id: customCommandMap[invocation.commandIdentifier] ?? "",
18-
editorContent: .init(invocation)
19-
) {
20-
invocation.accept(content)
21-
}
22-
completionHandler(nil)
23-
} catch is CancellationError {
24-
completionHandler(nil)
25-
} catch {
26-
completionHandler(error)
27-
}
15+
let service = try getService()
16+
_ = try await service.customCommand(
17+
id: customCommandMap[invocation.commandIdentifier] ?? "",
18+
editorContent: .init(invocation)
19+
)
2820
}
2921
}
3022
}
23+
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Client
2-
import SuggestionModel
32
import Foundation
3+
import SuggestionModel
44
import XcodeKit
55

66
class GetSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
@@ -10,20 +10,30 @@ class GetSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
1010
with invocation: XCSourceEditorCommandInvocation,
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
13-
Task {
14-
do {
15-
let service = try getService()
16-
if let content = try await service.getSuggestedCode(
17-
editorContent: .init(invocation)
18-
) {
19-
invocation.accept(content)
13+
switch UserDefaults.shared.value(for: \.suggestionPresentationMode) {
14+
case .comment:
15+
Task {
16+
do {
17+
let service = try getService()
18+
if let content = try await service.getSuggestedCode(
19+
editorContent: .init(invocation)
20+
) {
21+
invocation.accept(content)
22+
}
23+
completionHandler(nil)
24+
} catch is CancellationError {
25+
completionHandler(nil)
26+
} catch {
27+
completionHandler(error)
2028
}
21-
completionHandler(nil)
22-
} catch is CancellationError {
23-
completionHandler(nil)
24-
} catch {
25-
completionHandler(error)
29+
}
30+
case .floatingWidget:
31+
completionHandler(nil)
32+
Task {
33+
let service = try getService()
34+
_ = try await service.getSuggestedCode(editorContent: .init(invocation))
2635
}
2736
}
2837
}
2938
}
39+
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Client
2-
import SuggestionModel
32
import Foundation
3+
import SuggestionModel
44
import XcodeKit
55

66
class NextSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
@@ -10,20 +10,32 @@ class NextSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
1010
with invocation: XCSourceEditorCommandInvocation,
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
13-
Task {
14-
do {
15-
let service = try getService()
16-
if let content = try await service.getNextSuggestedCode(
17-
editorContent: .init(invocation)
18-
) {
19-
invocation.accept(content)
13+
switch UserDefaults.shared.value(for: \.suggestionPresentationMode) {
14+
case .comment:
15+
Task {
16+
do {
17+
try await (Task(timeout: 7) {
18+
let service = try getService()
19+
if let content = try await service.getNextSuggestedCode(
20+
editorContent: .init(invocation)
21+
) {
22+
invocation.accept(content)
23+
}
24+
completionHandler(nil)
25+
}.value)
26+
} catch is CancellationError {
27+
completionHandler(nil)
28+
} catch {
29+
completionHandler(error)
2030
}
21-
completionHandler(nil)
22-
} catch is CancellationError {
23-
completionHandler(nil)
24-
} catch {
25-
completionHandler(error)
31+
}
32+
case .floatingWidget:
33+
completionHandler(nil)
34+
Task {
35+
let service = try getService()
36+
_ = try await service.getNextSuggestedCode(editorContent: .init(invocation))
2637
}
2738
}
2839
}
2940
}
41+

EditorExtension/PrefetchSuggestionsCommand.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class PrefetchSuggestionsCommand: NSObject, XCSourceEditorCommand, CommandType {
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
1313
completionHandler(nil)
14-
1514
Task {
1615
let service = try getService()
1716
await service.prefetchRealtimeSuggestions(editorContent: .init(invocation))
Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Client
2-
import SuggestionModel
32
import Foundation
3+
import SuggestionModel
44
import XcodeKit
55

66
class PreviousSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
@@ -10,20 +10,30 @@ class PreviousSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
1010
with invocation: XCSourceEditorCommandInvocation,
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
13-
Task {
14-
do {
15-
let service = try getService()
16-
if let content = try await service.getPreviousSuggestedCode(
17-
editorContent: .init(invocation)
18-
) {
19-
invocation.accept(content)
13+
switch UserDefaults.shared.value(for: \.suggestionPresentationMode) {
14+
case .comment:
15+
Task {
16+
do {
17+
let service = try getService()
18+
if let content = try await service.getPreviousSuggestedCode(
19+
editorContent: .init(invocation)
20+
) {
21+
invocation.accept(content)
22+
}
23+
completionHandler(nil)
24+
} catch is CancellationError {
25+
completionHandler(nil)
26+
} catch {
27+
completionHandler(error)
2028
}
21-
completionHandler(nil)
22-
} catch is CancellationError {
23-
completionHandler(nil)
24-
} catch {
25-
completionHandler(error)
29+
}
30+
case .floatingWidget:
31+
completionHandler(nil)
32+
Task {
33+
let service = try getService()
34+
_ = try await service.getPreviousSuggestedCode(editorContent: .init(invocation))
2635
}
2736
}
2837
}
2938
}
39+
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Client
2-
import SuggestionModel
32
import Foundation
3+
import SuggestionModel
44
import XcodeKit
55

66
class RejectSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
@@ -10,20 +10,32 @@ class RejectSuggestionCommand: NSObject, XCSourceEditorCommand, CommandType {
1010
with invocation: XCSourceEditorCommandInvocation,
1111
completionHandler: @escaping (Error?) -> Void
1212
) {
13-
Task {
14-
do {
15-
let service = try getService()
16-
if let content = try await service.getSuggestionRejectedCode(
17-
editorContent: .init(invocation)
18-
) {
19-
invocation.accept(content)
13+
switch UserDefaults.shared.value(for: \.suggestionPresentationMode) {
14+
case .comment:
15+
Task {
16+
do {
17+
try await (Task(timeout: 7) {
18+
let service = try getService()
19+
if let content = try await service.getSuggestionRejectedCode(
20+
editorContent: .init(invocation)
21+
) {
22+
invocation.accept(content)
23+
}
24+
completionHandler(nil)
25+
}.value)
26+
} catch is CancellationError {
27+
completionHandler(nil)
28+
} catch {
29+
completionHandler(error)
2030
}
21-
completionHandler(nil)
22-
} catch is CancellationError {
23-
completionHandler(nil)
24-
} catch {
25-
completionHandler(error)
31+
}
32+
case .floatingWidget:
33+
completionHandler(nil)
34+
Task {
35+
let service = try getService()
36+
_ = try await service.getSuggestionRejectedCode(editorContent: .init(invocation))
2637
}
2738
}
2839
}
2940
}
41+

EditorExtension/SourceEditorExtension.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class SourceEditorExtension: NSObject, XCSourceEditorExtension {
1111
RejectSuggestionCommand(),
1212
NextSuggestionCommand(),
1313
PreviousSuggestionCommand(),
14-
ToggleRealtimeSuggestionsCommand(),
1514
RealtimeSuggestionsCommand(),
1615
PrefetchSuggestionsCommand(),
1716
ChatWithSelectionCommand(),

0 commit comments

Comments
 (0)