Skip to content

Commit e615203

Browse files
authored
Merge pull request #29 from literalpie/find-symbols-by-name
Add ability to find symbols by name
2 parents 9e3c606 + 4450b67 commit e615203

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ public final class IndexStoreDB {
8080
return body(SymbolOccurrence(occur))
8181
}
8282
}
83+
84+
@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
85+
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
86+
return body(SymbolOccurrence(occur))
87+
}
88+
}
89+
90+
@discardableResult public func forEachCanonicalSymbolOccurrence(
91+
containing pattern: String,
92+
anchorStart: Bool,
93+
anchorEnd: Bool,
94+
subsequence: Bool,
95+
ignoreCase: Bool,
96+
body: @escaping (SymbolOccurrence) -> Bool
97+
) -> Bool {
98+
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
99+
impl,
100+
pattern,
101+
anchorStart,
102+
anchorEnd,
103+
subsequence,
104+
ignoreCase
105+
) { occur in
106+
body(SymbolOccurrence(occur))
107+
}
108+
}
109+
83110
}
84111

85112
public struct SymbolRole: OptionSet {

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ typedef _Nullable indexstoredb_indexstore_library_t(^indexstore_library_provider
8585
/// Returns true to continue.
8686
typedef bool(^indexstoredb_symbol_occurrence_receiver_t)(_Nonnull indexstoredb_symbol_occurrence_t);
8787

88+
/// Returns true to continue.
89+
typedef bool(^indexstoredb_symbol_name_receiver)(const char *_Nonnull);
90+
8891
INDEXSTOREDB_PUBLIC _Nullable
8992
indexstoredb_index_t
9093
indexstoredb_index_create(const char * _Nonnull storePath,
@@ -159,6 +162,45 @@ indexstoredb_error_get_description(_Nonnull indexstoredb_error_t);
159162
INDEXSTOREDB_PUBLIC void
160163
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);
161164

165+
/// Loops through each symbol in the index and calls the receiver function with each symbol.
166+
/// @param index An IndexStoreDB object which contains the symbols.
167+
/// @param receiver A function to be called for each symbol, the CString of the symbol will be passed in to this function.
168+
/// The function should return a boolean indicating whether the looping should continue.
169+
INDEXSTOREDB_PUBLIC bool
170+
indexstoredb_index_symbol_names(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_symbol_name_receiver);
171+
172+
/// Loops through each canonical symbol that matches the string and performs the passed in function.
173+
/// @param index An IndexStoreDB object which contains the symbols.
174+
/// @param symbolName The name of the symbol whose canonical occurence should be found.
175+
/// @param receiver A function to be called for each canonical occurence.
176+
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
177+
/// The function should return a boolean indicating whether the looping should continue.
178+
INDEXSTOREDB_PUBLIC bool
179+
indexstoredb_index_canonical_symbol_occurences_by_name(
180+
indexstoredb_index_t _Nonnull index,
181+
const char *_Nonnull symbolName,
182+
indexstoredb_symbol_occurrence_receiver_t _Nonnull receiver
183+
);
184+
185+
/// Loops through each canonical symbol that matches the pattern and performs the passed in function.
186+
/// @param index An IndexStoreDB object which contains the symbols.
187+
/// @param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
188+
/// @param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
189+
/// @param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
190+
/// @param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
191+
/// @param receiver A function to be called for each canonical occurence that matches the pattern.
192+
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
193+
/// The function should return a boolean indicating whether the looping should continue.
194+
INDEXSTOREDB_PUBLIC bool
195+
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
196+
_Nonnull indexstoredb_index_t index,
197+
const char *_Nonnull pattern,
198+
bool anchorStart,
199+
bool anchorEnd,
200+
bool subsequence,
201+
bool ignoreCase,
202+
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
203+
162204
INDEXSTOREDB_END_DECLS
163205

164206
#endif

lib/CIndexStoreDB/CIndexStoreDB.cpp

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

147+
bool
148+
indexstoredb_index_symbol_names(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
149+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
150+
return obj->value->foreachSymbolName([&](StringRef ref) -> bool {
151+
return receiver(ref.str().c_str());
152+
});
153+
}
154+
155+
bool
156+
indexstoredb_index_canonical_symbol_occurences_by_name(
157+
indexstoredb_index_t index,
158+
const char *_Nonnull symbolName,
159+
indexstoredb_symbol_occurrence_receiver_t receiver)
160+
{
161+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
162+
return obj->value->foreachCanonicalSymbolOccurrenceByName(symbolName, [&](SymbolOccurrenceRef occur) -> bool {
163+
return receiver(make_object(occur));
164+
});
165+
}
166+
167+
bool
168+
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
169+
indexstoredb_index_t index,
170+
const char *_Nonnull pattern,
171+
bool anchorStart,
172+
bool anchorEnd,
173+
bool subsequence,
174+
bool ignoreCase,
175+
indexstoredb_symbol_occurrence_receiver_t receiver)
176+
{
177+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
178+
return obj->value->foreachCanonicalSymbolOccurrenceContainingPattern(
179+
pattern,
180+
anchorStart,
181+
anchorEnd,
182+
subsequence,
183+
ignoreCase,
184+
[&](SymbolOccurrenceRef occur
185+
) -> bool {
186+
return receiver(make_object(occur));
187+
});
188+
}
189+
147190
indexstoredb_symbol_t
148191
indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
149192
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;

0 commit comments

Comments
 (0)