ccls/src/messages/ccls_reload.cc

95 lines
3.0 KiB
C++
Raw Normal View History

2018-08-21 05:27:52 +00:00
/* Copyright 2017-2018 ccls Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "match.h"
2017-12-06 03:32:33 +00:00
#include "message_handler.h"
2018-08-09 17:08:14 +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 "working_files.h"
2018-05-28 00:50:02 +00:00
using namespace ccls;
2017-12-06 03:32:33 +00:00
#include <queue>
#include <unordered_set>
2017-12-06 03:32:33 +00:00
namespace {
MethodType kMethodType = "$ccls/reload";
struct In_CclsReload : 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
};
MAKE_REFLECT_STRUCT(In_CclsReload::Params, dependencies, whitelist,
2018-01-30 00:27:43 +00:00
blacklist);
MAKE_REFLECT_STRUCT(In_CclsReload, params);
REGISTER_IN_MESSAGE(In_CclsReload);
2017-12-06 04:39:44 +00:00
struct Handler_CclsReload : BaseMessageHandler<In_CclsReload> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_CclsReload *request) override {
const auto &params = request->params;
// Send index requests for every file.
if (params.whitelist.empty() && params.blacklist.empty()) {
2018-09-21 01:04:55 +00:00
vfs->Clear();
db->clear();
project->Index(working_files, lsRequestId());
return;
}
// TODO
GroupMatch matcher(params.whitelist, params.blacklist);
2017-12-06 03:32:33 +00:00
2018-08-09 17:08:14 +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.
2018-08-09 17:08:14 +00:00
std::unordered_map<std::string, const QueryFile *> path_to_file;
2018-08-09 17:08:14 +00:00
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;
2018-08-09 17:08:14 +00:00
for (const std::string &dependency : file.def->dependencies)
graph[dependency].push_back(file.def->path);
}
while (!q.empty()) {
2018-08-09 17:08:14 +00:00
const QueryFile *file = q.front();
q.pop();
need_index.insert(file->def->path);
2017-12-06 03:32:33 +00:00
if (request->params.dependencies)
2018-08-09 17:08:14 +00:00
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
}
}
};
REGISTER_MESSAGE_HANDLER(Handler_CclsReload);
2018-08-09 17:08:14 +00:00
} // namespace