2017-12-05 07:57:41 +00:00
|
|
|
#include "message_handler.h"
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "lex_utils.h"
|
2018-01-06 21:40:33 +00:00
|
|
|
#include "project.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
#include "query_utils.h"
|
2017-12-29 16:29:47 +00:00
|
|
|
#include "queue_manager.h"
|
|
|
|
#include "semantic_highlight_symbol_cache.h"
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
#include <loguru.hpp>
|
|
|
|
|
2018-01-10 03:04:08 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace {
|
2018-02-23 23:27:21 +00:00
|
|
|
|
|
|
|
struct Out_CquerySetInactiveRegion
|
|
|
|
: public lsOutMessage<Out_CquerySetInactiveRegion> {
|
|
|
|
struct Params {
|
|
|
|
lsDocumentUri uri;
|
|
|
|
std::vector<lsRange> inactiveRegions;
|
|
|
|
};
|
|
|
|
std::string method = "$cquery/setInactiveRegions";
|
|
|
|
Params params;
|
|
|
|
};
|
|
|
|
MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion::Params, uri, inactiveRegions);
|
|
|
|
MAKE_REFLECT_STRUCT(Out_CquerySetInactiveRegion, jsonrpc, method, params);
|
|
|
|
|
2018-01-10 03:04:08 +00:00
|
|
|
struct ScanLineEvent {
|
|
|
|
lsPosition pos;
|
|
|
|
lsPosition end_pos; // Second key when there is a tie for insertion events.
|
|
|
|
int id;
|
|
|
|
Out_CqueryPublishSemanticHighlighting::Symbol* symbol;
|
|
|
|
bool operator<(const ScanLineEvent& other) const {
|
|
|
|
// See the comments below when insertion/deletion events are inserted.
|
2018-02-22 17:43:53 +00:00
|
|
|
if (!(pos == other.pos))
|
|
|
|
return pos < other.pos;
|
|
|
|
if (!(other.end_pos == end_pos))
|
|
|
|
return other.end_pos < end_pos;
|
|
|
|
// This comparison essentially order Macro after non-Macro,
|
|
|
|
// So that macros will not be rendered as Var/Type/...
|
|
|
|
return symbol->kind < other.symbol->kind;
|
2018-01-10 03:04:08 +00:00
|
|
|
}
|
|
|
|
};
|
2018-01-11 02:43:01 +00:00
|
|
|
} // namespace
|
2018-01-10 03:04:08 +00:00
|
|
|
|
2017-12-05 07:57:41 +00:00
|
|
|
MessageHandler::MessageHandler() {
|
|
|
|
// Dynamically allocate |message_handlers|, otherwise there will be static
|
|
|
|
// initialization order races.
|
|
|
|
if (!message_handlers)
|
|
|
|
message_handlers = new std::vector<MessageHandler*>();
|
|
|
|
message_handlers->push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-12-06 03:32:33 +00:00
|
|
|
std::vector<MessageHandler*>* MessageHandler::message_handlers = nullptr;
|
|
|
|
|
|
|
|
bool FindFileOrFail(QueryDatabase* db,
|
2018-01-11 02:43:01 +00:00
|
|
|
const Project* project,
|
2017-12-06 03:32:33 +00:00
|
|
|
optional<lsRequestId> id,
|
|
|
|
const std::string& absolute_path,
|
|
|
|
QueryFile** out_query_file,
|
|
|
|
QueryFileId* out_file_id) {
|
|
|
|
*out_query_file = nullptr;
|
|
|
|
|
2018-01-30 00:55:51 +00:00
|
|
|
auto it = db->usr_to_file.find(NormalizedPath(absolute_path));
|
2017-12-06 03:32:33 +00:00
|
|
|
if (it != db->usr_to_file.end()) {
|
|
|
|
QueryFile& file = db->files[it->second.id];
|
|
|
|
if (file.def) {
|
|
|
|
*out_query_file = &file;
|
|
|
|
if (out_file_id)
|
|
|
|
*out_file_id = QueryFileId(it->second.id);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_file_id)
|
2018-02-03 18:33:22 +00:00
|
|
|
*out_file_id = QueryFileId();
|
2017-12-06 03:32:33 +00:00
|
|
|
|
2017-12-31 03:18:33 +00:00
|
|
|
bool indexing = project->absolute_path_to_entry_index_.find(absolute_path) !=
|
|
|
|
project->absolute_path_to_entry_index_.end();
|
|
|
|
if (indexing)
|
|
|
|
LOG_S(INFO) << "\"" << absolute_path << "\" is being indexed.";
|
|
|
|
else
|
|
|
|
LOG_S(INFO) << "Unable to find file \"" << absolute_path << "\"";
|
2017-12-28 17:03:35 +00:00
|
|
|
/*
|
2017-12-28 16:55:46 +00:00
|
|
|
LOG_S(INFO) << "Files (size=" << db->usr_to_file.size() << "): "
|
|
|
|
<< StringJoinMap(db->usr_to_file,
|
|
|
|
[](const std::pair<Usr, QueryFileId>& entry) {
|
|
|
|
return entry.first;
|
|
|
|
});
|
2017-12-28 17:03:35 +00:00
|
|
|
*/
|
2017-12-06 03:32:33 +00:00
|
|
|
|
|
|
|
if (id) {
|
|
|
|
Out_Error out;
|
|
|
|
out.id = *id;
|
2017-12-31 03:18:33 +00:00
|
|
|
if (indexing) {
|
|
|
|
out.error.code = lsErrorCodes::ServerNotInitialized;
|
|
|
|
out.error.message = absolute_path + " is being indexed.";
|
|
|
|
} else {
|
|
|
|
out.error.code = lsErrorCodes::InternalError;
|
|
|
|
out.error.message = "Unable to find file " + absolute_path;
|
|
|
|
}
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::Unknown, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmitInactiveLines(WorkingFile* working_file,
|
|
|
|
const std::vector<Range>& inactive_regions) {
|
|
|
|
Out_CquerySetInactiveRegion out;
|
|
|
|
out.params.uri = lsDocumentUri::FromPath(working_file->filename);
|
|
|
|
for (Range skipped : inactive_regions) {
|
|
|
|
optional<lsRange> ls_skipped = GetLsRange(working_file, skipped);
|
|
|
|
if (ls_skipped)
|
|
|
|
out.params.inactiveRegions.push_back(*ls_skipped);
|
|
|
|
}
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::CqueryPublishInactiveRegions, out);
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmitSemanticHighlighting(QueryDatabase* db,
|
|
|
|
SemanticHighlightSymbolCache* semantic_cache,
|
|
|
|
WorkingFile* working_file,
|
|
|
|
QueryFile* file) {
|
|
|
|
assert(file->def);
|
|
|
|
auto semantic_cache_for_file =
|
|
|
|
semantic_cache->GetCacheForFile(file->def->path);
|
|
|
|
|
|
|
|
// Group symbols together.
|
|
|
|
std::unordered_map<SymbolIdx, Out_CqueryPublishSemanticHighlighting::Symbol>
|
|
|
|
grouped_symbols;
|
|
|
|
for (SymbolRef sym : file->def->all_symbols) {
|
2018-01-31 04:59:31 +00:00
|
|
|
std::string_view detailed_name;
|
2018-01-28 20:34:31 +00:00
|
|
|
SymbolKind parent_kind = SymbolKind::Invalid;
|
2018-02-18 19:29:38 +00:00
|
|
|
lsSymbolKind kind = lsSymbolKind::Unknown;
|
2018-01-28 04:25:14 +00:00
|
|
|
StorageClass storage = StorageClass::Invalid;
|
2017-12-06 03:32:33 +00:00
|
|
|
// This switch statement also filters out symbols that are not highlighted.
|
2018-02-09 17:42:10 +00:00
|
|
|
switch (sym.kind) {
|
2017-12-06 03:32:33 +00:00
|
|
|
case SymbolKind::Func: {
|
2018-02-22 06:34:34 +00:00
|
|
|
const QueryFunc::Def* def = nullptr;
|
|
|
|
for (auto& i : db->GetFunc(sym).def) {
|
|
|
|
def = &i;
|
|
|
|
if (i.spell)
|
|
|
|
break;
|
|
|
|
}
|
2018-02-18 07:09:05 +00:00
|
|
|
if (!def)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue; // applies to for loop
|
2018-01-29 17:28:17 +00:00
|
|
|
// Don't highlight overloadable operators or implicit lambda ->
|
|
|
|
// std::function constructor.
|
2018-02-18 07:09:05 +00:00
|
|
|
std::string_view short_name = def->ShortName();
|
2018-01-31 06:39:39 +00:00
|
|
|
if (short_name.compare(0, 8, "operator") == 0 ||
|
|
|
|
short_name.compare(0, 27, "function<type-parameter-0-0") == 0)
|
2017-12-06 03:32:33 +00:00
|
|
|
continue; // applies to for loop
|
2018-02-19 03:01:22 +00:00
|
|
|
if (def->spell)
|
|
|
|
parent_kind = def->spell->kind;
|
2018-02-18 07:09:05 +00:00
|
|
|
kind = def->kind;
|
2018-02-22 06:34:34 +00:00
|
|
|
storage = def->storage;
|
2018-01-31 06:39:39 +00:00
|
|
|
detailed_name = short_name;
|
2018-02-01 05:38:04 +00:00
|
|
|
|
|
|
|
// Check whether the function name is actually there.
|
|
|
|
// If not, do not publish the semantic highlight.
|
|
|
|
// E.g. copy-initialization of constructors should not be highlighted
|
|
|
|
// but we still want to keep the range for jumping to definition.
|
|
|
|
std::string_view concise_name =
|
|
|
|
detailed_name.substr(0, detailed_name.find('<'));
|
2018-02-09 17:42:10 +00:00
|
|
|
int16_t start_line = sym.range.start.line;
|
|
|
|
int16_t start_col = sym.range.start.column;
|
2018-02-01 05:38:04 +00:00
|
|
|
if (start_line >= 0 && start_line < working_file->index_lines.size()) {
|
|
|
|
std::string_view line = working_file->index_lines[start_line];
|
2018-02-09 17:42:10 +00:00
|
|
|
sym.range.end.line = start_line;
|
2018-02-01 05:38:04 +00:00
|
|
|
if (line.compare(start_col, concise_name.size(), concise_name) == 0)
|
2018-02-09 17:42:10 +00:00
|
|
|
sym.range.end.column = start_col + concise_name.size();
|
2018-02-01 05:38:04 +00:00
|
|
|
else
|
|
|
|
continue; // applies to for loop
|
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-02-22 06:34:34 +00:00
|
|
|
case SymbolKind::Type:
|
|
|
|
for (auto& def : db->GetType(sym).def) {
|
|
|
|
kind = def.kind;
|
|
|
|
detailed_name = def.detailed_name;
|
|
|
|
if (def.spell) {
|
|
|
|
parent_kind = def.spell->kind;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
2018-02-22 06:34:34 +00:00
|
|
|
case SymbolKind::Var:
|
|
|
|
for (auto& def : db->GetVar(sym).def) {
|
|
|
|
kind = def.kind;
|
|
|
|
storage = def.storage;
|
|
|
|
detailed_name = def.detailed_name;
|
|
|
|
if (def.spell) {
|
|
|
|
parent_kind = def.spell->kind;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-18 05:52:14 +00:00
|
|
|
}
|
2017-12-06 03:32:33 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue; // applies to for loop
|
|
|
|
}
|
|
|
|
|
2018-02-09 17:42:10 +00:00
|
|
|
optional<lsRange> loc = GetLsRange(working_file, sym.range);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (loc) {
|
2018-02-11 18:25:37 +00:00
|
|
|
auto it = grouped_symbols.find(sym);
|
2017-12-06 03:32:33 +00:00
|
|
|
if (it != grouped_symbols.end()) {
|
|
|
|
it->second.ranges.push_back(*loc);
|
|
|
|
} else {
|
|
|
|
Out_CqueryPublishSemanticHighlighting::Symbol symbol;
|
2018-01-31 04:59:31 +00:00
|
|
|
symbol.stableId = semantic_cache_for_file->GetStableId(
|
2018-02-09 17:42:10 +00:00
|
|
|
sym.kind, std::string(detailed_name));
|
2018-01-28 20:34:31 +00:00
|
|
|
symbol.parentKind = parent_kind;
|
2018-01-17 01:48:22 +00:00
|
|
|
symbol.kind = kind;
|
2018-01-26 17:28:29 +00:00
|
|
|
symbol.storage = storage;
|
2017-12-06 03:32:33 +00:00
|
|
|
symbol.ranges.push_back(*loc);
|
2018-02-11 18:25:37 +00:00
|
|
|
grouped_symbols[sym] = symbol;
|
2017-12-06 03:32:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 03:04:08 +00:00
|
|
|
// Make ranges non-overlapping using a scan line algorithm.
|
|
|
|
std::vector<ScanLineEvent> events;
|
|
|
|
int id = 0;
|
|
|
|
for (auto& entry : grouped_symbols) {
|
|
|
|
Out_CqueryPublishSemanticHighlighting::Symbol& symbol = entry.second;
|
|
|
|
for (auto& loc : symbol.ranges) {
|
2018-01-11 02:43:01 +00:00
|
|
|
// For ranges sharing the same start point, the one with leftmost end
|
|
|
|
// point comes first.
|
2018-01-10 03:04:08 +00:00
|
|
|
events.push_back({loc.start, loc.end, id, &symbol});
|
|
|
|
// For ranges sharing the same end point, their relative order does not
|
|
|
|
// matter, therefore we arbitrarily assign loc.end to them. We use
|
|
|
|
// negative id to indicate a deletion event.
|
|
|
|
events.push_back({loc.end, loc.end, ~id, &symbol});
|
|
|
|
id++;
|
|
|
|
}
|
|
|
|
symbol.ranges.clear();
|
|
|
|
}
|
|
|
|
std::sort(events.begin(), events.end());
|
|
|
|
|
|
|
|
std::vector<uint8_t> deleted(id, 0);
|
|
|
|
int top = 0;
|
|
|
|
for (size_t i = 0; i < events.size(); i++) {
|
|
|
|
while (top && deleted[events[top - 1].id])
|
|
|
|
top--;
|
|
|
|
// Order [a, b0) after [a, b1) if b0 < b1. The range comes later overrides
|
|
|
|
// the ealier. The order of [a0, b) [a1, b) does not matter.
|
|
|
|
// The order of [a, b) [b, c) does not as long as we do not emit empty
|
|
|
|
// ranges.
|
2018-01-11 02:43:01 +00:00
|
|
|
// Attribute range [events[i-1].pos, events[i].pos) to events[top-1].symbol
|
|
|
|
// .
|
2018-01-10 03:04:08 +00:00
|
|
|
if (top && !(events[i - 1].pos == events[i].pos))
|
2018-01-11 02:43:01 +00:00
|
|
|
events[top - 1].symbol->ranges.emplace_back(events[i - 1].pos,
|
|
|
|
events[i].pos);
|
2018-01-10 03:04:08 +00:00
|
|
|
if (events[i].id >= 0)
|
|
|
|
events[top++] = events[i];
|
|
|
|
else
|
|
|
|
deleted[~events[i].id] = 1;
|
|
|
|
}
|
|
|
|
|
2017-12-06 03:32:33 +00:00
|
|
|
// Publish.
|
|
|
|
Out_CqueryPublishSemanticHighlighting out;
|
|
|
|
out.params.uri = lsDocumentUri::FromPath(working_file->filename);
|
|
|
|
for (auto& entry : grouped_symbols)
|
2018-01-10 03:04:08 +00:00
|
|
|
if (entry.second.ranges.size())
|
|
|
|
out.params.symbols.push_back(entry.second);
|
2017-12-24 00:25:18 +00:00
|
|
|
QueueManager::WriteStdout(IpcId::CqueryPublishSemanticHighlighting, out);
|
2017-12-07 01:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldIgnoreFileForIndexing(const std::string& path) {
|
|
|
|
return StartsWith(path, "git:");
|
|
|
|
}
|