mirror of
https://github.com/MaskRay/ccls.git
synced 2024-11-23 08:05:07 +00:00
11d6623938
We copy the file contents we indexed over to the index cache folder. Then we load those file contents into the WorkingFile instance as needed. This means code lens should never get out of sync, as the index buffer cache will always be correct.
59 lines
1.8 KiB
C++
59 lines
1.8 KiB
C++
#include "cache.h"
|
|
|
|
#include "indexer.h"
|
|
#include "platform.h"
|
|
#include "language_server_api.h"
|
|
|
|
#include <algorithm>
|
|
|
|
namespace {
|
|
|
|
std::string GetCachedBaseFileName(const std::string& cache_directory, std::string source_file) {
|
|
assert(!cache_directory.empty());
|
|
std::replace(source_file.begin(), source_file.end(), '\\', '_');
|
|
std::replace(source_file.begin(), source_file.end(), '/', '_');
|
|
std::replace(source_file.begin(), source_file.end(), ':', '_');
|
|
std::replace(source_file.begin(), source_file.end(), '.', '_');
|
|
return cache_directory + source_file;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::unique_ptr<IndexedFile> LoadCachedIndex(IndexerConfig* config, const std::string& filename) {
|
|
if (!config->enableCacheRead)
|
|
return nullptr;
|
|
|
|
optional<std::string> file_content = ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename) + ".json");
|
|
if (!file_content)
|
|
return nullptr;
|
|
|
|
optional<IndexedFile> indexed = Deserialize(filename, *file_content);
|
|
if (indexed && indexed->version == IndexedFile::kCurrentVersion)
|
|
return MakeUnique<IndexedFile>(indexed.value());
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
optional<std::string> LoadCachedFileContents(IndexerConfig* config, const std::string& filename) {
|
|
if (!config->enableCacheRead)
|
|
return nullptr;
|
|
|
|
return ReadContent(GetCachedBaseFileName(config->cacheDirectory, filename) + ".txt");
|
|
}
|
|
|
|
void WriteToCache(IndexerConfig* config, const std::string& filename, IndexedFile& file) {
|
|
if (!config->enableCacheWrite)
|
|
return;
|
|
|
|
std::string cache_basename = GetCachedBaseFileName(config->cacheDirectory, filename);
|
|
|
|
CopyFileTo(cache_basename + ".txt", filename);
|
|
|
|
std::string indexed_content = Serialize(file);
|
|
std::ofstream cache;
|
|
cache.open(cache_basename + ".json");
|
|
assert(cache.good());
|
|
cache << indexed_content;
|
|
cache.close();
|
|
}
|