2017-12-29 17:27:56 +00:00
|
|
|
#include "cache_manager.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "clang_complete.h"
|
|
|
|
#include "include_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
|
|
|
#include "timer.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "working_files.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-06 05:03:38 +00:00
|
|
|
namespace {
|
2018-03-22 04:05:25 +00:00
|
|
|
MethodType kMethodType = "textDocument/didOpen";
|
|
|
|
|
2017-12-06 04:39:44 +00:00
|
|
|
// Open, view, change, close file
|
2018-03-22 05:01:21 +00:00
|
|
|
struct In_TextDocumentDidOpen : 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 {
|
|
|
|
lsTextDocumentItem textDocument;
|
2018-03-17 20:24:01 +00:00
|
|
|
|
2018-03-31 03:16:33 +00:00
|
|
|
// ccls extension
|
2018-03-17 20:24:01 +00:00
|
|
|
// If specified (e.g. ["clang++", "-DM", "a.cc"]), it overrides the project
|
2018-03-31 03:16:33 +00:00
|
|
|
// entry (e.g. loaded from compile_commands.json or .ccls).
|
2018-03-17 20:24:01 +00:00
|
|
|
std::vector<std::string> args;
|
2017-12-06 04:39:44 +00:00
|
|
|
};
|
|
|
|
Params params;
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen::Params, textDocument, args);
|
|
|
|
MAKE_REFLECT_STRUCT(In_TextDocumentDidOpen, params);
|
|
|
|
REGISTER_IN_MESSAGE(In_TextDocumentDidOpen);
|
|
|
|
|
|
|
|
struct Handler_TextDocumentDidOpen
|
|
|
|
: BaseMessageHandler<In_TextDocumentDidOpen> {
|
|
|
|
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_TextDocumentDidOpen* request) override {
|
2017-12-06 03:32:33 +00:00
|
|
|
// NOTE: This function blocks code lens. If it starts taking a long time
|
|
|
|
// we will need to find a way to unblock the code lens request.
|
2018-03-17 20:24:01 +00:00
|
|
|
const auto& params = request->params;
|
|
|
|
std::string path = params.textDocument.uri.GetPath();
|
2017-12-07 01:00:19 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
ICacheManager cache;
|
2018-03-17 20:24:01 +00:00
|
|
|
WorkingFile* working_file = working_files->OnOpen(params.textDocument);
|
2018-03-31 03:16:33 +00:00
|
|
|
std::optional<std::string> cached_file_contents =
|
2018-05-05 22:29:17 +00:00
|
|
|
cache.LoadCachedFileContents(path);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (cached_file_contents)
|
|
|
|
working_file->SetIndexContent(*cached_file_contents);
|
|
|
|
|
|
|
|
QueryFile* file = nullptr;
|
2018-03-31 03:16:33 +00:00
|
|
|
FindFileOrFail(db, project, std::nullopt, path, &file);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (file && file->def) {
|
|
|
|
EmitInactiveLines(working_file, file->def->inactive_regions);
|
|
|
|
EmitSemanticHighlighting(db, semantic_cache, working_file, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
include_complete->AddFile(working_file->filename);
|
|
|
|
clang_complete->NotifyView(path);
|
2018-05-05 03:40:52 +00:00
|
|
|
if (params.args.size())
|
|
|
|
project->SetFlagsForFile(params.args, path);
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
// Submit new index request if it is not a header file.
|
|
|
|
if (SourceFileLanguage(path) != LanguageId::Unknown) {
|
|
|
|
Project::Entry entry = project->FindCompilationEntryForFile(path);
|
|
|
|
QueueManager::instance()->index_request.PushBack(
|
2018-05-05 22:29:17 +00:00
|
|
|
Index_Request(entry.filename,
|
|
|
|
params.args.size() ? params.args : entry.args,
|
2018-05-26 05:48:58 +00:00
|
|
|
true /*is_interactive*/),
|
2018-05-05 22:29:17 +00:00
|
|
|
true /* priority */);
|
2018-03-20 22:49:40 +00:00
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
clang_complete->FlushSession(entry.filename);
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
};
|
2018-03-22 04:05:25 +00:00
|
|
|
REGISTER_MESSAGE_HANDLER(Handler_TextDocumentDidOpen);
|
2017-12-07 01:00:19 +00:00
|
|
|
} // namespace
|