1
1
import Foundation
2
2
import CIndexStore
3
+ import IndexStoreDB
3
4
4
5
// About the use of `*_apply_f` functions.
5
6
//
@@ -20,6 +21,19 @@ import CIndexStore
20
21
// 2. The context is passed by pointer to the `apply_f` function
21
22
// 3. The context is unpacked using `assumingMemoryBound(to: Context.self).pointee`
22
23
24
+ public struct PathMapping {
25
+ /// Path prefix to be replaced, typically the canonical or hermetic path.
26
+ let original : String
27
+
28
+ /// Replacement path prefix, typically the path on the local machine.
29
+ let replacement : String
30
+
31
+ public init ( original: String , replacement: String ) {
32
+ self . original = original
33
+ self . replacement = replacement
34
+ }
35
+ }
36
+
23
37
public final class IndexStore {
24
38
fileprivate let store : indexstore_t
25
39
@@ -32,6 +46,34 @@ public final class IndexStore {
32
46
throw IndexStoreError ( error!)
33
47
}
34
48
}
49
+
50
+ public init (
51
+ path: String ,
52
+ prefixMappings: [ PathMapping ] = [ ]
53
+ ) throws {
54
+ let fullPath = ( path as NSString ) . expandingTildeInPath
55
+
56
+ guard let cOptions = indexstore_creation_options_create ( ) else {
57
+ throw fatalError ( " failed to create indexstore_creation_options_t " )
58
+ }
59
+ defer { indexstore_creation_options_dispose ( cOptions) }
60
+
61
+ for mapping in prefixMappings {
62
+ mapping. original. withCString { origCStr in
63
+ mapping. replacement. withCString { remappedCStr in
64
+ indexstore_creation_options_add_prefix_mapping ( cOptions, origCStr, remappedCStr)
65
+ }
66
+ }
67
+ }
68
+
69
+ var error : indexstore_error_t ? = nil
70
+ guard let store = indexstore_store_create_with_options ( fullPath, cOptions, & error) else {
71
+ defer { if error != nil { indexstore_error_dispose ( error!) } }
72
+ throw IndexStoreError ( error!)
73
+ }
74
+
75
+ self . store = store
76
+ }
35
77
36
78
deinit {
37
79
indexstore_store_dispose ( self . store)
0 commit comments