Skip to content

Commit f155ee7

Browse files
author
Nathan Hawes
committed
[SourceKit] Expose the data variant sourcekitd apis in the SwiftLang wrapper
These were recently added to support returning the SyntaxTree in the bytetree from SourceKit but were never added in SwiftLang (the Swift layer wrapping SourceKit).
1 parent 4612f07 commit f155ee7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

tools/SourceKit/tools/swift-lang/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if(NOT SWIFT_SOURCEKIT_USE_INPROC_LIBRARY AND SWIFT_BUILD_STDLIB)
1313
UIDs.swift.gyb
1414

1515
DEPENDS ${DEPENDS_LIST}
16-
SWIFT_MODULE_DEPENDS_OSX Darwin
16+
SWIFT_MODULE_DEPENDS_OSX Darwin Foundation
1717
PRIVATE_LINK_LIBRARIES ${SOURCEKITD_LINK_LIBS}
1818
SWIFT_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
1919
INSTALL_IN_COMPONENT ${INSTALLED_COMP}

tools/SourceKit/tools/swift-lang/SourceKitdResponse.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// This file provides convenient APIs to interpret a SourceKitd response.
1313
//===----------------------------------------------------------------------===//
1414

15+
import Foundation
1516
import sourcekitd
1617

1718
public class SourceKitdResponse: CustomStringConvertible {
@@ -60,6 +61,15 @@ public class SourceKitdResponse: CustomStringConvertible {
6061
return Dictionary(dict: value, context: context)
6162
}
6263

64+
public func getData(_ key: SourceKitdUID) -> Data {
65+
let value = sourcekitd_variant_dictionary_get_value(dict, key.uid)
66+
let size = sourcekitd_variant_data_get_size(value)
67+
guard let ptr = sourcekitd_variant_data_get_ptr(value), size > 0 else {
68+
return Data()
69+
}
70+
return Data(bytes: ptr, count: size)
71+
}
72+
6373
public func getOptional(_ key: SourceKitdUID) -> Variant? {
6474
let value = sourcekitd_variant_dictionary_get_value(dict, key.uid)
6575
if sourcekitd_variant_get_type(value).rawValue ==
@@ -188,6 +198,14 @@ public class SourceKitdResponse: CustomStringConvertible {
188198
return Dictionary(dict: val, context: context)
189199
}
190200

201+
public func getData() -> Data {
202+
let size = sourcekitd_variant_data_get_size(val)
203+
guard let ptr = sourcekitd_variant_data_get_ptr(val), size > 0 else {
204+
return Data()
205+
}
206+
return Data(bytes: ptr, count: size)
207+
}
208+
191209
public var description: String {
192210
return val.description
193211
}

0 commit comments

Comments
 (0)