ccls/src/messages/ccls_freshen_index.cc

87 lines
2.7 KiB
C++
Raw Normal View History

#include "cache_manager.h"
#include "match.h"
2017-12-06 03:32:33 +00:00
#include "message_handler.h"
2018-05-27 19:24:56 +00:00
#include "pipeline.hh"
2017-12-06 03:32:33 +00:00
#include "platform.h"
2017-12-29 16:29:47 +00:00
#include "project.h"
#include "queue_manager.h"
#include "timer.h"
2017-12-29 16:29:47 +00:00
#include "working_files.h"
2017-12-06 03:32:33 +00:00
#include <queue>
#include <unordered_set>
2017-12-06 03:32:33 +00:00
namespace {
2018-03-31 03:16:33 +00:00
MethodType kMethodType = "$ccls/freshenIndex";
2018-03-31 03:16:33 +00:00
struct In_CclsFreshenIndex : public NotificationInMessage {
MethodType GetMethodType() const override { return kMethodType; }
struct Params {
bool dependencies = true;
std::vector<std::string> whitelist;
std::vector<std::string> blacklist;
};
Params params;
2017-12-06 04:39:44 +00:00
};
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(In_CclsFreshenIndex::Params,
2018-01-30 00:27:43 +00:00
dependencies,
whitelist,
blacklist);
2018-03-31 03:16:33 +00:00
MAKE_REFLECT_STRUCT(In_CclsFreshenIndex, params);
REGISTER_IN_MESSAGE(In_CclsFreshenIndex);
2017-12-06 04:39:44 +00:00
2018-03-31 03:16:33 +00:00
struct Handler_CclsFreshenIndex : BaseMessageHandler<In_CclsFreshenIndex> {
MethodType GetMethodType() const override { return kMethodType; }
2018-03-31 03:16:33 +00:00
void Run(In_CclsFreshenIndex* request) override {
GroupMatch matcher(request->params.whitelist, request->params.blacklist);
2017-12-06 03:32:33 +00:00
std::queue<const QueryFile*> q;
// |need_index| stores every filename ever enqueued.
std::unordered_set<std::string> need_index;
// Reverse dependency graph.
std::unordered_map<std::string, std::vector<std::string>> graph;
// filename -> QueryFile mapping for files haven't enqueued.
std::unordered_map<std::string, const QueryFile*> path_to_file;
for (const auto& file : db->files)
if (file.def) {
if (matcher.IsMatch(file.def->path))
q.push(&file);
else
path_to_file[file.def->path] = &file;
for (const std::string& dependency : file.def->dependencies)
graph[dependency].push_back(file.def->path);
}
while (!q.empty()) {
const QueryFile* file = q.front();
q.pop();
need_index.insert(file->def->path);
2017-12-06 03:32:33 +00:00
std::optional<int64_t> write_time = LastWriteTime(file->def->path);
if (!write_time)
2017-12-06 03:32:33 +00:00
continue;
{
std::lock_guard<std::mutex> lock(vfs->mutex);
VFS::State& st = vfs->state[file->def->path];
if (st.timestamp < write_time)
st.stage = 0;
}
if (request->params.dependencies)
for (const std::string& path : graph[file->def->path]) {
auto it = path_to_file.find(path);
if (it != path_to_file.end()) {
q.push(it->second);
path_to_file.erase(it);
}
}
2017-12-06 03:32:33 +00:00
}
// Send index requests for every file.
2018-04-16 19:36:02 +00:00
project->Index(QueueManager::instance(), working_files, lsRequestId());
2017-12-06 03:32:33 +00:00
}
};
2018-03-31 03:16:33 +00:00
REGISTER_MESSAGE_HANDLER(Handler_CclsFreshenIndex);
} // namespace