From 71591d7805a4adf1eef0293b18c135aebaafa60f Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Fri, 29 Dec 2017 12:00:44 -0600 Subject: [PATCH] Index_Request::contents is no longer optional --- src/import_pipeline.cc | 18 ++++++------------ src/messages/cquery_freshen_index.cc | 22 +++++++++++++--------- src/messages/initialize.cc | 8 +++++++- src/messages/text_document_did_open.cc | 5 +++-- src/messages/text_document_did_save.cc | 13 ++++++++++--- src/queue_manager.cc | 2 +- src/queue_manager.h | 4 ++-- src/utils.h | 1 + 8 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/import_pipeline.cc b/src/import_pipeline.cc index f55671d3..0ca6b235 100644 --- a/src/import_pipeline.cc +++ b/src/import_pipeline.cc @@ -49,7 +49,7 @@ std::vector DoParseFile( bool is_interactive, const std::string& path, const std::vector& args, - const optional& contents) { + const FileContents& contents) { std::vector result; // Always run this block, even if we are interactive, so we can check @@ -168,17 +168,13 @@ std::vector DoParseFile( // TODO: We might be able to optimize perf by only copying for files in // working_files. We can pass that same set of files to the indexer as // well. We then default to a fast file-copy if not in working set. - bool loaded_primary = false; - std::vector file_contents; - if (contents) { - loaded_primary = loaded_primary || contents->path == path; - file_contents.push_back(*contents); - } + bool loaded_primary = contents.path == path; + std::vector file_contents = {contents}; cache_manager->IterateLoadedCaches([&](IndexFile* index) { // FIXME: ReadContent should go through |cache_manager|. optional index_content = ReadContent(index->path); if (!index_content) { - LOG_S(ERROR) << "Failed to preload index content for " << index->path; + LOG_S(ERROR) << "Failed to load index content for " << index->path; return; } @@ -226,10 +222,8 @@ std::vector ParseFile( ImportManager* import_manager, bool is_interactive, const Project::Entry& entry, - const optional& contents) { - optional file_contents; - if (contents) - file_contents = FileContents(entry.filename, *contents); + const std::string& contents) { + FileContents file_contents(entry.filename, contents); std::unique_ptr cache_manager = ICacheManager::Make(config); diff --git a/src/messages/cquery_freshen_index.cc b/src/messages/cquery_freshen_index.cc index cd3b7417..a2054a0f 100644 --- a/src/messages/cquery_freshen_index.cc +++ b/src/messages/cquery_freshen_index.cc @@ -45,15 +45,19 @@ struct CqueryFreshenIndexHandler : MessageHandler { auto* queue = QueueManager::instance(); // Send index requests for every file. - project->ForAllFilteredFiles(config, [&](int i, - const Project::Entry& entry) { - LOG_S(INFO) << "[" << i << "/" << (project->entries.size() - 1) - << "] Dispatching index request for file " << entry.filename; - bool is_interactive = - working_files->GetFileByFilename(entry.filename) != nullptr; - queue->index_request.Enqueue( - Index_Request(entry.filename, entry.args, is_interactive, nullopt)); - }); + project->ForAllFilteredFiles( + config, [&](int i, const Project::Entry& entry) { + optional content = ReadContent(entry.filename); + if (!content) { + LOG_S(ERROR) << "When freshening index, cannot read file " + << entry.filename; + return; + } + bool is_interactive = + working_files->GetFileByFilename(entry.filename) != nullptr; + queue->index_request.Enqueue(Index_Request(entry.filename, entry.args, + is_interactive, *content)); + }); } }; REGISTER_MESSAGE_HANDLER(CqueryFreshenIndexHandler); diff --git a/src/messages/initialize.cc b/src/messages/initialize.cc index 3e4c5f2d..87d2b86a 100644 --- a/src/messages/initialize.cc +++ b/src/messages/initialize.cc @@ -191,10 +191,16 @@ struct InitializeHandler : BaseMessageHandler { time.Reset(); project->ForAllFilteredFiles( config, [&](int i, const Project::Entry& entry) { + optional content = ReadContent(entry.filename); + if (!content) { + LOG_S(ERROR) << "When loading project, canont read file " + << entry.filename; + return; + } bool is_interactive = working_files->GetFileByFilename(entry.filename) != nullptr; queue->index_request.Enqueue(Index_Request( - entry.filename, entry.args, is_interactive, nullopt)); + entry.filename, entry.args, is_interactive, *content)); }); // We need to support multiple concurrent index processes. diff --git a/src/messages/text_document_did_open.cc b/src/messages/text_document_did_open.cc index e4b6f9ae..ac983af4 100644 --- a/src/messages/text_document_did_open.cc +++ b/src/messages/text_document_did_open.cc @@ -57,8 +57,9 @@ struct TextDocumentDidOpenHandler // Submit new index request. const Project::Entry& entry = project->FindCompilationEntryForFile(path); - QueueManager::instance()->index_request.PriorityEnqueue(Index_Request( - entry.filename, entry.args, true /*is_interactive*/, nullopt)); + QueueManager::instance()->index_request.PriorityEnqueue( + Index_Request(entry.filename, entry.args, true /*is_interactive*/, + request->params.textDocument.text)); } }; REGISTER_MESSAGE_HANDLER(TextDocumentDidOpenHandler); diff --git a/src/messages/text_document_did_save.cc b/src/messages/text_document_did_save.cc index 0b9099c0..d6e41854 100644 --- a/src/messages/text_document_did_save.cc +++ b/src/messages/text_document_did_save.cc @@ -3,6 +3,8 @@ #include "project.h" #include "queue_manager.h" +#include + namespace { struct Ipc_TextDocumentDidSave : public IpcMessage { struct Params { @@ -41,9 +43,14 @@ struct TextDocumentDidSaveHandler // mutex and check to see if we should skip the current request. // if so, ignore that index response. // TODO: send as priority request - Project::Entry entry = project->FindCompilationEntryForFile(path); - QueueManager::instance()->index_request.Enqueue(Index_Request( - entry.filename, entry.args, true /*is_interactive*/, nullopt)); + optional content = ReadContent(path); + if (!content) { + LOG_S(ERROR) << "Unable to read file content after saving " << path; + } else { + Project::Entry entry = project->FindCompilationEntryForFile(path); + QueueManager::instance()->index_request.Enqueue(Index_Request( + entry.filename, entry.args, true /*is_interactive*/, *content)); + } clang_complete->NotifySave(path); } diff --git a/src/queue_manager.cc b/src/queue_manager.cc index aacc5c84..c6afe982 100644 --- a/src/queue_manager.cc +++ b/src/queue_manager.cc @@ -8,7 +8,7 @@ Index_Request::Index_Request(const std::string& path, const std::vector& args, bool is_interactive, - optional contents) + const std::string& contents) : path(path), args(args), is_interactive(is_interactive), diff --git a/src/queue_manager.h b/src/queue_manager.h index 646cbb50..98acd0a2 100644 --- a/src/queue_manager.h +++ b/src/queue_manager.h @@ -19,12 +19,12 @@ struct Index_Request { // TODO: make |args| a string that is parsed lazily. std::vector args; bool is_interactive; - optional contents; // Preloaded contents. Useful for tests. + std::string contents; // Preloaded contents. Useful for tests. Index_Request(const std::string& path, const std::vector& args, bool is_interactive, - optional contents); + const std::string& contents); }; struct Index_DoIdMap { diff --git a/src/utils.h b/src/utils.h index 08c97034..a1e63293 100644 --- a/src/utils.h +++ b/src/utils.h @@ -71,6 +71,7 @@ void EnsureEndsInSlash(std::string& path); // e.g. foo/bar.c => foo_bar.c std::string EscapeFileName(std::string path); +// FIXME: Move ReadContent into ICacheManager? optional ReadContent(const std::string& filename); std::vector ReadLinesWithEnding(std::string filename); std::vector ToLines(const std::string& content,