Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Include symbol role information in semantic highlighting (fixes #787)
Browse files Browse the repository at this point in the history
This allows clients to e.g. color references differently from
declarations.
  • Loading branch information
HighCommander4 authored and jacobdufault committed Nov 26, 2018
1 parent 9494c3e commit 70c755b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/message_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ void EmitInactiveLines(WorkingFile* working_file,
QueueManager::WriteStdout(kMethodType_CqueryPublishInactiveRegions, out);
}

struct SymbolAndRole {
SymbolIdx symbol;
Role role;

bool operator==(const SymbolAndRole& o) const {
return symbol == o.symbol && role == o.role;
}
};
MAKE_HASHABLE(SymbolAndRole, t.symbol, t.role);


void EmitSemanticHighlighting(QueryDatabase* db,
SemanticHighlightSymbolCache* semantic_cache,
WorkingFile* working_file,
Expand All @@ -127,13 +138,14 @@ void EmitSemanticHighlighting(QueryDatabase* db,
semantic_cache->GetCacheForFile(file->def->path);

// Group symbols together.
std::unordered_map<SymbolIdx, Out_CqueryPublishSemanticHighlighting::Symbol>
std::unordered_map<SymbolAndRole, Out_CqueryPublishSemanticHighlighting::Symbol>
grouped_symbols;
for (QueryId::SymbolRef sym : file->def->all_symbols) {
std::string_view detailed_name;
lsSymbolKind parent_kind = lsSymbolKind::Unknown;
lsSymbolKind kind = lsSymbolKind::Unknown;
StorageClass storage = StorageClass::Invalid;
Role role = sym.role;
// This switch statement also filters out symbols that are not highlighted.
switch (sym.kind) {
case SymbolKind::Func: {
Expand Down Expand Up @@ -217,8 +229,9 @@ void EmitSemanticHighlighting(QueryDatabase* db,
}

optional<lsRange> loc = GetLsRange(working_file, sym.range);
SymbolAndRole key{sym, role};
if (loc) {
auto it = grouped_symbols.find(sym);
auto it = grouped_symbols.find(key);
if (it != grouped_symbols.end()) {
it->second.ranges.push_back(*loc);
} else {
Expand All @@ -228,8 +241,9 @@ void EmitSemanticHighlighting(QueryDatabase* db,
symbol.parentKind = parent_kind;
symbol.kind = kind;
symbol.storage = storage;
symbol.role = role;
symbol.ranges.push_back(*loc);
grouped_symbols[sym] = symbol;
grouped_symbols[key] = symbol;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/message_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Out_CqueryPublishSemanticHighlighting
lsSymbolKind parentKind;
lsSymbolKind kind;
StorageClass storage;
Role role;
std::vector<lsRange> ranges;
};
struct Params {
Expand All @@ -46,6 +47,7 @@ MAKE_REFLECT_STRUCT(Out_CqueryPublishSemanticHighlighting::Symbol,
parentKind,
kind,
storage,
role,
ranges);
MAKE_REFLECT_STRUCT(Out_CqueryPublishSemanticHighlighting::Params,
uri,
Expand Down

0 comments on commit 70c755b

Please sign in to comment.