Skip to content

Commit 0c569fb

Browse files
authored
support set IndexStoreDB prefixMappings (#68)
Signed-off-by: Kila2 <[email protected]>
1 parent 9cd7ac9 commit 0c569fb

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Sources/IndexStore/IndexStore.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ import CIndexStore
2020
// 2. The context is passed by pointer to the `apply_f` function
2121
// 3. The context is unpacked using `assumingMemoryBound(to: Context.self).pointee`
2222

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+
2336
public final class IndexStore {
2437
fileprivate let store: indexstore_t
2538

@@ -32,6 +45,32 @@ public final class IndexStore {
3245
throw IndexStoreError(error!)
3346
}
3447
}
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+
}
3574

3675
deinit {
3776
indexstore_store_dispose(self.store)

0 commit comments

Comments
 (0)