@@ -200,19 +200,19 @@ extension SourceKitVariant {
200
200
switch sourcekitd_variant_get_type ( sourcekitObject) {
201
201
case SOURCEKITD_VARIANT_TYPE_ARRAY:
202
202
var array = [ SourceKitVariant] ( )
203
- _ = sourcekitd_variant_array_apply ( sourcekitObject) { index, value in
203
+ _ = __sourcekitd_variant_array_apply ( sourcekitObject) { index, value in
204
204
array. insert ( SourceKitVariant ( variant: value, response: response) , at: Int ( index) )
205
205
return true
206
206
}
207
207
self = . array( array)
208
208
case SOURCEKITD_VARIANT_TYPE_DICTIONARY:
209
209
var count : Int = 0
210
- _ = sourcekitd_variant_dictionary_apply ( sourcekitObject) { _, _ in
210
+ _ = __sourcekitd_variant_dictionary_apply ( sourcekitObject) { _, _ in
211
211
count += 1
212
212
return true
213
213
}
214
214
var dictionary = [ String: SourceKitVariant] ( minimumCapacity: count)
215
- _ = sourcekitd_variant_dictionary_apply ( sourcekitObject) { key, value in
215
+ _ = __sourcekitd_variant_dictionary_apply ( sourcekitObject) { key, value in
216
216
if let key = String ( sourceKitUID: key!) {
217
217
dictionary [ key] = SourceKitVariant ( variant: value, response: response)
218
218
}
@@ -445,3 +445,40 @@ extension SourceKitVariant {
445
445
var attributes : [ SourceKitVariant ] ? { return self [ " key.attributes " ] ? . array }
446
446
var attribute : SourceKitVariant ? { return self [ " key.attribute " ] }
447
447
}
448
+
449
+ // MARK: - sourcekitd_variant_*_apply
450
+ // It is hard to pass multiple Swift objects in context parameter on calling
451
+ // sourcekitd's `*_apply_f` functions.
452
+ // So, I added `*_apply` compatible functions that passing Swift closure as
453
+ // context and calling them in C function.
454
+ func __sourcekitd_variant_array_apply(
455
+ _ array: sourcekitd_variant_t ,
456
+ _ applier: @escaping ( Int , sourcekitd_variant_t ) -> Bool ) -> Bool {
457
+ typealias array_applier = ( Int , sourcekitd_variant_t ) -> Bool
458
+ var applier = applier
459
+ return withUnsafeMutablePointer ( to: & applier) { context in
460
+ sourcekitd_variant_array_apply_f ( array, { index, value, context in
461
+ if let context = context {
462
+ let applier = context. assumingMemoryBound ( to: array_applier. self) . pointee
463
+ return applier ( index, value)
464
+ }
465
+ return true
466
+ } , context)
467
+ }
468
+ }
469
+
470
+ func __sourcekitd_variant_dictionary_apply(
471
+ _ dict: sourcekitd_variant_t ,
472
+ _ applier: @escaping ( sourcekitd_uid_t ? , sourcekitd_variant_t ) -> Bool ) -> Bool {
473
+ typealias dictionary_applier = ( sourcekitd_uid_t ? , sourcekitd_variant_t ) -> Bool
474
+ var applier = applier
475
+ return withUnsafeMutablePointer ( to: & applier) { context in
476
+ sourcekitd_variant_dictionary_apply_f ( dict, { key, value, context in
477
+ if let context = context {
478
+ let applier = context. assumingMemoryBound ( to: dictionary_applier. self) . pointee
479
+ return applier ( key, value)
480
+ }
481
+ return true
482
+ } , context)
483
+ }
484
+ }
0 commit comments