2018-08-21 05:27:52 +00:00
|
|
|
// Copyright 2017-2018 ccls Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-24 01:30:52 +00:00
|
|
|
|
2018-09-08 19:07:43 +00:00
|
|
|
#include "clang_complete.hh"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "config.h"
|
2018-05-13 16:52:19 +00:00
|
|
|
#include "include_complete.h"
|
2018-05-27 19:24:56 +00:00
|
|
|
#include "log.hh"
|
2018-02-24 00:12:39 +00:00
|
|
|
#include "lsp.h"
|
2017-12-24 01:30:52 +00:00
|
|
|
#include "message_handler.h"
|
2018-08-09 17:08:14 +00:00
|
|
|
#include "pipeline.hh"
|
2017-12-24 01:30:52 +00:00
|
|
|
#include "platform.h"
|
|
|
|
#include "project.h"
|
|
|
|
#include "query_utils.h"
|
|
|
|
|
2018-05-27 19:24:56 +00:00
|
|
|
#include <llvm/ADT/Twine.h>
|
|
|
|
#include <llvm/Support/Threading.h>
|
2018-06-01 03:06:09 +00:00
|
|
|
#include <llvm/Support/Timer.h>
|
2018-05-27 19:24:56 +00:00
|
|
|
using namespace llvm;
|
2017-12-24 01:30:52 +00:00
|
|
|
|
2018-06-08 04:53:41 +00:00
|
|
|
#include <chrono>
|
2018-09-10 06:46:13 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <shared_mutex>
|
2018-05-13 16:52:19 +00:00
|
|
|
#include <thread>
|
2018-07-03 22:47:43 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2017-12-24 01:30:52 +00:00
|
|
|
|
2018-06-08 04:53:41 +00:00
|
|
|
void DiagnosticsPublisher::Init() {
|
|
|
|
frequencyMs_ = g_config->diagnostics.frequencyMs;
|
|
|
|
match_ = std::make_unique<GroupMatch>(g_config->diagnostics.whitelist,
|
|
|
|
g_config->diagnostics.blacklist);
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void DiagnosticsPublisher::Publish(WorkingFiles *working_files,
|
2018-06-08 04:53:41 +00:00
|
|
|
std::string path,
|
|
|
|
std::vector<lsDiagnostic> diagnostics) {
|
2018-07-03 22:47:43 +00:00
|
|
|
bool good = true;
|
2018-06-08 04:53:41 +00:00
|
|
|
// Cache diagnostics so we can show fixits.
|
2018-08-09 17:08:14 +00:00
|
|
|
working_files->DoActionOnFile(path, [&](WorkingFile *working_file) {
|
2018-07-03 22:47:43 +00:00
|
|
|
if (working_file) {
|
|
|
|
good = working_file->diagnostics_.empty();
|
2018-06-08 04:53:41 +00:00
|
|
|
working_file->diagnostics_ = diagnostics;
|
2018-07-03 22:47:43 +00:00
|
|
|
}
|
2018-06-08 04:53:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
int64_t now =
|
|
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
|
|
std::chrono::high_resolution_clock::now().time_since_epoch())
|
|
|
|
.count();
|
2018-07-03 22:47:43 +00:00
|
|
|
if (frequencyMs_ >= 0 &&
|
|
|
|
(nextPublish_ <= now || (!good && diagnostics.empty())) &&
|
2018-06-08 04:53:41 +00:00
|
|
|
match_->IsMatch(path)) {
|
|
|
|
nextPublish_ = now + frequencyMs_;
|
|
|
|
|
|
|
|
Out_TextDocumentPublishDiagnostics out;
|
|
|
|
out.params.uri = lsDocumentUri::FromPath(path);
|
|
|
|
out.params.diagnostics = diagnostics;
|
2018-08-09 17:08:14 +00:00
|
|
|
ccls::pipeline::WriteStdout(kMethodType_TextDocumentPublishDiagnostics,
|
|
|
|
out);
|
2018-06-08 04:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ccls::pipeline {
|
|
|
|
namespace {
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
struct Index_Request {
|
|
|
|
std::string path;
|
|
|
|
std::vector<std::string> args;
|
2018-09-08 06:40:22 +00:00
|
|
|
IndexMode mode;
|
2018-05-28 00:50:02 +00:00
|
|
|
lsRequestId id;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Stdout_Request {
|
|
|
|
MethodType method;
|
|
|
|
std::string content;
|
|
|
|
};
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
MultiQueueWaiter *main_waiter;
|
|
|
|
MultiQueueWaiter *indexer_waiter;
|
|
|
|
MultiQueueWaiter *stdout_waiter;
|
|
|
|
ThreadedQueue<std::unique_ptr<InMessage>> *on_request;
|
|
|
|
ThreadedQueue<Index_Request> *index_request;
|
|
|
|
ThreadedQueue<IndexUpdate> *on_indexed;
|
|
|
|
ThreadedQueue<Stdout_Request> *for_stdout;
|
2018-05-28 00:50:02 +00:00
|
|
|
|
2018-09-08 17:37:48 +00:00
|
|
|
struct InMemoryIndexFile {
|
|
|
|
std::string content;
|
|
|
|
IndexFile index;
|
|
|
|
};
|
2018-09-10 06:46:13 +00:00
|
|
|
std::shared_mutex g_index_mutex;
|
2018-09-08 17:37:48 +00:00
|
|
|
std::unordered_map<std::string, InMemoryIndexFile> g_index;
|
|
|
|
|
2018-07-08 18:51:07 +00:00
|
|
|
bool CacheInvalid(VFS *vfs, IndexFile *prev, const std::string &path,
|
|
|
|
const std::vector<std::string> &args,
|
|
|
|
const std::optional<std::string> &from) {
|
2018-05-05 22:29:17 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(vfs->mutex);
|
2018-09-08 17:37:48 +00:00
|
|
|
if (prev->mtime < vfs->state[path].timestamp) {
|
2018-05-05 22:29:17 +00:00
|
|
|
LOG_S(INFO) << "timestamp changed for " << path
|
|
|
|
<< (from ? " (via " + *from + ")" : std::string());
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-18 05:53:03 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 18:51:07 +00:00
|
|
|
if (prev->args != args) {
|
2018-08-09 17:08:14 +00:00
|
|
|
LOG_S(INFO) << "args changed for " << path
|
|
|
|
<< (from ? " (via " + *from + ")" : std::string());
|
2018-07-08 18:51:07 +00:00
|
|
|
return true;
|
2018-01-18 07:19:08 +00:00
|
|
|
}
|
2018-01-18 06:04:07 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
return false;
|
2018-01-18 05:53:03 +00:00
|
|
|
};
|
2018-01-18 05:48:09 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::string AppendSerializationFormat(const std::string &base) {
|
2018-05-28 00:50:02 +00:00
|
|
|
switch (g_config->cacheFormat) {
|
2018-08-09 17:08:14 +00:00
|
|
|
case SerializeFormat::Binary:
|
|
|
|
return base + ".blob";
|
|
|
|
case SerializeFormat::Json:
|
|
|
|
return base + ".json";
|
2018-05-28 00:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::string GetCachePath(const std::string &source_file) {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::string cache_file;
|
|
|
|
size_t len = g_config->projectRoot.size();
|
|
|
|
if (StartsWith(source_file, g_config->projectRoot)) {
|
|
|
|
cache_file = EscapeFileName(g_config->projectRoot) +
|
|
|
|
EscapeFileName(source_file.substr(len));
|
|
|
|
} else {
|
|
|
|
cache_file = '@' + EscapeFileName(g_config->projectRoot) +
|
|
|
|
EscapeFileName(source_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_config->cacheDirectory + cache_file;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
std::unique_ptr<IndexFile> RawCacheLoad(const std::string &path) {
|
2018-09-08 17:37:48 +00:00
|
|
|
if (g_config->cacheDirectory.empty()) {
|
2018-09-10 06:46:13 +00:00
|
|
|
std::shared_lock lock(g_index_mutex);
|
2018-09-08 17:37:48 +00:00
|
|
|
auto it = g_index.find(path);
|
|
|
|
if (it == g_index.end())
|
|
|
|
return nullptr;
|
|
|
|
return std::make_unique<IndexFile>(it->second.index);
|
|
|
|
}
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
std::string cache_path = GetCachePath(path);
|
|
|
|
std::optional<std::string> file_content = ReadContent(cache_path);
|
|
|
|
std::optional<std::string> serialized_indexed_content =
|
|
|
|
ReadContent(AppendSerializationFormat(cache_path));
|
|
|
|
if (!file_content || !serialized_indexed_content)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-06-08 04:53:41 +00:00
|
|
|
return ccls::Deserialize(g_config->cacheFormat, path,
|
|
|
|
*serialized_indexed_content, *file_content,
|
|
|
|
IndexFile::kMajorVersion);
|
2018-05-28 00:50:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 23:00:14 +00:00
|
|
|
bool Indexer_Parse(CompletionManager *completion, WorkingFiles *wfiles,
|
2018-08-30 15:47:48 +00:00
|
|
|
Project *project, VFS *vfs, const GroupMatch &matcher) {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::optional<Index_Request> opt_request = index_request->TryPopFront();
|
2018-05-05 22:29:17 +00:00
|
|
|
if (!opt_request)
|
|
|
|
return false;
|
2018-08-09 17:08:14 +00:00
|
|
|
auto &request = *opt_request;
|
2018-09-08 06:40:22 +00:00
|
|
|
bool loud = request.mode != IndexMode::OnChange;
|
2018-01-18 07:59:48 +00:00
|
|
|
|
2018-05-08 15:56:20 +00:00
|
|
|
// Dummy one to trigger refresh semantic highlight.
|
2018-05-08 07:35:32 +00:00
|
|
|
if (request.path.empty()) {
|
2018-05-08 15:56:20 +00:00
|
|
|
IndexUpdate dummy;
|
|
|
|
dummy.refresh = true;
|
2018-06-01 04:21:34 +00:00
|
|
|
on_indexed->PushBack(std::move(dummy), false);
|
2018-05-08 07:35:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-30 15:47:48 +00:00
|
|
|
if (std::string reason; !matcher.IsMatch(request.path, &reason)) {
|
2018-09-08 06:40:22 +00:00
|
|
|
LOG_IF_S(INFO, loud) << "skip " << request.path << " for " << reason;
|
2018-08-30 15:47:48 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
Project::Entry entry;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(project->mutex_);
|
2018-09-10 06:46:13 +00:00
|
|
|
auto it = project->path_to_entry_index.find(request.path);
|
|
|
|
if (it != project->path_to_entry_index.end())
|
2018-05-05 22:29:17 +00:00
|
|
|
entry = project->entries[it->second];
|
|
|
|
else {
|
|
|
|
entry.filename = request.path;
|
|
|
|
entry.args = request.args;
|
2018-04-30 04:49:03 +00:00
|
|
|
}
|
2018-01-18 07:59:48 +00:00
|
|
|
}
|
2018-05-05 22:29:17 +00:00
|
|
|
std::string path_to_index = entry.filename;
|
|
|
|
std::unique_ptr<IndexFile> prev;
|
2018-01-18 07:59:48 +00:00
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
// Try to load the file from cache.
|
|
|
|
std::optional<int64_t> write_time = LastWriteTime(path_to_index);
|
|
|
|
if (!write_time)
|
|
|
|
return true;
|
2018-07-08 18:51:07 +00:00
|
|
|
int reparse = vfs->Stamp(path_to_index, *write_time);
|
2018-09-08 06:40:22 +00:00
|
|
|
if (g_config->index.onChange)
|
|
|
|
reparse = 2;
|
2018-07-08 18:51:07 +00:00
|
|
|
if (!vfs->Mark(path_to_index, g_thread_id, 1) && !reparse)
|
2018-05-05 22:29:17 +00:00
|
|
|
return true;
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
prev = RawCacheLoad(path_to_index);
|
2018-05-05 22:29:17 +00:00
|
|
|
if (!prev)
|
|
|
|
reparse = 2;
|
|
|
|
else {
|
2018-07-08 18:51:07 +00:00
|
|
|
if (CacheInvalid(vfs, prev.get(), path_to_index, entry.args, std::nullopt))
|
2018-05-05 22:29:17 +00:00
|
|
|
reparse = 2;
|
2018-06-02 07:33:12 +00:00
|
|
|
int reparseForDep = g_config->index.reparseForDependency;
|
|
|
|
if (reparseForDep > 1 || (reparseForDep == 1 && !Project::loaded))
|
2018-08-09 17:08:14 +00:00
|
|
|
for (const auto &dep : prev->dependencies) {
|
2018-06-02 07:33:12 +00:00
|
|
|
if (auto write_time1 = LastWriteTime(dep.first().str())) {
|
|
|
|
if (dep.second < *write_time1) {
|
|
|
|
reparse = 2;
|
|
|
|
std::lock_guard<std::mutex> lock(vfs->mutex);
|
|
|
|
vfs->state[dep.first().str()].stage = 0;
|
|
|
|
}
|
|
|
|
} else
|
2018-05-05 22:29:17 +00:00
|
|
|
reparse = 2;
|
2018-06-02 07:33:12 +00:00
|
|
|
}
|
2018-05-05 22:29:17 +00:00
|
|
|
}
|
2018-01-18 07:59:48 +00:00
|
|
|
|
2018-08-20 16:57:50 +00:00
|
|
|
// Grab the ownership
|
|
|
|
if (reparse) {
|
|
|
|
std::lock_guard<std::mutex> lock(vfs->mutex);
|
|
|
|
vfs->state[path_to_index].owner = g_thread_id;
|
|
|
|
vfs->state[path_to_index].stage = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
if (reparse < 2) {
|
2018-06-02 07:33:12 +00:00
|
|
|
LOG_S(INFO) << "load cache for " << path_to_index;
|
2018-05-05 22:29:17 +00:00
|
|
|
auto dependencies = prev->dependencies;
|
|
|
|
if (reparse) {
|
|
|
|
IndexUpdate update = IndexUpdate::CreateDelta(nullptr, prev.get());
|
2018-09-08 06:40:22 +00:00
|
|
|
on_indexed->PushBack(std::move(update),
|
|
|
|
request.mode != IndexMode::NonInteractive);
|
2018-02-01 05:20:38 +00:00
|
|
|
}
|
2018-07-08 18:51:07 +00:00
|
|
|
for (const auto &dep : dependencies)
|
2018-06-06 07:36:39 +00:00
|
|
|
if (vfs->Mark(dep.first().str(), 0, 2) &&
|
|
|
|
(prev = RawCacheLoad(dep.first().str()))) {
|
2018-05-05 22:29:17 +00:00
|
|
|
IndexUpdate update = IndexUpdate::CreateDelta(nullptr, prev.get());
|
2018-09-08 06:40:22 +00:00
|
|
|
on_indexed->PushBack(std::move(update),
|
|
|
|
request.mode != IndexMode::NonInteractive);
|
2018-05-05 22:29:17 +00:00
|
|
|
}
|
|
|
|
return true;
|
2018-01-18 08:21:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 06:40:22 +00:00
|
|
|
LOG_IF_S(INFO, loud) << "parse " << path_to_index;
|
2018-01-18 08:21:39 +00:00
|
|
|
|
2018-09-08 06:40:22 +00:00
|
|
|
std::vector<std::pair<std::string, std::string>> remapped;
|
|
|
|
if (g_config->index.onChange) {
|
2018-09-08 19:07:43 +00:00
|
|
|
std::string content = wfiles->GetContent(request.path);
|
2018-09-08 06:40:22 +00:00
|
|
|
if (content.size())
|
|
|
|
remapped.emplace_back(request.path, content);
|
|
|
|
}
|
2018-09-08 19:07:43 +00:00
|
|
|
auto indexes = idx::Index(completion, wfiles, vfs, entry.directory,
|
|
|
|
path_to_index, entry.args, remapped);
|
2018-01-20 07:56:49 +00:00
|
|
|
|
2018-04-02 07:22:12 +00:00
|
|
|
if (indexes.empty()) {
|
2018-04-16 19:36:02 +00:00
|
|
|
if (g_config->index.enabled && request.id.Valid()) {
|
2018-01-20 07:56:49 +00:00
|
|
|
Out_Error out;
|
|
|
|
out.id = request.id;
|
|
|
|
out.error.code = lsErrorCodes::InternalError;
|
|
|
|
out.error.message = "Failed to index " + path_to_index;
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType_Unknown, out);
|
2018-01-20 07:56:49 +00:00
|
|
|
}
|
2018-05-05 22:29:17 +00:00
|
|
|
vfs->Reset(path_to_index);
|
|
|
|
return true;
|
2018-01-20 07:56:49 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
for (std::unique_ptr<IndexFile> &curr : indexes) {
|
2018-05-05 22:29:17 +00:00
|
|
|
std::string path = curr->path;
|
2018-09-08 17:37:48 +00:00
|
|
|
bool do_update = path == path_to_index, loaded;
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(vfs->mutex);
|
|
|
|
VFS::State &st = vfs->state[path];
|
|
|
|
if (st.timestamp < curr->mtime) {
|
|
|
|
st.timestamp = curr->mtime;
|
|
|
|
do_update = true;
|
|
|
|
}
|
|
|
|
loaded = st.loaded;
|
|
|
|
st.loaded = true;
|
|
|
|
}
|
|
|
|
if (!do_update)
|
2018-05-05 22:29:17 +00:00
|
|
|
continue;
|
2018-08-30 15:47:48 +00:00
|
|
|
if (std::string reason; !matcher.IsMatch(path, &reason)) {
|
2018-09-08 06:40:22 +00:00
|
|
|
LOG_IF_S(INFO, loud) << "skip emitting and storing index of " << path << " for "
|
|
|
|
<< reason;
|
2018-08-30 15:47:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-09-08 17:37:48 +00:00
|
|
|
prev.reset();
|
|
|
|
if (loaded)
|
|
|
|
prev = RawCacheLoad(path);
|
2018-03-20 18:55:40 +00:00
|
|
|
|
2018-09-08 17:37:48 +00:00
|
|
|
// Store current index.
|
|
|
|
LOG_IF_S(INFO, loud) << "store index for " << path << " (delta: " << !!prev
|
|
|
|
<< ")";
|
|
|
|
if (g_config->cacheDirectory.empty()) {
|
2018-09-10 06:46:13 +00:00
|
|
|
std::lock_guard lock(g_index_mutex);
|
2018-09-08 17:37:48 +00:00
|
|
|
auto it = g_index.insert_or_assign(
|
|
|
|
path, InMemoryIndexFile{curr->file_contents, *curr});
|
|
|
|
std::string().swap(it.first->second.index.file_contents);
|
|
|
|
} else {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::string cache_path = GetCachePath(path);
|
|
|
|
WriteToFile(cache_path, curr->file_contents);
|
|
|
|
WriteToFile(AppendSerializationFormat(cache_path),
|
2018-09-08 17:37:48 +00:00
|
|
|
Serialize(g_config->cacheFormat, *curr));
|
2018-05-28 00:50:02 +00:00
|
|
|
}
|
2018-05-05 22:29:17 +00:00
|
|
|
|
2018-07-08 18:51:07 +00:00
|
|
|
vfs->Reset(path);
|
2018-05-05 22:29:17 +00:00
|
|
|
if (entry.id >= 0) {
|
|
|
|
std::lock_guard<std::mutex> lock(project->mutex_);
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &dep : curr->dependencies)
|
2018-09-10 06:46:13 +00:00
|
|
|
project->path_to_entry_index[dep.first()] = entry.id;
|
2018-03-20 18:55:40 +00:00
|
|
|
}
|
2017-12-24 01:30:52 +00:00
|
|
|
|
2018-05-05 03:40:52 +00:00
|
|
|
// Build delta update.
|
2018-05-05 22:29:17 +00:00
|
|
|
IndexUpdate update = IndexUpdate::CreateDelta(prev.get(), curr.get());
|
2017-12-29 16:45:10 +00:00
|
|
|
|
2018-09-08 06:40:22 +00:00
|
|
|
on_indexed->PushBack(std::move(update),
|
|
|
|
request.mode != IndexMode::NonInteractive);
|
2018-04-22 17:01:44 +00:00
|
|
|
}
|
|
|
|
|
2018-05-05 22:29:17 +00:00
|
|
|
return true;
|
2018-04-22 17:01:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace
|
2017-12-29 16:45:10 +00:00
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
void Init() {
|
|
|
|
main_waiter = new MultiQueueWaiter;
|
|
|
|
on_request = new ThreadedQueue<std::unique_ptr<InMessage>>(main_waiter);
|
2018-06-01 04:21:34 +00:00
|
|
|
on_indexed = new ThreadedQueue<IndexUpdate>(main_waiter);
|
2018-05-28 00:50:02 +00:00
|
|
|
|
|
|
|
indexer_waiter = new MultiQueueWaiter;
|
|
|
|
index_request = new ThreadedQueue<Index_Request>(indexer_waiter);
|
|
|
|
|
|
|
|
stdout_waiter = new MultiQueueWaiter;
|
|
|
|
for_stdout = new ThreadedQueue<Stdout_Request>(stdout_waiter);
|
|
|
|
}
|
|
|
|
|
2018-09-08 23:00:14 +00:00
|
|
|
void Indexer_Main(CompletionManager *completion, VFS *vfs, Project *project,
|
|
|
|
WorkingFiles *wfiles) {
|
2018-08-30 15:47:48 +00:00
|
|
|
GroupMatch matcher(g_config->index.whitelist, g_config->index.blacklist);
|
2018-05-05 22:29:17 +00:00
|
|
|
while (true)
|
2018-09-08 23:00:14 +00:00
|
|
|
if (!Indexer_Parse(completion, wfiles, project, vfs, matcher))
|
2018-05-28 00:50:02 +00:00
|
|
|
indexer_waiter->Wait(index_request);
|
2018-02-05 03:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void Main_OnIndexed(DB *db, SemanticHighlightSymbolCache *semantic_cache,
|
|
|
|
WorkingFiles *working_files, IndexUpdate *update) {
|
2018-06-01 04:21:34 +00:00
|
|
|
if (update->refresh) {
|
2018-06-02 07:33:12 +00:00
|
|
|
Project::loaded = true;
|
2018-08-09 17:08:14 +00:00
|
|
|
LOG_S(INFO)
|
|
|
|
<< "loaded project. Refresh semantic highlight for all working file.";
|
2018-05-08 07:35:32 +00:00
|
|
|
std::lock_guard<std::mutex> lock(working_files->files_mutex);
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &f : working_files->files) {
|
2018-05-08 15:56:20 +00:00
|
|
|
std::string filename = LowerPathIfInsensitive(f->filename);
|
|
|
|
if (db->name2file_id.find(filename) == db->name2file_id.end())
|
|
|
|
continue;
|
2018-08-09 17:08:14 +00:00
|
|
|
QueryFile *file = &db->files[db->name2file_id[filename]];
|
2018-05-08 07:35:32 +00:00
|
|
|
EmitSemanticHighlighting(db, semantic_cache, f.get(), file);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 22:47:43 +00:00
|
|
|
static Timer timer("apply", "apply index");
|
2018-06-01 03:06:09 +00:00
|
|
|
timer.startTimer();
|
2018-06-01 04:21:34 +00:00
|
|
|
db->ApplyIndexUpdate(update);
|
2018-06-01 03:06:09 +00:00
|
|
|
timer.stopTimer();
|
2018-02-05 03:38:57 +00:00
|
|
|
|
2018-07-08 19:49:27 +00:00
|
|
|
// Update indexed content, skipped ranges, and semantic highlighting.
|
2018-06-01 04:21:34 +00:00
|
|
|
if (update->files_def_update) {
|
2018-08-09 17:08:14 +00:00
|
|
|
auto &def_u = *update->files_def_update;
|
2018-09-08 06:40:22 +00:00
|
|
|
if (WorkingFile *wfile =
|
2018-06-01 04:21:34 +00:00
|
|
|
working_files->GetFileByFilename(def_u.first.path)) {
|
2018-09-08 06:40:22 +00:00
|
|
|
// FIXME With index.onChange: true, use buffer_content only for
|
|
|
|
// request.path
|
|
|
|
wfile->SetIndexContent(g_config->index.onChange ? wfile->buffer_content
|
|
|
|
: def_u.second);
|
|
|
|
EmitSkippedRanges(wfile, def_u.first.skipped_ranges);
|
|
|
|
EmitSemanticHighlighting(db, semantic_cache, wfile,
|
2018-06-01 04:21:34 +00:00
|
|
|
&db->files[update->file_id]);
|
2018-02-05 03:38:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-20 03:01:23 +00:00
|
|
|
|
2018-06-01 03:06:09 +00:00
|
|
|
void LaunchStdin() {
|
|
|
|
std::thread([]() {
|
2018-05-27 19:24:56 +00:00
|
|
|
set_thread_name("stdin");
|
2018-05-13 16:52:19 +00:00
|
|
|
while (true) {
|
|
|
|
std::unique_ptr<InMessage> message;
|
|
|
|
std::optional<std::string> err =
|
|
|
|
MessageRegistry::instance()->ReadMessageFromStdin(&message);
|
|
|
|
|
|
|
|
// Message parsing can fail if we don't recognize the method.
|
|
|
|
if (err) {
|
|
|
|
// The message may be partially deserialized.
|
|
|
|
// Emit an error ResponseMessage if |id| is available.
|
|
|
|
if (message) {
|
|
|
|
lsRequestId id = message->GetRequestId();
|
|
|
|
if (id.Valid()) {
|
|
|
|
Out_Error out;
|
|
|
|
out.id = id;
|
|
|
|
out.error.code = lsErrorCodes::InvalidParams;
|
|
|
|
out.error.message = std::move(*err);
|
2018-05-28 00:50:02 +00:00
|
|
|
WriteStdout(kMethodType_Unknown, out);
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2018-02-05 03:38:57 +00:00
|
|
|
|
2018-05-13 16:52:19 +00:00
|
|
|
// Cache |method_id| so we can access it after moving |message|.
|
|
|
|
MethodType method_type = message->GetMethodType();
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
on_request->PushBack(std::move(message));
|
2018-05-13 16:52:19 +00:00
|
|
|
|
|
|
|
// If the message was to exit then querydb will take care of the actual
|
|
|
|
// exit. Stop reading from stdin since it might be detached.
|
|
|
|
if (method_type == kMethodType_Exit)
|
|
|
|
break;
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 03:06:09 +00:00
|
|
|
void LaunchStdout() {
|
2018-05-13 16:52:19 +00:00
|
|
|
std::thread([=]() {
|
2018-05-27 19:24:56 +00:00
|
|
|
set_thread_name("stdout");
|
2018-05-13 16:52:19 +00:00
|
|
|
|
|
|
|
while (true) {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::vector<Stdout_Request> messages = for_stdout->DequeueAll();
|
2018-05-13 16:52:19 +00:00
|
|
|
if (messages.empty()) {
|
2018-05-28 00:50:02 +00:00
|
|
|
stdout_waiter->Wait(for_stdout);
|
2018-05-13 16:52:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &message : messages) {
|
2018-07-03 22:47:43 +00:00
|
|
|
#ifdef _WIN32
|
2018-05-13 16:52:19 +00:00
|
|
|
fwrite(message.content.c_str(), message.content.size(), 1, stdout);
|
|
|
|
fflush(stdout);
|
2018-07-03 22:47:43 +00:00
|
|
|
#else
|
|
|
|
write(1, message.content.c_str(), message.content.size());
|
|
|
|
#endif
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-09 17:08:14 +00:00
|
|
|
})
|
|
|
|
.detach();
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 00:50:02 +00:00
|
|
|
void MainLoop() {
|
2018-05-13 16:52:19 +00:00
|
|
|
Project project;
|
|
|
|
SemanticHighlightSymbolCache semantic_cache;
|
|
|
|
WorkingFiles working_files;
|
|
|
|
VFS vfs;
|
2018-06-03 07:29:33 +00:00
|
|
|
DiagnosticsPublisher diag_pub;
|
2018-05-13 16:52:19 +00:00
|
|
|
|
2018-09-08 19:07:43 +00:00
|
|
|
CompletionManager clang_complete(
|
2018-05-13 16:52:19 +00:00
|
|
|
&project, &working_files,
|
|
|
|
[&](std::string path, std::vector<lsDiagnostic> diagnostics) {
|
2018-06-03 07:29:33 +00:00
|
|
|
diag_pub.Publish(&working_files, path, diagnostics);
|
2018-05-13 16:52:19 +00:00
|
|
|
},
|
|
|
|
[](lsRequestId id) {
|
|
|
|
if (id.Valid()) {
|
|
|
|
Out_Error out;
|
|
|
|
out.id = id;
|
|
|
|
out.error.code = lsErrorCodes::InternalError;
|
2018-08-09 17:08:14 +00:00
|
|
|
out.error.message = "Dropping completion request; a newer request "
|
|
|
|
"has come in that will be serviced instead.";
|
2018-05-28 00:50:02 +00:00
|
|
|
pipeline::WriteStdout(kMethodType_Unknown, out);
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
IncludeComplete include_complete(&project);
|
2018-05-30 06:56:14 +00:00
|
|
|
DB db;
|
2018-05-13 16:52:19 +00:00
|
|
|
|
|
|
|
// Setup shared references.
|
2018-08-09 17:08:14 +00:00
|
|
|
for (MessageHandler *handler : *MessageHandler::message_handlers) {
|
2018-05-13 16:52:19 +00:00
|
|
|
handler->db = &db;
|
|
|
|
handler->waiter = indexer_waiter;
|
|
|
|
handler->project = &project;
|
2018-06-03 07:29:33 +00:00
|
|
|
handler->diag_pub = &diag_pub;
|
2018-05-13 16:52:19 +00:00
|
|
|
handler->vfs = &vfs;
|
|
|
|
handler->semantic_cache = &semantic_cache;
|
|
|
|
handler->working_files = &working_files;
|
|
|
|
handler->clang_complete = &clang_complete;
|
|
|
|
handler->include_complete = &include_complete;
|
2017-12-24 01:30:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 16:52:19 +00:00
|
|
|
while (true) {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::vector<std::unique_ptr<InMessage>> messages = on_request->DequeueAll();
|
2018-05-13 16:52:19 +00:00
|
|
|
bool did_work = messages.size();
|
2018-08-09 17:08:14 +00:00
|
|
|
for (auto &message : messages) {
|
2018-05-13 16:52:19 +00:00
|
|
|
// TODO: Consider using std::unordered_map to lookup the handler
|
2018-08-09 17:08:14 +00:00
|
|
|
for (MessageHandler *handler : *MessageHandler::message_handlers) {
|
2018-05-13 16:52:19 +00:00
|
|
|
if (handler->GetMethodType() == message->GetMethodType()) {
|
|
|
|
handler->Run(std::move(message));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message)
|
|
|
|
LOG_S(ERROR) << "No handler for " << message->GetMethodType();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 80; i--;) {
|
2018-06-01 04:21:34 +00:00
|
|
|
std::optional<IndexUpdate> update = on_indexed->TryPopFront();
|
|
|
|
if (!update)
|
2018-05-13 16:52:19 +00:00
|
|
|
break;
|
|
|
|
did_work = true;
|
2018-06-01 04:21:34 +00:00
|
|
|
Main_OnIndexed(&db, &semantic_cache, &working_files, &*update);
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 01:20:51 +00:00
|
|
|
if (!did_work) {
|
|
|
|
FreeUnusedMemory();
|
2018-05-28 00:50:02 +00:00
|
|
|
main_waiter->Wait(on_indexed, on_request);
|
2018-06-09 01:20:51 +00:00
|
|
|
}
|
2018-05-13 16:52:19 +00:00
|
|
|
}
|
2017-12-24 01:30:52 +00:00
|
|
|
}
|
2018-05-28 00:50:02 +00:00
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void Index(const std::string &path, const std::vector<std::string> &args,
|
2018-09-08 06:40:22 +00:00
|
|
|
IndexMode mode, lsRequestId id) {
|
|
|
|
index_request->PushBack({path, args, mode, id}, mode != IndexMode::NonInteractive);
|
2018-05-28 00:50:02 +00:00
|
|
|
}
|
|
|
|
|
2018-09-08 17:37:48 +00:00
|
|
|
std::optional<int64_t> LastWriteTime(const std::string &path) {
|
|
|
|
sys::fs::file_status Status;
|
|
|
|
if (sys::fs::status(path, Status))
|
|
|
|
return {};
|
|
|
|
return sys::toTimeT(Status.getLastModificationTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<std::string> LoadIndexedContent(const std::string &path) {
|
|
|
|
if (g_config->cacheDirectory.empty()) {
|
2018-09-10 06:46:13 +00:00
|
|
|
std::shared_lock lock(g_index_mutex);
|
2018-09-08 17:37:48 +00:00
|
|
|
auto it = g_index.find(path);
|
|
|
|
if (it == g_index.end())
|
|
|
|
return {};
|
|
|
|
return it->second.content;
|
|
|
|
}
|
2018-05-28 00:50:02 +00:00
|
|
|
return ReadContent(GetCachePath(path));
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
void WriteStdout(MethodType method, lsBaseOutMessage &response) {
|
2018-05-28 00:50:02 +00:00
|
|
|
std::ostringstream sstream;
|
|
|
|
response.Write(sstream);
|
|
|
|
|
|
|
|
Stdout_Request out;
|
|
|
|
out.content = sstream.str();
|
|
|
|
out.method = method;
|
|
|
|
for_stdout->PushBack(std::move(out));
|
|
|
|
}
|
|
|
|
|
2018-08-09 17:08:14 +00:00
|
|
|
} // namespace ccls::pipeline
|