diff --git a/src/cache.cc b/src/cache.cc index 5c121303..e9406874 100644 --- a/src/cache.cc +++ b/src/cache.cc @@ -17,10 +17,11 @@ std::string GetCachedFileName(const std::string& cache_directory, std::string so } // namespace -std::unique_ptr LoadCachedFile(const std::string& cache_directory, const std::string& filename) { - return nullptr; +std::unique_ptr LoadCachedFile(IndexerConfig* config, const std::string& filename) { + if (!config->enableCacheRead) + return nullptr; - optional file_content = ReadContent(GetCachedFileName(cache_directory, filename)); + optional file_content = ReadContent(GetCachedFileName(config->cacheDirectory, filename)); if (!file_content) return nullptr; @@ -31,11 +32,14 @@ std::unique_ptr LoadCachedFile(const std::string& cache_directory, return nullptr; } -void WriteToCache(const std::string& cache_directory, const std::string& filename, IndexedFile& file) { +void WriteToCache(IndexerConfig* config, const std::string& filename, IndexedFile& file) { + if (!config->enableCacheWrite) + return; + std::string indexed_content = Serialize(file); std::ofstream cache; - cache.open(GetCachedFileName(cache_directory, filename)); + cache.open(GetCachedFileName(config->cacheDirectory, filename)); assert(cache.good()); cache << indexed_content; cache.close(); diff --git a/src/cache.h b/src/cache.h index a5e35a77..f01215cc 100644 --- a/src/cache.h +++ b/src/cache.h @@ -1,10 +1,12 @@ #pragma once +#include "language_server_api.h" + #include #include struct IndexedFile; -std::unique_ptr LoadCachedFile(const std::string& cache_directory, const std::string& filename); +std::unique_ptr LoadCachedFile(IndexerConfig* config, const std::string& filename); -void WriteToCache(const std::string& cache_directory, const std::string& filename, IndexedFile& file); \ No newline at end of file +void WriteToCache(IndexerConfig* config, const std::string& filename, IndexedFile& file); \ No newline at end of file diff --git a/src/command_line.cc b/src/command_line.cc index ff9e291d..eadad5c4 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -769,7 +769,8 @@ std::vector FindSymbolsAtLocation(QueryFile* file, lsPosition positio struct Index_DoIndex { enum class Type { - Import, + ImportAndUpdate, + ImportOnly, Update }; @@ -873,26 +874,32 @@ bool IndexMain_DoIndex(IndexerConfig* config, // If the index update is an import, then we will load the previous index // into memory if we have a previous index. After that, we dispatch an // update request to get the latest version. - if (index_request->type == Index_DoIndex::Type::Import) { - index_request->type = Index_DoIndex::Type::Update; - std::unique_ptr old_index = LoadCachedFile(config->cacheDirectory, index_request->path); + if (index_request->type == Index_DoIndex::Type::ImportAndUpdate || + index_request->type == Index_DoIndex::Type::ImportOnly) { + + std::unique_ptr old_index = LoadCachedFile(config, index_request->path); time.ResetAndPrint("Reading cached index from disk " + index_request->path); // If import fails just do a standard update. if (old_index) { for (auto& dependency_path : old_index->dependencies) { + // TODO: These requests should go to the front of the queue. std::cerr << "- Dispatching dependency import " << dependency_path << std::endl; - Index_DoIndex dep_index_request(Index_DoIndex::Type::Import); + Index_DoIndex dep_index_request(Index_DoIndex::Type::ImportOnly); dep_index_request.path = dependency_path; dep_index_request.args = index_request->args; queue_do_index->Enqueue(std::move(dep_index_request)); } - Index_DoIdMap response(nullptr, std::move(old_index)); queue_do_id_map->Enqueue(std::move(response)); - queue_do_index->Enqueue(std::move(*index_request)); + // If we need a reparse, send the document to the back of the queue so it + // gets processed. + if (index_request->type == Index_DoIndex::Type::ImportAndUpdate) { + index_request->type = Index_DoIndex::Type::Update; + queue_do_index->Enqueue(std::move(*index_request)); + } return true; } } @@ -904,7 +911,7 @@ bool IndexMain_DoIndex(IndexerConfig* config, for (auto& current_index : indexes) { std::cerr << "Got index for " << current_index->path << std::endl; - std::unique_ptr old_index = LoadCachedFile(config->cacheDirectory, current_index->path); + std::unique_ptr old_index = LoadCachedFile(config, current_index->path); time.ResetAndPrint("Loading cached index"); // TODO: Cache to disk on a separate thread. Maybe we do the cache after we @@ -912,7 +919,7 @@ bool IndexMain_DoIndex(IndexerConfig* config, // of the current 4). // Cache file so we can diff it later. - WriteToCache(config->cacheDirectory, current_index->path, *current_index); + WriteToCache(config, current_index->path, *current_index); time.ResetAndPrint("Cache index update to disk"); // Send response to create id map. @@ -975,8 +982,13 @@ void IndexMain( int count = 0; - if (!IndexMain_DoIndex(config, file_consumer_shared, queue_do_index, queue_do_id_map) && - !IndexMain_DoCreateIndexUpdate(queue_on_id_mapped, queue_on_indexed)) { + // We need to make sure to run both IndexMain_DoIndex and + // IndexMain_DoCreateIndexUpdate so we don't starve querydb from doing any + // work. Running both also lets the user query the partially constructed + // index. + bool did_index = IndexMain_DoIndex(config, file_consumer_shared, queue_do_index, queue_do_id_map); + bool did_create_update = IndexMain_DoCreateIndexUpdate(queue_on_id_mapped, queue_on_indexed); + if (!did_index && !did_create_update) { //if (count++ > 2) { // count = 0; @@ -1124,7 +1136,7 @@ void QueryDbMainLoop( << "] Dispatching index request for file " << filepath << std::endl; - Index_DoIndex request(Index_DoIndex::Type::Import); + Index_DoIndex request(Index_DoIndex::Type::ImportAndUpdate); request.path = filepath; request.args = entry.args; queue_do_index->Enqueue(std::move(request)); diff --git a/src/indexer.cc b/src/indexer.cc index b86864e2..dbea4a53 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1334,8 +1334,8 @@ void indexEntityReference(CXClientData client_data, std::vector> Parse(IndexerConfig* config, FileConsumer::SharedState* file_consumer_shared, std::string filename, std::vector args, bool dump_ast) { - // NOTE: uncomment to disable indexer - //return {}; + if (!config->enableIndexing) + return {}; filename = NormalizePath(filename); diff --git a/src/language_server_api.h b/src/language_server_api.h index b87bf217..d9da01c6 100644 --- a/src/language_server_api.h +++ b/src/language_server_api.h @@ -62,8 +62,20 @@ struct IndexerConfig { int indexerCount = 1; int maxWorkspaceSearchResults = 1000; std::vector extraClangArguments; + // If false, the indexer will be disabled. + bool enableIndexing = true; + // If false, indexed files will not be written to disk. + bool enableCacheWrite = true; + // If false, the index will not be loaded from a previous run. + bool enableCacheRead = true; }; -MAKE_REFLECT_STRUCT(IndexerConfig, cacheDirectory, whitelist, blacklist, indexerCount, maxWorkspaceSearchResults, extraClangArguments); +MAKE_REFLECT_STRUCT(IndexerConfig, + cacheDirectory, + whitelist, blacklist, + indexerCount, + maxWorkspaceSearchResults, + extraClangArguments, + enableIndexing, enableCacheWrite, enableCacheRead); diff --git a/src/libclangmm/TranslationUnit.cc b/src/libclangmm/TranslationUnit.cc index e84257fc..4313e4c5 100644 --- a/src/libclangmm/TranslationUnit.cc +++ b/src/libclangmm/TranslationUnit.cc @@ -28,10 +28,12 @@ TranslationUnit::TranslationUnit( for (const std::string& arg : config->extraClangArguments) args.push_back(arg.c_str()); +#if false std::cerr << "Parsing " << filepath << " with args "; for (const auto& arg : args) std::cerr << arg << " "; std::cerr << std::endl; +#endif CXErrorCode error_code = clang_parseTranslationUnit2( index.cx_index,