From bdabb7596cb71886dfa28897a50759cf490d19b1 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Wed, 21 Mar 2018 20:49:28 +0100 Subject: [PATCH] Make overridden flags persistent didOpen can override flags from compilation database. didSave was able to reset the flags back. This makes sure that the overridden flags persist. --- src/messages/text_document_did_open.cc | 5 ++++- src/project.cc | 17 +++++++++++++++++ src/project.h | 7 +++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/messages/text_document_did_open.cc b/src/messages/text_document_did_open.cc index 36e20738..e354e449 100644 --- a/src/messages/text_document_did_open.cc +++ b/src/messages/text_document_did_open.cc @@ -61,7 +61,7 @@ struct TextDocumentDidOpenHandler clang_complete->NotifyView(path); // Submit new index request. - const Project::Entry& entry = project->FindCompilationEntryForFile(path); + Project::Entry entry = project->FindCompilationEntryForFile(path); QueueManager::instance()->index_request.PushBack( Index_Request( entry.filename, params.args.size() ? params.args : entry.args, @@ -70,6 +70,9 @@ struct TextDocumentDidOpenHandler clang_complete->FlushSession(entry.filename); LOG_S(INFO) << "Flushed clang complete sessions for " << entry.filename; + if (params.args.size()) { + project->SetFlagsForFile(params.args, path); + } } }; REGISTER_MESSAGE_HANDLER(TextDocumentDidOpenHandler); diff --git a/src/project.cc b/src/project.cc index 0c476a71..11617fe1 100644 --- a/src/project.cc +++ b/src/project.cc @@ -569,6 +569,23 @@ void Project::Load(Config* config, const std::string& root_directory) { absolute_path_to_entry_index_[entries[i].filename] = i; } +void Project::SetFlagsForFile( + const std::vector& flags, + const std::string& path) { + auto it = absolute_path_to_entry_index_.find(path); + if (it != absolute_path_to_entry_index_.end()) { + // The entry already exists in the project, just set the flags. + this->entries[it->second].args = flags; + } else { + // Entry wasn't found, so we create a new one. + Entry entry; + entry.is_inferred = false; + entry.filename = path; + entry.args = flags; + this->entries.emplace_back(entry); + } +} + Project::Entry Project::FindCompilationEntryForFile( const std::string& filename) { auto it = absolute_path_to_entry_index_.find(filename); diff --git a/src/project.h b/src/project.h index db93f4b2..c3acdf22 100644 --- a/src/project.h +++ b/src/project.h @@ -48,6 +48,13 @@ struct Project { // will infer one based on existing project structure. Entry FindCompilationEntryForFile(const std::string& filename); + // If the client has overridden the flags, or specified them for a file + // that is not in the compilation_database.json make sure those changes + // are permanent. + void SetFlagsForFile( + const std::vector& flags, + const std::string& path); + // Run |action| on every file in the project. void ForAllFilteredFiles( Config* config,