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
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import Csourcekitd
import LSPLogging
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(CRT)
import CRT
#elseif canImport(Bionic)
import Bionic
#endif
/// Values that can be stored in a `SKDRequestDictionary`.
///
/// - Warning: `SKDRequestDictionary.subscript` and `SKDRequestArray.append`
/// switch exhaustively over this protocol.
/// Do not add new conformances without adding a new case in the subscript and
/// `append` function.
public protocol SKDRequestValue {}
extension String: SKDRequestValue {}
extension Int: SKDRequestValue {}
extension sourcekitd_api_uid_t: SKDRequestValue {}
extension SKDRequestDictionary: SKDRequestValue {}
extension SKDRequestArray: SKDRequestValue {}
extension Array<SKDRequestValue>: SKDRequestValue {}
extension Dictionary<sourcekitd_api_uid_t, SKDRequestValue>: SKDRequestValue {}
extension Optional: SKDRequestValue where Wrapped: SKDRequestValue {}
extension SourceKitD {
/// Create a `SKDRequestDictionary` from the given dictionary.
public func dictionary(_ dict: [sourcekitd_api_uid_t: SKDRequestValue]) -> SKDRequestDictionary {
let result = SKDRequestDictionary(sourcekitd: self)
for (key, value) in dict {
result.set(key, to: value)
}
return result
}
}
public final class SKDRequestDictionary: Sendable {
nonisolated(unsafe) let dict: sourcekitd_api_object_t
private let sourcekitd: SourceKitD
public init(_ dict: sourcekitd_api_object_t? = nil, sourcekitd: SourceKitD) {
self.dict = dict ?? sourcekitd.api.request_dictionary_create(nil, nil, 0)!
self.sourcekitd = sourcekitd
}
deinit {
sourcekitd.api.request_release(dict)
}
public func set(_ key: sourcekitd_api_uid_t, to newValue: SKDRequestValue) {
switch newValue {
case let newValue as String:
sourcekitd.api.request_dictionary_set_string(dict, key, newValue)
case let newValue as Int:
sourcekitd.api.request_dictionary_set_int64(dict, key, Int64(newValue))
case let newValue as sourcekitd_api_uid_t:
sourcekitd.api.request_dictionary_set_uid(dict, key, newValue)
case let newValue as SKDRequestDictionary:
sourcekitd.api.request_dictionary_set_value(dict, key, newValue.dict)
case let newValue as SKDRequestArray:
sourcekitd.api.request_dictionary_set_value(dict, key, newValue.array)
case let newValue as Array<SKDRequestValue>:
self.set(key, to: sourcekitd.array(newValue))
case let newValue as Dictionary<sourcekitd_api_uid_t, SKDRequestValue>:
self.set(key, to: sourcekitd.dictionary(newValue))
case let newValue as Optional<SKDRequestValue>:
if let newValue {
self.set(key, to: newValue)
}
default:
preconditionFailure("Unknown type conforming to SKDRequestValue")
}
}
}
extension SKDRequestDictionary: CustomStringConvertible {
public var description: String {
let ptr = sourcekitd.api.request_description_copy(dict)!
defer { free(ptr) }
return String(cString: ptr)
}
}
extension SKDRequestDictionary: CustomLogStringConvertible {
public var redactedDescription: String {
// FIXME: (logging) Implement a better redacted log that contains keys,
// number of elements in an array but not the data itself.
return "<\(description.filter(\.isNewline).count) lines>"
}
}
|