2018-01-30 05:34:28 +00:00
|
|
|
#include "cache_manager.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "clang_complete.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "message_handler.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "project.h"
|
|
|
|
#include "queue_manager.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-29 18:00:44 +00:00
|
|
|
#include <loguru/loguru.hpp>
|
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/didSave";
|
|
|
|
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentDidSave : public NotificationInMessage {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
struct Params {
|
|
|
|
// The document that was saved.
|
|
|
|
lsTextDocumentIdentifier textDocument;
|
|
|
|
|
|
|
|
// Optional the content when saved. Depends on the includeText value
|
|
|
|
// when the save notifcation was requested.
|
|
|
|
// std::string text;
|
|
|
|
};
|
|
|
|
Params params;
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentDidSave::Params, textDocument);
|
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentDidSave, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentDidSave);
|
|
|
|
|
|
|
|
struct Handler_TextDocumentDidSave
|
|
|
|
: BaseMessageHandler<In_TextDocumentDidSave> {
|
|
|
|
MethodType GetMethodType() const override { return kMethodType; }
|
2017-12-06 04:39:44 +00:00
|
|
|
|
2018-03-22 04:05:25 +00:00
|
|
|
void Run(In_TextDocumentDidSave* request) override {
|
2017-12-06 03:32:33 +00:00
|
|
|
std::string path = request->params.textDocument.uri.GetPath();
|
2017-12-07 01:00:19 +00:00
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
// Send out an index request, and copy the current buffer state so we
|
|
|
|
// can update the cached index contents when the index is done.
|
|
|
|
//
|
2018-03-19 20:28:11 +00:00
|
|
|
// We also do not index if there is already an index request or if
|
|
|
|
// the client requested indexing on didChange instead.
|
2017-12-06 03:32:33 +00:00
|
|
|
//
|
|
|
|
// TODO: Cancel outgoing index request. Might be tricky to make
|
|
|
|
// efficient since we have to cancel.
|
|
|
|
// - we could have an |atomic<int> active_cancellations| variable
|
|
|
|
// that all of the indexers check before accepting an index. if
|
|
|
|
// zero we don't slow down fast-path. if non-zero we acquire
|
|
|
|
// mutex and check to see if we should skip the current request.
|
|
|
|
// if so, ignore that index response.
|
|
|
|
// TODO: send as priority request
|
2018-04-04 06:05:41 +00:00
|
|
|
if (!g_config->index.onDidChange) {
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<std::string> content = ReadContent(path);
|
2018-03-19 20:28:11 +00:00
|
|
|
if (!content) {
|
|
|
|
LOG_S(ERROR) << "Unable to read file content after saving " << path;
|
|
|
|
} else {
|
|
|
|
Project::Entry entry = project->FindCompilationEntryForFile(path);
|
|
|
|
QueueManager::instance()->index_request.PushBack(
|
|
|
|
Index_Request(entry.filename, entry.args, true /*is_interactive*/,
|
2018-04-01 00:49:32 +00:00
|
|
|
*content, ICacheManager::Make()),
|
2018-03-19 20:28:11 +00:00
|
|
|
true);
|
|
|
|
}
|
2017-12-29 18:00:44 +00:00
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
clang_complete->NotifySave(path);
|
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidSave);
|
2017-12-07 01:00:19 +00:00
|
|
|
} // namespace
|