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

View File

@ -18,7 +18,7 @@ struct ICacheManager {
std::string json; std::string json;
}; };
static std::shared_ptr<ICacheManager> Make(Config* config); static std::shared_ptr<ICacheManager> Make();
static std::shared_ptr<ICacheManager> MakeFake( static std::shared_ptr<ICacheManager> MakeFake(
const std::vector<FakeCacheEntry>& entries); 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); GroupMatch matcher(request->params.whitelist, request->params.blacklist);
// Unmark all files whose timestamp has changed. // 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; std::queue<const QueryFile*> q;
// |need_index| stores every filename ever enqueued. // |need_index| stores every filename ever enqueued.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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