Skip to content

Commit

Permalink
Add Request.sendAsync() function (#762)
Browse files Browse the repository at this point in the history
Set min macOS deployment target to macOS 12
  • Loading branch information
jpsim authored Feb 20, 2023
1 parent 0849eb7 commit 0d2585d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

##### Enhancements

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

##### Bug Fixes

Expand Down
13 changes: 13 additions & 0 deletions Source/SourceKittenFramework/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,19 @@ public enum Request {
/**
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
- returns: SourceKit output as a dictionary.
- throws: Request.Error on fail ()
*/
public func asyncSend() async throws -> [String: SourceKitRepresentable] {
initializeSourceKitFailable
let response = try await sourcekitObject.sendAsync()
defer { sourcekitd_response_dispose(response) }
return fromSourceKit(sourcekitd_response_get_value(response)) as! [String: SourceKitRepresentable]
}

/**
Sends the request to SourceKit and return the response as an [String: SourceKitRepresentable].
- returns: SourceKit output as a dictionary.
- throws: Request.Error on fail ()
*/
Expand Down
25 changes: 25 additions & 0 deletions Source/SourceKittenFramework/SourceKitObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ public final class SourceKitObject {
func sendSync() -> sourcekitd_response_t? {
return sourcekitd_send_request_sync(sourcekitdObject)
}

func sendAsync() async throws -> sourcekitd_response_t {
let handle = UnsafeMutablePointer<sourcekitd_request_handle_t?>.allocate(capacity: 1)

return try await withTaskCancellationHandler {
try await withUnsafeThrowingContinuation { continuation in
sourcekitd_send_request(sourcekitdObject, handle) { response in
enum SourceKitSendError: Error { case error, noResponse }

guard let response else {
continuation.resume(throwing: SourceKitSendError.noResponse)
return
}

if sourcekitd_response_is_error(response) {
continuation.resume(throwing: SourceKitSendError.error)
} else {
continuation.resume(returning: response)
}
}
}
} onCancel: {
sourcekitd_cancel_request(handle)
}
}
}

extension SourceKitObject: SourceKitObjectConvertible {
Expand Down
2 changes: 1 addition & 1 deletion SourceKittenFramework.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pod::Spec.new do |s|
s.source = { git: s.homepage + '.git', tag: s.version }
s.license = { type: 'MIT', file: 'LICENSE' }
s.author = { 'JP Simard' => '[email protected]' }
s.platform = :osx, '10.9'
s.platform = :osx, '12'
s.source_files = 'Source/Clang_C/include/*.h', 'Source/SourceKit/include/*.h', 'Source/SourceKittenFramework/*.swift'
s.swift_versions = ['5.3', '5.4', '5.5', '5.6', '5.7']
s.pod_target_xcconfig = { 'APPLICATION_EXTENSION_API_ONLY' => 'YES' }
Expand Down

0 comments on commit 0d2585d

Please sign in to comment.