Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Sources/IndexStore/IndexStore.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation
import CIndexStore
import IndexStoreDB

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

public struct PathMapping {
/// Path prefix to be replaced, typically the canonical or hermetic path.
let original: String

/// Replacement path prefix, typically the path on the local machine.
let replacement: String

public init(original: String, replacement: String) {
self.original = original
self.replacement = replacement
}
}

public final class IndexStore {
fileprivate let store: indexstore_t

Expand All @@ -32,6 +46,34 @@ public final class IndexStore {
throw IndexStoreError(error!)
}
}

public init(
path: String,
prefixMappings: [PathMapping] = []
) throws {
let fullPath = (path as NSString).expandingTildeInPath

guard let cOptions = indexstore_creation_options_create() else {
throw fatalError("failed to create indexstore_creation_options_t")
}
defer { indexstore_creation_options_dispose(cOptions) }

for mapping in prefixMappings {
mapping.original.withCString { origCStr in
mapping.replacement.withCString { remappedCStr in
indexstore_creation_options_add_prefix_mapping(cOptions, origCStr, remappedCStr)
}
}
}

var error: indexstore_error_t? = nil
guard let store = indexstore_store_create_with_options(fullPath, cOptions, &error) else {
defer { if error != nil { indexstore_error_dispose(error!) } }
throw IndexStoreError(error!)
}

self.store = store
}

deinit {
indexstore_store_dispose(self.store)
Expand Down
Loading