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