Skip to content

Add ability to find symbols by name #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 27 additions & 0 deletions Sources/IndexStoreDB/IndexStoreDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ public final class IndexStoreDB {
return body(SymbolOccurrence(occur))
}
}

@discardableResult public func forEachCanonicalSymbolOccurrence(byName: String, body: @escaping (SymbolOccurrence) -> Bool) -> Bool {
return indexstoredb_index_canonical_symbol_occurences_by_name(impl, byName) { occur in
return body(SymbolOccurrence(occur))
}
}

@discardableResult public func forEachCanonicalSymbolOccurrence(
containing pattern: String,
anchorStart: Bool,
anchorEnd: Bool,
subsequence: Bool,
ignoreCase: Bool,
body: @escaping (SymbolOccurrence) -> Bool
) -> Bool {
return indexstoredb_index_canonical_symbol_occurences_containing_pattern(
impl,
pattern,
anchorStart,
anchorEnd,
subsequence,
ignoreCase
) { occur in
body(SymbolOccurrence(occur))
}
}

}

public struct SymbolRole: OptionSet {
Expand Down
42 changes: 42 additions & 0 deletions include/CIndexStoreDB/CIndexStoreDB.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ typedef _Nullable indexstoredb_indexstore_library_t(^indexstore_library_provider
/// Returns true to continue.
typedef bool(^indexstoredb_symbol_occurrence_receiver_t)(_Nonnull indexstoredb_symbol_occurrence_t);

/// Returns true to continue.
typedef bool(^indexstoredb_symbol_name_receiver)(const char *_Nonnull);

INDEXSTOREDB_PUBLIC _Nullable
indexstoredb_index_t
indexstoredb_index_create(const char * _Nonnull storePath,
Expand Down Expand Up @@ -159,6 +162,45 @@ indexstoredb_error_get_description(_Nonnull indexstoredb_error_t);
INDEXSTOREDB_PUBLIC void
indexstoredb_error_dispose(_Nullable indexstoredb_error_t);

/// Loops through each symbol in the index and calls the receiver function with each symbol.
/// @param index An IndexStoreDB object which contains the symbols.
/// @param receiver A function to be called for each symbol, the CString of the symbol will be passed in to this function.
/// The function should return a boolean indicating whether the looping should continue.
INDEXSTOREDB_PUBLIC bool
indexstoredb_index_symbol_names(_Nonnull indexstoredb_index_t index, _Nonnull indexstoredb_symbol_name_receiver);

/// Loops through each canonical symbol that matches the string and performs the passed in function.
/// @param index An IndexStoreDB object which contains the symbols.
/// @param symbolName The name of the symbol whose canonical occurence should be found.
/// @param receiver A function to be called for each canonical occurence.
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
/// The function should return a boolean indicating whether the looping should continue.
INDEXSTOREDB_PUBLIC bool
indexstoredb_index_canonical_symbol_occurences_by_name(
indexstoredb_index_t _Nonnull index,
const char *_Nonnull symbolName,
indexstoredb_symbol_occurrence_receiver_t _Nonnull receiver
);

/// Loops through each canonical symbol that matches the pattern and performs the passed in function.
/// @param index An IndexStoreDB object which contains the symbols.
/// @param anchorStart When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
/// @param anchorEnd When true, symbol names should only be considered matching when the first characters of the symbol name match the pattern.
/// @param subsequence When true, symbols will be matched even if the pattern is not matched contiguously.
/// @param ignoreCase When true, symbols may be returned even if the case of letters does not match the pattern.
/// @param receiver A function to be called for each canonical occurence that matches the pattern.
/// The SymbolOccurenceRef of the symbol will be passed in to this function.
/// The function should return a boolean indicating whether the looping should continue.
INDEXSTOREDB_PUBLIC bool
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
_Nonnull indexstoredb_index_t index,
const char *_Nonnull pattern,
bool anchorStart,
bool anchorEnd,
bool subsequence,
bool ignoreCase,
_Nonnull indexstoredb_symbol_occurrence_receiver_t receiver);

INDEXSTOREDB_END_DECLS

#endif
43 changes: 43 additions & 0 deletions lib/CIndexStoreDB/CIndexStoreDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,49 @@ indexstoredb_symbol_name(indexstoredb_symbol_t symbol) {
return obj->value->getName().c_str();
}

bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comments should go in the header, not the .cpp file. Please capitalize the first word as well. Same for the other functions.

indexstoredb_index_symbol_names(indexstoredb_index_t index, indexstoredb_symbol_name_receiver receiver) {
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
return obj->value->foreachSymbolName([&](StringRef ref) -> bool {
return receiver(ref.str().c_str());
});
}

bool
indexstoredb_index_canonical_symbol_occurences_by_name(
indexstoredb_index_t index,
const char *_Nonnull symbolName,
indexstoredb_symbol_occurrence_receiver_t receiver)
{
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
return obj->value->foreachCanonicalSymbolOccurrenceByName(symbolName, [&](SymbolOccurrenceRef occur) -> bool {
return receiver(make_object(occur));
});
}

bool
indexstoredb_index_canonical_symbol_occurences_containing_pattern(
indexstoredb_index_t index,
const char *_Nonnull pattern,
bool anchorStart,
bool anchorEnd,
bool subsequence,
bool ignoreCase,
indexstoredb_symbol_occurrence_receiver_t receiver)
{
auto obj = (IndexStoreDBObject<std::shared_ptr<IndexSystem>> *)index;
return obj->value->foreachCanonicalSymbolOccurrenceContainingPattern(
pattern,
anchorStart,
anchorEnd,
subsequence,
ignoreCase,
[&](SymbolOccurrenceRef occur
) -> bool {
return receiver(make_object(occur));
});
}

indexstoredb_symbol_t
indexstoredb_symbol_occurrence_symbol(indexstoredb_symbol_occurrence_t occur) {
auto obj = (IndexStoreDBObject<SymbolOccurrenceRef> *)occur;
Expand Down