Skip to content

Commit 0d2585d

Browse files
authored
Add Request.sendAsync() function (jpsim#762)
Set min macOS deployment target to macOS 12
1 parent 0849eb7 commit 0d2585d

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
##### Enhancements
88

9-
* None.
9+
* Add `Request.sendAsync()` function.
10+
[JP Simard](https://github.com/jpsim)
11+
[#759](https://github.com/jpsim/SourceKitten/issues/759)
1012

1113
##### Bug Fixes
1214

Source/SourceKittenFramework/Request.swift

+13
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ public enum Request {
333333
/**
334334
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
335335

336+
- returns: SourceKit output as a dictionary.
337+
- throws: Request.Error on fail ()
338+
*/
339+
public func asyncSend() async throws -> [String: SourceKitRepresentable] {
340+
initializeSourceKitFailable
341+
let response = try await sourcekitObject.sendAsync()
342+
defer { sourcekitd_response_dispose(response) }
343+
return fromSourceKit(sourcekitd_response_get_value(response)) as! [String: SourceKitRepresentable]
344+
}
345+
346+
/**
347+
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
348+
336349
- returns: SourceKit output as a dictionary.
337350
- throws: Request.Error on fail ()
338351
*/

Source/SourceKittenFramework/SourceKitObject.swift

+25
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,31 @@ public final class SourceKitObject {
100100
func sendSync() -> sourcekitd_response_t? {
101101
return sourcekitd_send_request_sync(sourcekitdObject)
102102
}
103+
104+
func sendAsync() async throws -> sourcekitd_response_t {
105+
let handle = UnsafeMutablePointer<sourcekitd_request_handle_t?>.allocate(capacity: 1)
106+
107+
return try await withTaskCancellationHandler {
108+
try await withUnsafeThrowingContinuation { continuation in
109+
sourcekitd_send_request(sourcekitdObject, handle) { response in
110+
enum SourceKitSendError: Error { case error, noResponse }
111+
112+
guard let response else {
113+
continuation.resume(throwing: SourceKitSendError.noResponse)
114+
return
115+
}
116+
117+
if sourcekitd_response_is_error(response) {
118+
continuation.resume(throwing: SourceKitSendError.error)
119+
} else {
120+
continuation.resume(returning: response)
121+
}
122+
}
123+
}
124+
} onCancel: {
125+
sourcekitd_cancel_request(handle)
126+
}
127+
}
103128
}
104129

105130
extension SourceKitObject: SourceKitObjectConvertible {

SourceKittenFramework.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Pod::Spec.new do |s|
66
s.source = { git: s.homepage + '.git', tag: s.version }
77
s.license = { type: 'MIT', file: 'LICENSE' }
88
s.author = { 'JP Simard' => '[email protected]' }
9-
s.platform = :osx, '10.9'
9+
s.platform = :osx, '12'
1010
s.source_files = 'Source/Clang_C/include/*.h', 'Source/SourceKit/include/*.h', 'Source/SourceKittenFramework/*.swift'
1111
s.swift_versions = ['5.3', '5.4', '5.5', '5.6', '5.7']
1212
s.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES' }

0 commit comments

Comments
 (0)