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