Skip to content

Commit 98a5742

Browse files
committed
Merge branch 'feature/make-commands-return-immediately-if-possible' into develop
2 parents ac72b53 + 9d59125 commit 98a5742

8 files changed

+113
-79
lines changed
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+

EditorExtension/Helpers.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,20 @@ extension EditorContent {
4343
let buffer = invocation.buffer
4444
self.init(
4545
content: buffer.completeBuffer,
46-
lines: buffer.lines as! [String],
46+
lines: buffer.lines as? [String] ?? [],
4747
uti: buffer.contentUTI,
4848
cursorPosition: ((buffer.selections.lastObject as? XCSourceTextRange)?.end).map {
4949
CursorPosition(line: $0.line, character: $0.column)
5050
} ?? CursorPosition(line: 0, character: 0),
5151
selections: buffer.selections.map {
52-
Selection(
53-
start: CursorPosition(
54-
line: ($0 as! XCSourceTextRange).start.line,
55-
character: ($0 as! XCSourceTextRange).start.column
56-
),
57-
end: CursorPosition(
58-
line: ($0 as! XCSourceTextRange).end.line,
59-
character: ($0 as! XCSourceTextRange).end.column
60-
)
52+
let sl = ($0 as? XCSourceTextRange)?.start.line ?? 0
53+
let sc = ($0 as? XCSourceTextRange)?.start.column ?? 0
54+
let el = ($0 as? XCSourceTextRange)?.end.line ?? 0
55+
let ec = ($0 as? XCSourceTextRange)?.end.column ?? 0
56+
57+
return Selection(
58+
start: CursorPosition( line: sl, character: sc ),
59+
end: CursorPosition( line: el, character: ec )
6160
)
6261
},
6362
tabSize: buffer.tabWidth,
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)