ccls/src/messages/workspace_did_change_watched_files.cc

76 lines
2.5 KiB
C++
Raw Normal View History

#include "cache_manager.h"
2018-01-26 07:04:07 +00:00
#include "clang_complete.h"
#include "message_handler.h"
2018-01-26 07:04:07 +00:00
#include "project.h"
#include "queue_manager.h"
2018-01-26 07:04:07 +00:00
#include "working_files.h"
#include <loguru/loguru.hpp>
namespace {
MethodType kMethodType = "workspace/didChangeWatchedFiles";
enum class lsFileChangeType {
Created = 1,
Changed = 2,
Deleted = 3,
};
2018-01-30 00:35:01 +00:00
MAKE_REFLECT_TYPE_PROXY(lsFileChangeType);
struct lsFileEvent {
lsDocumentUri uri;
lsFileChangeType type;
};
MAKE_REFLECT_STRUCT(lsFileEvent, uri, type);
struct lsDidChangeWatchedFilesParams {
std::vector<lsFileEvent> changes;
};
MAKE_REFLECT_STRUCT(lsDidChangeWatchedFilesParams, changes);
2018-03-22 05:01:21 +00:00
struct In_WorkspaceDidChangeWatchedFiles : public NotificationInMessage {
MethodType GetMethodType() const override { return kMethodType; }
lsDidChangeWatchedFilesParams params;
};
MAKE_REFLECT_STRUCT(In_WorkspaceDidChangeWatchedFiles, params);
REGISTER_IN_MESSAGE(In_WorkspaceDidChangeWatchedFiles);
struct Handler_WorkspaceDidChangeWatchedFiles
: BaseMessageHandler<In_WorkspaceDidChangeWatchedFiles> {
MethodType GetMethodType() const override { return kMethodType; }
void Run(In_WorkspaceDidChangeWatchedFiles* request) override {
for (lsFileEvent& event : request->params.changes) {
2018-01-26 07:04:07 +00:00
std::string path = event.uri.GetPath();
2018-01-27 07:17:49 +00:00
auto it = project->absolute_path_to_entry_index_.find(path);
if (it == project->absolute_path_to_entry_index_.end())
continue;
const Project::Entry& entry = project->entries[it->second];
bool is_interactive =
working_files->GetFileByFilename(entry.filename) != nullptr;
switch (event.type) {
case lsFileChangeType::Created:
2018-01-26 07:04:07 +00:00
case lsFileChangeType::Changed: {
optional<std::string> content = ReadContent(path);
if (!content)
LOG_S(ERROR) << "Unable to read file content after saving " << path;
else {
QueueManager::instance()->index_request.PushBack(
2018-02-22 07:34:32 +00:00
Index_Request(path, entry.args, is_interactive, *content,
ICacheManager::Make(config)));
2018-01-27 07:17:49 +00:00
if (is_interactive)
clang_complete->NotifySave(path);
2018-01-26 07:04:07 +00:00
}
break;
2018-01-26 07:04:07 +00:00
}
case lsFileChangeType::Deleted:
QueueManager::instance()->index_request.PushBack(
2018-02-22 07:34:32 +00:00
Index_Request(path, entry.args, is_interactive, std::string(),
ICacheManager::Make(config)));
break;
}
}
}
};
REGISTER_MESSAGE_HANDLER(Handler_WorkspaceDidChangeWatchedFiles);
} // namespace