This commit is contained in:
Fangrui Song 2018-03-31 17:49:32 -07:00
parent c96631d1ee
commit daf7a41278
9 changed files with 23 additions and 25 deletions

View File

@ -14,14 +14,14 @@ namespace {
// Manages loading caches from file paths for the indexer process.
struct RealCacheManager : ICacheManager {
explicit RealCacheManager(Config* config) : config_(config) {}
explicit RealCacheManager() {}
~RealCacheManager() override = default;
void WriteToCache(IndexFile& file) override {
std::string cache_path = GetCachePath(file.path);
WriteToFile(cache_path, file.file_contents);
std::string indexed_content = Serialize(config_->cacheFormat, file);
std::string indexed_content = Serialize(g_config.cacheFormat, file);
WriteToFile(AppendSerializationFormat(cache_path), indexed_content);
}
@ -38,27 +38,27 @@ struct RealCacheManager : ICacheManager {
if (!file_content || !serialized_indexed_content)
return nullptr;
return Deserialize(config_->cacheFormat, path, *serialized_indexed_content,
return Deserialize(g_config.cacheFormat, path, *serialized_indexed_content,
*file_content, IndexFile::kMajorVersion);
}
std::string GetCachePath(const std::string& source_file) {
assert(!config_->cacheDirectory.empty());
assert(!g_config.cacheDirectory.empty());
std::string cache_file;
size_t len = config_->projectRoot.size();
if (StartsWith(source_file, config_->projectRoot)) {
cache_file = EscapeFileName(config_->projectRoot) +
size_t len = g_config.projectRoot.size();
if (StartsWith(source_file, g_config.projectRoot)) {
cache_file = EscapeFileName(g_config.projectRoot) +
EscapeFileName(source_file.substr(len));
} else {
cache_file = '@' + EscapeFileName(config_->projectRoot) +
cache_file = '@' + EscapeFileName(g_config.projectRoot) +
EscapeFileName(source_file);
}
return config_->cacheDirectory + cache_file;
return g_config.cacheDirectory + cache_file;
}
std::string AppendSerializationFormat(const std::string& base) {
switch (config_->cacheFormat) {
switch (g_config.cacheFormat) {
case SerializeFormat::Json:
return base + ".json";
case SerializeFormat::MessagePack:
@ -67,8 +67,6 @@ struct RealCacheManager : ICacheManager {
assert(false);
return ".json";
}
Config* config_;
};
struct FakeCacheManager : ICacheManager {
@ -105,8 +103,8 @@ struct FakeCacheManager : ICacheManager {
} // namespace
// static
std::shared_ptr<ICacheManager> ICacheManager::Make(Config* config) {
return std::make_shared<RealCacheManager>(config);
std::shared_ptr<ICacheManager> ICacheManager::Make() {
return std::make_shared<RealCacheManager>();
}
// static

View File

@ -18,7 +18,7 @@ struct ICacheManager {
std::string json;
};
static std::shared_ptr<ICacheManager> Make(Config* config);
static std::shared_ptr<ICacheManager> Make();
static std::shared_ptr<ICacheManager> MakeFake(
const std::vector<FakeCacheEntry>& entries);

View File

@ -40,7 +40,7 @@ struct Handler_CclsFreshenIndex : BaseMessageHandler<In_CclsFreshenIndex> {
GroupMatch matcher(request->params.whitelist, request->params.blacklist);
// Unmark all files whose timestamp has changed.
std::shared_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
std::shared_ptr<ICacheManager> cache_manager = ICacheManager::Make();
std::queue<const QueryFile*> q;
// |need_index| stores every filename ever enqueued.

View File

@ -33,7 +33,7 @@ struct Handler_CclsIndexFile : BaseMessageHandler<In_CclsIndexFile> {
QueueManager::instance()->index_request.PushBack(
Index_Request(NormalizePath(request->params.path), request->params.args,
request->params.is_interactive, request->params.contents,
ICacheManager::Make(config)));
ICacheManager::Make()));
}
};
REGISTER_MESSAGE_HANDLER(Handler_CclsIndexFile);

View File

@ -33,7 +33,7 @@ struct Handler_TextDocumentDidChange
Project::Entry entry = project->FindCompilationEntryForFile(path);
QueueManager::instance()->index_request.PushBack(
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
*content, ICacheManager::Make(config)),
*content, ICacheManager::Make()),
true);
}
}

View File

@ -41,7 +41,7 @@ struct Handler_TextDocumentDidOpen
Timer time;
std::string path = params.textDocument.uri.GetPath();
std::shared_ptr<ICacheManager> cache_manager = ICacheManager::Make(config);
std::shared_ptr<ICacheManager> cache_manager = ICacheManager::Make();
WorkingFile* working_file = working_files->OnOpen(params.textDocument);
std::optional<std::string> cached_file_contents =
cache_manager->LoadCachedFileContents(path);

View File

@ -55,7 +55,7 @@ struct Handler_TextDocumentDidSave
Project::Entry entry = project->FindCompilationEntryForFile(path);
QueueManager::instance()->index_request.PushBack(
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
*content, ICacheManager::Make(config)),
*content, ICacheManager::Make()),
true);
}
}

View File

@ -56,7 +56,7 @@ struct Handler_WorkspaceDidChangeWatchedFiles
else {
QueueManager::instance()->index_request.PushBack(
Index_Request(path, entry.args, is_interactive, *content,
ICacheManager::Make(config)));
ICacheManager::Make()));
if (is_interactive)
clang_complete->NotifySave(path);
}
@ -65,7 +65,7 @@ struct Handler_WorkspaceDidChangeWatchedFiles
case lsFileChangeType::Deleted:
QueueManager::instance()->index_request.PushBack(
Index_Request(path, entry.args, is_interactive, std::string(),
ICacheManager::Make(config)));
ICacheManager::Make()));
break;
}
}

View File

@ -568,9 +568,9 @@ void Project::Index(Config* config,
return;
}
bool is_interactive = wfiles->GetFileByFilename(entry.filename) != nullptr;
queue->index_request.PushBack(
Index_Request(entry.filename, entry.args, is_interactive, *content,
ICacheManager::Make(config), id));
queue->index_request.PushBack(Index_Request(entry.filename, entry.args,
is_interactive, *content,
ICacheManager::Make(), id));
});
}