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.
|
|
|
|
==============================================================================*/
|
|
|
|
|
2018-01-20 19:34:37 +00:00
|
|
|
#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
|
|
|
|
2018-01-27 17:29:28 +00:00
|
|
|
#include <queue>
|
|
|
|
#include <unordered_set>
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-09-12 06:31:06 +00:00
|
|
|
MethodType kMethodType = "$ccls/reload";
|
2018-03-22 04:05:25 +00:00
|
|
|
|
2018-09-12 06:31:06 +00:00
|
|
|
struct In_CclsReload : public NotificationInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-01-27 17:29:28 +00:00
|
|
|
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-09-12 06:31:06 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsReload::Params, dependencies, whitelist,
|
2018-01-30 00:27:43 +00:00
|
|
|
blacklist);
|
2018-09-12 06:31:06 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_CclsReload, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_CclsReload);
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-09-12 06:31:06 +00:00
|
|
|
struct Handler_CclsReload : BaseMessageHandler<In_CclsReload> {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2018-09-12 06:31:06 +00:00
|
|
|
void Run(In_CclsReload *request) override {
|
|
|
|
const auto ¶ms = 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();
|
2018-09-12 06:31:06 +00:00
|
|
|
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;
|
2018-01-27 17:29:28 +00:00
|
|
|
// |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-01-27 17:29:28 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const auto &file : db->files)
|
2018-01-27 17:29:28 +00:00
|
|
|
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)
|
2018-01-27 17:29:28 +00:00
|
|
|
graph[dependency].push_back(file.def->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!q.empty()) {
|
2018-08-09 17:08:14 +00:00
|
|
|
const QueryFile *file = q.front();
|
2018-01-27 17:29:28 +00:00
|
|
|
q.pop();
|
|
|
|
need_index.insert(file->def->path);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-01-27 17:29:28 +00:00
|
|
|
if (request->params.dependencies)
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const std::string &path : graph[file->def->path]) {
|
2018-01-27 17:29:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-09-12 06:31:06 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_CclsReload);
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|