Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ public final class JSONRPCConnection: Connection {
/// - parameter receiveHandler: The message handler to invoke for requests received on the `inFD`.
///
/// - Important: `start` must be called before sending any data over the `JSONRPCConnection`.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
public func start(
receiveHandler: MessageHandler,
closeHandler: @escaping @Sendable () async -> Void = {}
closeHandler: nonisolated(nonsending) @escaping @Sendable () async -> Void = {}
) {
queue.sync {
precondition(state == .created)
Expand Down
16 changes: 12 additions & 4 deletions Sources/ToolsProtocolsSwiftExtensions/AsyncQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,13 @@ public final class AsyncQueue<TaskMetadata: DependencyTracker>: Sendable {
/// If this is a barrier, all previously scheduled tasks are guaranteed to
/// finish execution before the barrier is executed and all tasks that are
/// added later will wait until the barrier finishes execution.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
@discardableResult
public func async<Success: Sendable>(
priority: TaskPriority? = nil,
metadata: TaskMetadata,
@_inheritActorContext operation: @escaping @Sendable () async -> Success
@_inheritActorContext operation: nonisolated(nonsending) @escaping @Sendable () async -> Success
) -> Task<Success, Never> {
let throwingTask = asyncThrowing(priority: priority, metadata: metadata, operation: operation)
return Task(priority: priority) {
Expand All @@ -111,10 +113,12 @@ public final class AsyncQueue<TaskMetadata: DependencyTracker>: Sendable {
///
/// - Important: The caller is responsible for handling any errors thrown from
/// the operation by awaiting the result of the returned task.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
public func asyncThrowing<Success: Sendable>(
priority: TaskPriority? = nil,
metadata: TaskMetadata,
@_inheritActorContext operation: @escaping @Sendable () async throws -> Success
@_inheritActorContext operation: nonisolated(nonsending) @escaping @Sendable () async throws -> Success
) -> Task<Success, any Error> {
let id = UUID()

Expand Down Expand Up @@ -175,19 +179,23 @@ public final class AsyncQueue<TaskMetadata: DependencyTracker>: Sendable {
extension AsyncQueue where TaskMetadata == Serial {
/// Same as ``async(priority:operation:)`` but specialized for serial queues
/// that don't specify any metadata.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
@discardableResult
public func async<Success: Sendable>(
priority: TaskPriority? = nil,
@_inheritActorContext operation: @escaping @Sendable () async -> Success
@_inheritActorContext operation: nonisolated(nonsending) @escaping @Sendable () async -> Success
) -> Task<Success, Never> {
return self.async(priority: priority, metadata: Serial(), operation: operation)
}

/// Same as ``asyncThrowing(priority:metadata:operation:)`` but specialized
/// for serial queues that don't specify any metadata.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
public func asyncThrowing<Success: Sendable>(
priority: TaskPriority? = nil,
@_inheritActorContext operation: @escaping @Sendable () async throws -> Success
@_inheritActorContext operation: nonisolated(nonsending) @escaping @Sendable () async throws -> Success
) -> Task<Success, any Error> {
return self.asyncThrowing(priority: priority, metadata: Serial(), operation: operation)
}
Expand Down
8 changes: 6 additions & 2 deletions Sources/ToolsProtocolsSwiftExtensions/AsyncUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,11 @@ extension Task where Failure == Never {

extension Collection where Self: Sendable, Element: Sendable {
/// Transforms all elements in the collection concurrently and returns the transformed collection.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
@_spi(SourceKitLSP) public func concurrentMap<TransformedElement: Sendable>(
maxConcurrentTasks: Int = ProcessInfo.processInfo.activeProcessorCount,
_ transform: @escaping @Sendable (Element) async -> TransformedElement
_ transform: nonisolated(nonsending) @escaping @Sendable (Element) async -> TransformedElement
) async -> [TransformedElement] {
let indexedResults = await withTaskGroup(of: (index: Int, element: TransformedElement).self) { taskGroup in
var indexedResults: [(index: Int, element: TransformedElement)] = []
Expand Down Expand Up @@ -170,7 +172,9 @@ extension Collection where Self: Sendable, Element: Sendable {
}

/// Invoke `body` for every element in the collection and wait for all calls of `body` to finish
@_spi(SourceKitLSP) public func concurrentForEach(_ body: @escaping @Sendable (Element) async -> Void) async {
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
@_spi(SourceKitLSP) public func concurrentForEach(_ body: nonisolated(nonsending) @escaping @Sendable (Element) async -> Void) async {
await withTaskGroup(of: Void.self) { taskGroup in
for element in self {
taskGroup.addTask {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
/// `pollingInterval`.
/// The function assumes that the original priority of the task is `initialPriority`. If the task priority changed
/// compared to `initialPriority`, the `taskPriorityChanged` will be called.
// Workaround formatter issue: https://github.com/swiftlang/swift-format/issues/1081
// swift-format-ignore
@_spi(SourceKitLSP) public func withTaskPriorityChangedHandler<T: Sendable>(
initialPriority: TaskPriority = Task.currentPriority,
pollingInterval: Duration = .seconds(0.1),
@_inheritActorContext operation: @escaping @Sendable () async throws -> T,
@_inheritActorContext operation: nonisolated(nonsending) @escaping @Sendable () async throws -> T,
taskPriorityChanged: @escaping @Sendable () -> Void
) async throws -> T {
let lastPriority = ThreadSafeBox(initialValue: initialPriority)
Expand Down