Skip to content

Commit 22bba1f

Browse files
committed
Expose symbol pattern matching to Swift
1 parent 313cc12 commit 22bba1f

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

Sources/IndexStoreDB/IndexStoreDB.swift

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,29 +81,45 @@ public final class IndexStoreDB {
8181
}
8282
}
8383

84-
public func foreachCanonicalSymbolOccurrenceByName(symbolName: String, body: @escaping (SymbolOccurrence) -> Bool) {
84+
public func forEachCanonicalSymbolOccurrenceByName(symbolName: String, body: @escaping (SymbolOccurrence) -> Bool) {
8585
indexstoredb_for_each_canonical_symbol_occurence_by_name(impl, symbolName) { (occur) -> Bool in
8686
body(SymbolOccurrence(occur))
8787
}
8888
}
8989

90-
public func findSymbols(matching query: String) -> [SymbolOccurrence] {
91-
var symbolNameResults: [String] = []
92-
indexstoredb_for_each_symbol_name(impl) { pointer in
93-
let str = String(cString: pointer)
94-
if str.range(of: query) != nil {
95-
symbolNameResults.append(str)
96-
}
97-
return true
90+
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+
){
98+
indexstoredb_for_each_canonical_symbol_occurence_containing_pattern(
99+
impl,
100+
pattern,
101+
anchorStart,
102+
anchorEnd,
103+
subsequence,
104+
ignoreCase
105+
) { occur in
106+
body(SymbolOccurrence(occur))
98107
}
108+
}
109+
110+
public func findSymbols(matching query: String) -> [SymbolOccurrence] {
99111
var symbolOccurenceResults: [SymbolOccurrence] = []
100-
symbolNameResults.forEach { (symbolName) in
101-
foreachCanonicalSymbolOccurrenceByName(symbolName: symbolName) {occurence in
102-
if !occurence.location.isSystem {
103-
symbolOccurenceResults.append(occurence)
104-
}
105-
return true
112+
forEachCanonicalSymbolOccurrence(
113+
containing: query,
114+
anchorStart: false,
115+
anchorEnd: false,
116+
subsequence: true,
117+
ignoreCase: true
118+
) { (occurence) -> Bool in
119+
if !occurence.location.isSystem {
120+
symbolOccurenceResults.append(occurence)
106121
}
122+
return true
107123
}
108124
return symbolOccurenceResults
109125
}

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typedef _Nullable indexstoredb_indexstore_library_t(^indexstore_library_provider
8686
typedef bool(^indexstoredb_symbol_occurrence_receiver_t)(_Nonnull indexstoredb_symbol_occurrence_t);
8787

8888
/// Returns true to continue.
89-
typedef bool(^indexstoredb_for_each_symbol_receiver)(const char *_Nonnull);
89+
typedef bool(^indexstoredb_symbol_name_receiver)(const char *_Nonnull);
9090

9191
INDEXSTOREDB_PUBLIC _Nullable
9292
indexstoredb_index_t
@@ -163,7 +163,18 @@ INDEXSTOREDB_PUBLIC void
163163
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);
164164

165165
INDEXSTOREDB_PUBLIC bool
166-
indexstoredb_for_each_symbol_name(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_for_each_symbol_receiver);
166+
indexstoredb_for_each_symbol_name(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_symbol_name_receiver);
167+
168+
INDEXSTOREDB_PUBLIC bool
169+
indexstoredb_for_each_canonical_symbol_occurence_containing_pattern(
170+
_Nonnull indexstoredb_index_t index,
171+
const char *_Nonnull Pattern,
172+
bool AnchorStart,
173+
bool AnchorEnd,
174+
bool Subsequence,
175+
bool IgnoreCase,
176+
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);
177+
167178

168179
INDEXSTOREDB_PUBLIC bool
169180
indexstoredb_for_each_canonical_symbol_occurence_by_name(

lib/CIndexStoreDB/CIndexStoreDB.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
149149
/// @param receiver a function to be called for each symbol, the CString of the symbol will be passed in to this function.
150150
/// The function should return a boolean indicating whether the looping should continue.
151151
bool
152-
indexstoredb_for_each_symbol_name(indexstoredb_index_t index, indexstoredb_for_each_symbol_receiver receiver) {
152+
indexstoredb_for_each_symbol_name(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
153153
// indexSystem has foreachsymbolName.
154154
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
155155
return obj->value->foreachSymbolName([&](StringRef ref) -> bool {
@@ -175,6 +175,38 @@ indexstoredb_for_each_canonical_symbol_occurence_by_name(
175175
});
176176
}
177177

178+
/// loops through each canonical symbol that matches the pattern, perform the passed in function
179+
/// @param index an IndexStoreDB object which contains the symbols.
180+
/// @param AnchorStart when true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
181+
/// @param AnchorEnd when true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
182+
/// @param Subsequence when true, symbols will be matched even if the pattern is not matched contiguously.
183+
/// @param IgnoreCase when true, symbols may be returned even if the case of letters does not match the pattern.
184+
/// @param receiver a function to be called for each canonical occurence that matches the pattern.
185+
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
186+
/// The function should return a boolean indicating whether the looping should continue.
187+
bool
188+
indexstoredb_for_each_canonical_symbol_occurence_containing_pattern(
189+
indexstoredb_index_t index,
190+
const char *_Nonnull Pattern,
191+
bool AnchorStart,
192+
bool AnchorEnd,
193+
bool Subsequence,
194+
bool IgnoreCase,
195+
indexstoredb_symbol_occurrence_receiver_t receiver)
196+
{
197+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
198+
return obj->value->foreachCanonicalSymbolOccurrenceContainingPattern(
199+
Pattern,
200+
AnchorStart,
201+
AnchorEnd,
202+
Subsequence,
203+
IgnoreCase,
204+
[&](SymbolOccurrenceRef occur
205+
) -> bool {
206+
return receiver(make_object(occur));
207+
});
208+
}
209+
178210
indexstoredb_symbol_t
179211
indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
180212
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;

0 commit comments

Comments
 (0)