Skip to content

Commit 313cc12

Browse files
committed
Add ability to find symbols by name
This can be used in lsp projects to find all symbols in a workspace that match a string. https://bugs.swift.org/browse/SR-10806
1 parent 9e3c606 commit 313cc12

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-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+
public func foreachCanonicalSymbolOccurrenceByName(symbolName: String, body: @escaping (SymbolOccurrence) -> Bool) {
85+
indexstoredb_for_each_canonical_symbol_occurence_by_name(impl, symbolName) { (occur) -> Bool in
86+
body(SymbolOccurrence(occur))
87+
}
88+
}
89+
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
98+
}
99+
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
106+
}
107+
}
108+
return symbolOccurenceResults
109+
}
83110
}
84111

85112
public struct SymbolRole: OptionSet {

include/CIndexStoreDB/CIndexStoreDB.h

Lines changed: 13 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_for_each_symbol_receiver)(const char *_Nonnull);
90+
8891
INDEXSTOREDB_PUBLIC _Nullable
8992
indexstoredb_index_t
9093
indexstoredb_index_create(const char * _Nonnull storePath,
@@ -159,6 +162,16 @@ indexstoredb_error_get_description(_Nonnull indexstoredb_error_t);
159162
INDEXSTOREDB_PUBLIC void
160163
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);
161164

165+
INDEXSTOREDB_PUBLIC bool
166+
indexstoredb_for_each_symbol_name(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_for_each_symbol_receiver);
167+
168+
INDEXSTOREDB_PUBLIC bool
169+
indexstoredb_for_each_canonical_symbol_occurence_by_name(
170+
indexstoredb_index_t _Nonnull index,
171+
const char *_Nonnull symbolName,
172+
indexstoredb_symbol_occurrence_receiver_t _Nonnull receiver
173+
);
174+
162175
INDEXSTOREDB_END_DECLS
163176

164177
#endif

lib/CIndexStoreDB/CIndexStoreDB.cpp

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

147+
/// loops through each symbol in the index and calls the receiver function with each symbol
148+
/// @param index an IndexStoreDB object which contains the symbols
149+
/// @param receiver a function to be called for each symbol, the CString of the symbol will be passed in to this function.
150+
/// The function should return a boolean indicating whether the looping should continue.
151+
bool
152+
indexstoredb_for_each_symbol_name(indexstoredb_index_t index, indexstoredb_for_each_symbol_receiver receiver) {
153+
// indexSystem has foreachsymbolName.
154+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
155+
return obj->value->foreachSymbolName([&](StringRef ref) -> bool {
156+
return receiver(ref.str().c_str());
157+
});
158+
}
159+
160+
/// loops through each canonical symbol that matches the string, perform the passed in function
161+
/// @param index an IndexStoreDB object which contains the symbols
162+
/// @param symbolName the name of the symbol whose canonical occurence should be found
163+
/// @param receiver a function to be called for each canonical occurence,
164+
/// the SymbolOccurenceRef of the symbol will be passed in to this function.
165+
/// The function should return a boolean indicating whether the looping should continue.
166+
bool
167+
indexstoredb_for_each_canonical_symbol_occurence_by_name(
168+
indexstoredb_index_t index,
169+
const char *_Nonnull symbolName,
170+
indexstoredb_symbol_occurrence_receiver_t receiver)
171+
{
172+
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
173+
return obj->value->foreachCanonicalSymbolOccurrenceByName(symbolName, [&](SymbolOccurrenceRef occur) -> bool {
174+
return receiver(make_object(occur));
175+
});
176+
}
177+
147178
indexstoredb_symbol_t
148179
indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
149180
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;

0 commit comments

Comments
 (0)