From 554833abf22a033101bd4e8109b7845a058a8136 Mon Sep 17 00:00:00 2001 From: Felipe Lema <1232306+FelipeLema@users.noreply.github.com> Date: Mon, 12 Apr 2021 14:33:19 -0400 Subject: [PATCH] handle TODOs, imprve readbility --- src/messages/textDocument_semanticToken.cc | 116 +++++++++++---------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/src/messages/textDocument_semanticToken.cc b/src/messages/textDocument_semanticToken.cc index 5d3631ef..2c8cd740 100644 --- a/src/messages/textDocument_semanticToken.cc +++ b/src/messages/textDocument_semanticToken.cc @@ -23,7 +23,8 @@ struct SemanticTokens { REFLECT_STRUCT(SemanticTokens, data); struct CclsSemanticHighlightSymbol { - int id = 0; + using Id=int; + Id id = 0; SymbolKind parentKind; SymbolKind kind; uint8_t storage; @@ -33,7 +34,8 @@ struct CclsSemanticHighlightSymbol { struct ScanLineEvent { Position pos; Position end_pos; // Second key when there is a tie for insertion events. - int id; + using Id=int; + Id id; CclsSemanticHighlightSymbol *symbol; Role role; bool operator<(const ScanLineEvent &o) const { @@ -63,9 +65,9 @@ void MessageHandler::textDocument_semanticTokensFull( return; } - QueryFile *file = findFile(path); - if (!file) { - reply.notOpened(path); //TODO + auto [queryFile,wFile] = findOrFail(path, reply); + if (!queryFile) { + // `findOrFail` already set the reply message return; } @@ -75,21 +77,21 @@ void MessageHandler::textDocument_semanticTokensFull( g_config->highlight.blacklist); assert(file->def); if (wfile->buffer_content.size() > g_config->highlight.largeFileSize || - !match.matches(file->def->path)) { - reply.notOpened(path); //TODO - return; //TODO? + !match.matches(queryFile->def->path)) { + LOG_V(INFO) << "Not SemTokenizing " << path << "because of allowlist/denylist"; + return; } // Group symbols together. std::unordered_map grouped_symbols; - for (auto [sym, refcnt] : file->symbol2refcnt) { - if (refcnt <= 0) //TODO: unused? + for (auto [sym, refcnt] : queryFile->symbol2refcnt) { + if (refcnt <= 0) continue; std::string_view detailed_name; SymbolKind parent_kind = SymbolKind::Unknown; SymbolKind kind = SymbolKind::Unknown; uint8_t storage = SC_None; - int idx; + DB::UsrIndex idx; // This switch statement also filters out symbols that are not highlighted. switch (sym.kind) { case Kind::Func: { @@ -100,7 +102,7 @@ void MessageHandler::textDocument_semanticTokensFull( continue; // applies to for loop // Don't highlight overloadable operators or implicit lambda -> // std::function constructor. - std::string_view short_name = def->name(false); + const auto short_name = def->name(false); if (short_name.compare(0, 8, "operator") == 0) continue; // applies to for loop kind = def->kind; @@ -112,14 +114,14 @@ void MessageHandler::textDocument_semanticTokensFull( // 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 = + const auto concise_name = detailed_name.substr(0, detailed_name.find('<')); - uint16_t start_line = sym.range.start.line; - int16_t start_col = sym.range.start.column; - if (start_line >= wfile->index_lines.size()) + const auto start_line_idx = sym.range.start.line; + const auto start_col = sym.range.start.column; + if (start_line_idx >= wfile->index_lines.size()) // out-of-range ? continue; - std::string_view line = wfile->index_lines[start_line]; - sym.range.end.line = start_line; + const auto line = wfile->index_lines[start_line_idx]; + sym.range.end.line = start_line_idx; if (!(start_col + concise_name.size() <= line.size() && line.compare(start_col, concise_name.size(), concise_name) == 0)) continue; @@ -157,10 +159,10 @@ void MessageHandler::textDocument_semanticTokensFull( continue; // applies to for loop } - if (std::optional loc = getLsRange(wfile, sym.range)) { + if (auto maybe_loc = getLsRange(wfile, sym.range)) { auto it = grouped_symbols.find(sym); if (it != grouped_symbols.end()) { - it->second.lsRangeAndRoles.push_back({*loc, sym.role}); + it->second.lsRangeAndRoles.push_back({*maybe_loc, sym.role}); } else { CclsSemanticHighlightSymbol symbol; symbol.id = idx; @@ -175,7 +177,7 @@ void MessageHandler::textDocument_semanticTokensFull( // Make ranges non-overlapping using a scan line algorithm. std::vector events; - int id = 0; + ScanLineEvent::Id id = 0; for (auto &entry : grouped_symbols) { CclsSemanticHighlightSymbol &symbol = entry.second; for (auto &loc : symbol.lsRangeAndRoles) { @@ -213,44 +215,44 @@ void MessageHandler::textDocument_semanticTokensFull( } // Transform lsRange into pair (offset pairs) - std::vector, CclsSemanticHighlightSymbol *>> scratch; - for (auto &entry : grouped_symbols) { - for (auto &range : entry.second.lsRangeAndRoles) - scratch.emplace_back(range, &entry.second); - entry.second.lsRangeAndRoles.clear(); + std::vector, CclsSemanticHighlightSymbol *>> scratch; + for (auto &entry : grouped_symbols) { + for (auto &range : entry.second.lsRangeAndRoles) + scratch.emplace_back(range, &entry.second); + entry.second.lsRangeAndRoles.clear(); + } + std::sort(scratch.begin(), scratch.end(), + [](auto &l, auto &r) { return l.first.first.start < r.first.first.start; }); + int line = 0; + int column = 0; + for (auto &entry : scratch) { + lsRange &r = entry.first.first; + if (r.start.line != line) { + column = 0; } - std::sort(scratch.begin(), scratch.end(), - [](auto &l, auto &r) { return l.first.first.start < r.first.first.start; }); - int line = 0; - int column = 0; - for (auto &entry : scratch) { - lsRange &r = entry.first.first; - if (r.start.line != line) { - column = 0; - } - result.data.push_back(r.start.line - line); line = r.start.line; - result.data.push_back(r.start.character - column); column = r.start.character; - result.data.push_back(r.end.character - r.start.character); - uint8_t kindId; - int modifiers = entry.second->storage == SC_Static ? 4 : 0; - if (entry.first.second & Role::Declaration) { - modifiers |= 1; - } - if (entry.first.second & Role::Definition) { - modifiers |= 2; - } - if (entry.second->kind == SymbolKind::StaticMethod) { - kindId = (uint8_t) SymbolKind::Method; - modifiers = 4; - } else { - kindId = (uint8_t) entry.second->kind; - if (kindId > (uint8_t) SymbolKind::StaticMethod) - kindId--; - if (kindId >= 252) kindId = 27 + kindId - 252; - } - result.data.push_back(kindId); - result.data.push_back(modifiers); + result.data.push_back(r.start.line - line); line = r.start.line; + result.data.push_back(r.start.character - column); column = r.start.character; + result.data.push_back(r.end.character - r.start.character); + uint8_t kindId; + int modifiers = entry.second->storage == SC_Static ? 4 : 0; + if (entry.first.second & Role::Declaration) { + modifiers |= 1; } + if (entry.first.second & Role::Definition) { + modifiers |= 2; + } + if (entry.second->kind == SymbolKind::StaticMethod) { + kindId = (uint8_t) SymbolKind::Method; + modifiers = 4; + } else { + kindId = (uint8_t) entry.second->kind; + if (kindId > (uint8_t) SymbolKind::StaticMethod) + kindId--; + if (kindId >= 252) kindId = 27 + kindId - 252; + } + result.data.push_back(kindId); + result.data.push_back(modifiers); + } reply(result); }