<commit_before>//
//  Request.swift
//  SwiftSourceKit
//

import Foundation
import sourcekitd

public enum RequestValue {
    case UID(sourcekitd_uid_t)
    case Str(String)
    case Integer(Int)
    case Boolean(Bool)
    case Array([RequestValue])
}

extension RequestValue {
    private var sourcekitObject: sourcekitd_object_t {
        switch self {
        case UID(let value): return sourcekitd_request_uid_create(value)
        case Str(let value):
            return value.withCString {
                return sourcekitd_request_string_create($0)
            }
        case Integer(let value): return sourcekitd_request_int64_create(Int64(value))
        case Boolean(let value): return sourcekitd_request_int64_create(value ? 1 : 0)
        case Array(let values):
            let objects = values.map { $0.sourcekitObject }
            let result = objects.withUnsafeBufferPointer {
                return sourcekitd_request_array_create($0.baseAddress, objects.count)
            }
            for i in objects {
                sourcekitd_request_release(i)
            }
            return result
        }
    }
}
    
public final class Request {
    private let request: sourcekitd_object_t
    
    public init(dictionary: [sourcekitd_uid_t : RequestValue], compilerArgs: [String] = []) {
        request = sourcekitd_request_dictionary_create(nil, nil, 0)
        for (key, value) in dictionary {
            let object = value.sourcekitObject
            sourcekitd_request_dictionary_set_value(request, key, object)
            sourcekitd_request_release(object)
        }
        if !compilerArgs.isEmpty {
            let object = RequestValue.Array(compilerArgs.map { RequestValue.Str($0) }).sourcekitObject
            sourcekitd_request_dictionary_set_value(request, KeyCompilerArgs, object)
            sourcekitd_request_release(object)
        }
    }
    
    deinit {
        sourcekitd_request_release(request)
    }
    
    public var description: String {
        guard let str = String.fromCString(sourcekitd_request_description_copy(request)) else {
            return ""
        }
        return str
    }
    
    public func sendAndWaitForResponse() throws -> Response {
        let response = sourcekitd_send_request_sync(request)
        if sourcekitd_response_is_error(response) {
            throw ResponseError(response: response)
        }
        return Response(response: response)
    }

    public func send(errorCallback: (ResponseError) -> (), responseCallback: (Response) -> ()) {
        sourcekitd_send_request(request, nil) {
            (response) in
            if sourcekitd_response_is_error(response) {
                errorCallback(ResponseError(response: response))
            }
            responseCallback(Response(response: response))
        }
    }
}<commit_msg>Fix response callback for the async request send method<commit_after>//
//  Request.swift
//  SwiftSourceKit
//

import Foundation
import sourcekitd

public enum RequestValue {
    case UID(sourcekitd_uid_t)
    case Str(String)
    case Integer(Int)
    case Boolean(Bool)
    case Array([RequestValue])
}

extension RequestValue {
    private var sourcekitObject: sourcekitd_object_t {
        switch self {
        case UID(let value): return sourcekitd_request_uid_create(value)
        case Str(let value):
            return value.withCString {
                return sourcekitd_request_string_create($0)
            }
        case Integer(let value): return sourcekitd_request_int64_create(Int64(value))
        case Boolean(let value): return sourcekitd_request_int64_create(value ? 1 : 0)
        case Array(let values):
            let objects = values.map { $0.sourcekitObject }
            let result = objects.withUnsafeBufferPointer {
                return sourcekitd_request_array_create($0.baseAddress, objects.count)
            }
            for i in objects {
                sourcekitd_request_release(i)
            }
            return result
        }
    }
}
    
public final class Request {
    private let request: sourcekitd_object_t
    
    public init(dictionary: [sourcekitd_uid_t : RequestValue], compilerArgs: [String] = []) {
        request = sourcekitd_request_dictionary_create(nil, nil, 0)
        for (key, value) in dictionary {
            let object = value.sourcekitObject
            sourcekitd_request_dictionary_set_value(request, key, object)
            sourcekitd_request_release(object)
        }
        if !compilerArgs.isEmpty {
            let object = RequestValue.Array(compilerArgs.map { RequestValue.Str($0) }).sourcekitObject
            sourcekitd_request_dictionary_set_value(request, KeyCompilerArgs, object)
            sourcekitd_request_release(object)
        }
    }
    
    deinit {
        sourcekitd_request_release(request)
    }
    
    public var description: String {
        guard let str = String.fromCString(sourcekitd_request_description_copy(request)) else {
            return ""
        }
        return str
    }
    
    public func sendAndWaitForResponse() throws -> Response {
        let response = sourcekitd_send_request_sync(request)
        if sourcekitd_response_is_error(response) {
            throw ResponseError(response: response)
        }
        return Response(response: response)
    }

    public func send(errorCallback: (ResponseError) -> (), responseCallback: (Response) -> ()) {
        sourcekitd_send_request(request, nil) {
            (response) in
            if sourcekitd_response_is_error(response) {
                errorCallback(ResponseError(response: response))
            } else {
                responseCallback(Response(response: response))
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.07642645262606874, 'fraction_numerical': 0.004536730064561159, 'mean_word_length': 2.4038004750593824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6172886', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1532, 'n_words': 327}