Skip to content

Commit 1b97ba6

Browse files
committed
Add SymbolRelation class with roles and symbols
Add kind property to Symbol Add forEachRelation of occurrence function
1 parent e615203 commit 1b97ba6

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ public final class IndexStoreDB {
107107
}
108108
}
109109

110+
@discardableResult public func forEachRelation(
111+
of occurrence: SymbolOccurrence,
112+
body: @escaping (SymbolRelation) -> Bool
113+
) -> Bool {
114+
return indexstoredb_symbol_occurrence_relations(occurrence.value){ relation in
115+
body(SymbolRelation(relation))
116+
}
117+
}
118+
119+
}
120+
121+
public final class SymbolRelation {
122+
let value: indexstoredb_symbol_relation_t
123+
124+
public lazy var roles = SymbolRole(rawValue: indexstoredb_symbol_relation_get_roles(value))
125+
public lazy var symbol = Symbol(indexstoredb_symbol_relation_get_symbol(value))
126+
127+
public init(_ value: indexstoredb_symbol_relation_t) {
128+
self.value = value
129+
}
110130
}
111131

112132
public struct SymbolRole: OptionSet {
@@ -154,6 +174,7 @@ public final class Symbol {
154174

155175
public lazy var usr: String = String(cString: indexstoredb_symbol_usr(value))
156176
public lazy var name: String = String(cString: indexstoredb_symbol_name(value))
177+
public lazy var kind = indexstoredb_symbol_kind(value)
157178

158179
init(_ value: indexstoredb_symbol_t) {
159180
self.value = value
@@ -192,6 +213,7 @@ public final class SymbolOccurrence {
192213
public lazy var symbol: Symbol = Symbol(indexstoredb_symbol_occurrence_symbol(value))
193214
public lazy var roles: SymbolRole = SymbolRole(rawValue: indexstoredb_symbol_occurrence_roles(value))
194215
public lazy var location: SymbolLocation = SymbolLocation(indexstoredb_symbol_occurrence_location(value))
216+
public lazy var relations: [SymbolRelation] = getRelations()
195217

196218
init(_ value: indexstoredb_symbol_occurrence_t) {
197219
self.value = value
@@ -200,6 +222,15 @@ public final class SymbolOccurrence {
200222
deinit {
201223
indexstoredb_release(value)
202224
}
225+
226+
private func getRelations() -> [SymbolRelation] {
227+
var relations: [SymbolRelation] = []
228+
indexstoredb_symbol_occurrence_relations(value){ relation in
229+
relations.append(SymbolRelation(relation))
230+
return true
231+
}
232+
return relations
233+
}
203234
}
204235

205236
public protocol IndexStoreLibraryProvider {

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ typedef indexstoredb_object_t indexstoredb_symbol_occurrence_t;
5252

5353
typedef void *indexstoredb_error_t;
5454
typedef void *indexstoredb_symbol_location_t;
55+
typedef void *indexstoredb_symbol_relation_t;
56+
typedef void *indexstoredb_symbol_kind_t;
5557

5658
typedef enum {
5759
INDEXSTOREDB_SYMBOL_ROLE_DECLARATION = 1 << 0,
@@ -201,6 +203,30 @@ indexstoredb_index_canonical_symbol_occurences_containing_pattern(
201203
bool ignoreCase,
202204
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
203205

206+
/// Gets the set of roles of the passed in symbol relation
207+
/// @param relation A symbol relation
208+
INDEXSTOREDB_PUBLIC uint64_t
209+
indexstoredb_symbol_relation_get_roles(_Nonnull indexstoredb_symbol_relation_t);
210+
211+
/// Gets the symbol associated with the passed in relation
212+
/// @param relation A symbol relation
213+
INDEXSTOREDB_PUBLIC _Nonnull indexstoredb_symbol_t
214+
indexstoredb_symbol_relation_get_symbol(_Nonnull indexstoredb_symbol_relation_t);
215+
216+
/// Loops through each relation that a passed in symbol has, and performs the passed in function.
217+
/// The relations are owned by the occurrence and shall not be used after the occurrence is freed.
218+
/// @param occurrence The symbol occurrence that whose relations should be found.
219+
/// @param applier The function that should be performed on each symbol relation.
220+
/// The function should return a boolean indicating whether the looping should continue.
221+
INDEXSTOREDB_PUBLIC bool
222+
indexstoredb_symbol_occurrence_relations(_Nonnull indexstoredb_symbol_occurrence_t,
223+
bool(^ _Nonnull applier)(indexstoredb_symbol_relation_t _Nonnull ));
224+
225+
/// Get the SymbolKind of a Symbol
226+
/// @param symbol The symbol whose kind should be found.
227+
INDEXSTOREDB_PUBLIC uint8_t
228+
indexstoredb_symbol_kind(_Nonnull indexstoredb_symbol_t);
229+
204230
INDEXSTOREDB_END_DECLS
205231

206232
#endif

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
144144
return obj->value->getName().c_str();
145145
}
146146

147+
uint8_t
148+
indexstoredb_symbol_kind(indexstoredb_symbol_t symbol) {
149+
auto symbolObj = (IndexStoreDBObject<std::shared_ptr<Symbol>> *)symbol;
150+
return (uint8_t) symbolObj->value->getSymbolKind();
151+
}
152+
147153
bool
148154
indexstoredb_index_symbol_names(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
149155
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
@@ -193,6 +199,31 @@ indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
193199
return make_object(obj->value->getSymbol());
194200
}
195201

202+
uint64_t
203+
indexstoredb_symbol_relation_get_roles(indexstoredb_symbol_relation_t relation) {
204+
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
205+
return relationObj->value.getRoles().toRaw();
206+
}
207+
208+
indexstoredb_symbol_t
209+
indexstoredb_symbol_relation_get_symbol(indexstoredb_symbol_relation_t relation) {
210+
auto relationObj = (IndexStoreDBObject<SymbolRelation> *)relation;
211+
return make_object(relationObj->value.getSymbol());
212+
}
213+
214+
bool
215+
indexstoredb_symbol_occurrence_relations(indexstoredb_symbol_occurrence_t occurrence,
216+
bool(^applier)(indexstoredb_symbol_relation_t)) {
217+
auto occurrenceObj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occurrence;
218+
ArrayRef<SymbolRelation> relations = occurrenceObj->value->getRelations();
219+
for (SymbolRelation rel : relations) {
220+
if(!applier(make_object(rel))) {
221+
return false;
222+
}
223+
}
224+
return true;
225+
}
226+
196227
uint64_t
197228
indexstoredb_symbol_occurrence_roles(indexstoredb_symbol_occurrence_t occur) {
198229
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;

0 commit comments

Comments
 (0)