Skip to content

Commit 4411aba

Browse files
committed
support set IndexStoreDB prefixMappings
Signed-off-by: Kila2 <[email protected]>
1 parent 9cd7ac9 commit 4411aba

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Sources/IndexStore/IndexStore.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Foundation
22
import CIndexStore
3+
import IndexStoreDB
34

45
// About the use of `*_apply_f` functions.
56
//
@@ -20,6 +21,19 @@ import CIndexStore
2021
// 2. The context is passed by pointer to the `apply_f` function
2122
// 3. The context is unpacked using `assumingMemoryBound(to: Context.self).pointee`
2223

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+
2337
public final class IndexStore {
2438
fileprivate let store: indexstore_t
2539

@@ -32,6 +46,34 @@ public final class IndexStore {
3246
throw IndexStoreError(error!)
3347
}
3448
}
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+
}
3577

3678
deinit {
3779
indexstore_store_dispose(self.store)

0 commit comments

Comments
 (0)