1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
//===--------------- SourceKitdRequest.swift ------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This file provides a convenient way to build a sourcekitd request.
//===----------------------------------------------------------------------===//
import sourcekitd
public struct SourceKitdRequest: CustomStringConvertible {
public class Dictionary: CustomStringConvertible {
let dict: sourcekitd_object_t
public init() {
dict = sourcekitd_request_dictionary_create(nil, nil, 0)
}
deinit {
sourcekitd_request_release(UnsafeMutableRawPointer(dict))
}
public func add(_ key: SourceKitdUID, value: String) {
sourcekitd_request_dictionary_set_string(dict, key.uid, value)
}
public func add(_ key: SourceKitdUID, value: Int) {
sourcekitd_request_dictionary_set_int64(dict, key.uid, Int64(value))
}
public func add(_ key: SourceKitdUID, value: SourceKitdUID) {
sourcekitd_request_dictionary_set_uid(dict, key.uid, value.uid)
}
public func add(_ key: SourceKitdUID, value: Array) {
sourcekitd_request_dictionary_set_value(dict, key.uid, value.arr)
}
public func add(_ key: SourceKitdUID, value: Dictionary) {
sourcekitd_request_dictionary_set_value(dict, key.uid, value.dict)
}
public func add(_ key: SourceKitdUID, value: Bool) {
sourcekitd_request_dictionary_set_int64(dict, key.uid, value ? 1 : 0)
}
public var description: String {
let utf8Str = sourcekitd_request_description_copy(dict)!
let result = String(cString: utf8Str)
free(utf8Str)
return result
}
}
public class Array: CustomStringConvertible {
let arr: sourcekitd_object_t
private let Append: Int = -1
public init() {
arr = sourcekitd_request_array_create(nil, 0)
}
deinit {
sourcekitd_request_release(arr)
}
public func add(_ value: String) {
sourcekitd_request_array_set_string(arr, Append, value)
}
public func add(_ value: Int) {
sourcekitd_request_array_set_int64(arr, Append, Int64(value))
}
public func add(_ value: SourceKitdUID) {
sourcekitd_request_array_set_uid(arr, Append, value.uid)
}
public func add(_ value: Dictionary) {
sourcekitd_request_array_set_value(arr, Append, value.dict)
}
public var description: String {
let utf8Str = sourcekitd_request_description_copy(arr)!
let result = String(cString: utf8Str)
free(utf8Str)
return result
}
}
private let req = Dictionary()
public init(uid: SourceKitdUID) {
req.add(SourceKitdUID.key_Request, value: uid)
}
public func addParameter(_ key: SourceKitdUID, value: String) {
req.add(key, value: value)
}
public func addParameter(_ key: SourceKitdUID, value: Int) {
req.add(key, value: value)
}
public func addParameter(_ key: SourceKitdUID, value: SourceKitdUID) {
req.add(key, value: value)
}
public func addArrayParameter(_ key: SourceKitdUID) -> Array {
let arr = Array()
req.add(key, value: arr)
return arr
}
public func addDictionaryParameter(_ key: SourceKitdUID) -> Dictionary {
let dict = Dictionary()
req.add(key, value: dict)
return dict
}
public var description: String {
return req.description
}
public var rawRequest: sourcekitd_object_t {
return req.dict
}
public func addCompilerArgs(_ compilerArguments: [String]?) {
let args = self.addArrayParameter(SourceKitdUID.key_CompilerArgs)
if let compilerArguments = compilerArguments {
for argument in compilerArguments {
args.add(argument)
}
}
}
}
|