2017-12-06 03:32:33 +00:00
|
|
|
#include "semantic_highlight_symbol_cache.h"
|
|
|
|
|
2017-12-22 15:29:13 +00:00
|
|
|
SemanticHighlightSymbolCache::Entry::Entry(
|
|
|
|
SemanticHighlightSymbolCache* all_caches,
|
|
|
|
const std::string& path)
|
|
|
|
: all_caches_(all_caches), path(path) {}
|
|
|
|
|
|
|
|
optional<int> SemanticHighlightSymbolCache::Entry::TryGetStableId(
|
|
|
|
SymbolKind kind,
|
|
|
|
const std::string& detailed_name) {
|
|
|
|
TNameToId* map = GetMapForSymbol_(kind);
|
|
|
|
auto it = map->find(detailed_name);
|
|
|
|
if (it != map->end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
int SemanticHighlightSymbolCache::Entry::GetStableId(
|
|
|
|
SymbolKind kind,
|
|
|
|
const std::string& detailed_name) {
|
2017-12-22 15:29:13 +00:00
|
|
|
optional<int> id = TryGetStableId(kind, detailed_name);
|
|
|
|
if (id)
|
|
|
|
return *id;
|
|
|
|
|
|
|
|
// Create a new id. First try to find a key in another map.
|
|
|
|
all_caches_->cache_.IterateValues([&](const std::shared_ptr<Entry>& entry) {
|
|
|
|
optional<int> other_id = entry->TryGetStableId(kind, detailed_name);
|
|
|
|
if (other_id) {
|
|
|
|
id = other_id;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create a new id.
|
|
|
|
TNameToId* map = GetMapForSymbol_(kind);
|
|
|
|
if (!id)
|
|
|
|
id = all_caches_->next_stable_id_++;
|
|
|
|
return (*map)[detailed_name] = *id;
|
|
|
|
}
|
|
|
|
|
|
|
|
SemanticHighlightSymbolCache::Entry::TNameToId*
|
|
|
|
SemanticHighlightSymbolCache::Entry::GetMapForSymbol_(SymbolKind kind) {
|
2017-12-06 03:32:33 +00:00
|
|
|
switch (kind) {
|
|
|
|
case SymbolKind::Type:
|
2017-12-22 15:29:13 +00:00
|
|
|
return &detailed_type_name_to_stable_id;
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Func:
|
2017-12-22 15:29:13 +00:00
|
|
|
return &detailed_func_name_to_stable_id;
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Var:
|
2017-12-22 15:29:13 +00:00
|
|
|
return &detailed_var_name_to_stable_id;
|
|
|
|
case SymbolKind::File:
|
|
|
|
case SymbolKind::Invalid:
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-12-22 15:29:13 +00:00
|
|
|
assert(false);
|
|
|
|
return nullptr;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SemanticHighlightSymbolCache::SemanticHighlightSymbolCache()
|
|
|
|
: cache_(kCacheSize) {}
|
|
|
|
|
|
|
|
std::shared_ptr<SemanticHighlightSymbolCache::Entry>
|
|
|
|
SemanticHighlightSymbolCache::GetCacheForFile(const std::string& path) {
|
2017-12-22 15:29:13 +00:00
|
|
|
return cache_.Get(
|
|
|
|
path, [&, this]() { return std::make_shared<Entry>(this, path); });
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|