From 5208eb6be642a45cae412d3ad5937b78328fee21 Mon Sep 17 00:00:00 2001 From: Ludovic Jozeau Date: Sun, 9 May 2021 22:04:17 +0200 Subject: [PATCH 1/2] remove document symbols from macro expansion eg: #define M \ namespace ns_name \ { \ namespace ns_name_impl \ { \ void f() {} \ } \ } M; --- src/messages/textDocument_document.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/messages/textDocument_document.cc b/src/messages/textDocument_document.cc index 5d34668c..518430b8 100644 --- a/src/messages/textDocument_document.cc +++ b/src/messages/textDocument_document.cc @@ -130,6 +130,20 @@ template <> bool ignore(const QueryVar::Def *def) { } } // namespace +bool is_macro_expansion(DB *db, WorkingFile *wf, QueryFile *file, + DocumentSymbol const &ds) noexcept { + if (ds.kind == SymbolKind::Macro) { + return false; + } + auto start = ds.range.start; + auto syms = findSymbolsAtLocation(wf, file, start, true); + + auto end = std::end(syms); + return std::find_if(std::begin(syms), end, [db](SymbolRef sym) { + return getSymbolKind(db, sym) == SymbolKind::Macro; + }) != end; +} + void MessageHandler::textDocument_documentSymbol(JsonReader &reader, ReplyOnce &reply) { DocumentSymbolParam param; @@ -183,7 +197,7 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader, range1 && range1->includes(*range)) ds->range = *range1; } - withEntity(db, sym, [&](const auto &entity) { + withEntity(db, sym, [&, wf = wf, file = file](const auto &entity) { const auto *def = entity.anyDef(); if (!def) return; @@ -191,7 +205,9 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader, ds->detail = def->detailed_name; ds->kind = def->kind; - if (!ignore(def) && (ds->kind == SymbolKind::Namespace || allows(sym))) { + if (!ignore(def) && + (ds->kind == SymbolKind::Namespace || allows(sym)) && + !is_macro_expansion(db, wf, file, *ds)) { // Drop scopes which are before selectionRange.start. In // `int i, j, k;`, the scope of i will be ended by j. while (!scopes.empty() && From 0b57c9503d6bb9b9dfa627936fd5eef6157f992e Mon Sep 17 00:00:00 2001 From: Ludovic Jozeau Date: Mon, 10 May 2021 16:20:14 +0200 Subject: [PATCH 2/2] remove part of macro expansion from ignore function rename is_macro_expansion to isPartOfMacroExpansion --- src/messages/textDocument_document.cc | 59 ++++++++++++++++----------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/messages/textDocument_document.cc b/src/messages/textDocument_document.cc index 518430b8..54f5e76c 100644 --- a/src/messages/textDocument_document.cc +++ b/src/messages/textDocument_document.cc @@ -121,29 +121,41 @@ void reflect(JsonWriter &vis, std::unique_ptr &v) { reflect(vis, *v); } -template bool ignore(const Def *def) { return false; } -template <> bool ignore(const QueryType::Def *def) { - return !def || def->kind == SymbolKind::TypeParameter; -} -template <> bool ignore(const QueryVar::Def *def) { - return !def || def->is_local(); -} -} // namespace - -bool is_macro_expansion(DB *db, WorkingFile *wf, QueryFile *file, - DocumentSymbol const &ds) noexcept { - if (ds.kind == SymbolKind::Macro) { +template +bool isPartOfMacroExpansion(DB *db, WorkingFile *wf, QueryFile *file, + Def const *def) noexcept { + if (def->kind == SymbolKind::Macro) { return false; } - auto start = ds.range.start; - auto syms = findSymbolsAtLocation(wf, file, start, true); + auto range = getLsRange(wf, def->spell->range); + if (range) { + auto syms = findSymbolsAtLocation(wf, file, range->start, true); - auto end = std::end(syms); - return std::find_if(std::begin(syms), end, [db](SymbolRef sym) { - return getSymbolKind(db, sym) == SymbolKind::Macro; - }) != end; + auto end = std::end(syms); + return std::find_if(std::begin(syms), end, [db](SymbolRef sym) { + return getSymbolKind(db, sym) == SymbolKind::Macro; + }) != end; + } + return false; } +template +bool ignore(DB *db, WorkingFile *wf, QueryFile *file, const Def *def) { + return isPartOfMacroExpansion(db, wf, file, def); +} +template <> +bool ignore(DB *db, WorkingFile *wf, QueryFile *file, + const QueryType::Def *def) { + return !def || def->kind == SymbolKind::TypeParameter || + isPartOfMacroExpansion(db, wf, file, def); +} +template <> +bool ignore(DB *db, WorkingFile *wf, QueryFile *file, + const QueryVar::Def *def) { + return !def || def->is_local() || isPartOfMacroExpansion(db, wf, file, def); +} +} // namespace + void MessageHandler::textDocument_documentSymbol(JsonReader &reader, ReplyOnce &reply) { DocumentSymbolParam param; @@ -205,9 +217,8 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader, ds->detail = def->detailed_name; ds->kind = def->kind; - if (!ignore(def) && - (ds->kind == SymbolKind::Namespace || allows(sym)) && - !is_macro_expansion(db, wf, file, *ds)) { + if (!ignore(db, wf, file, def) && + (ds->kind == SymbolKind::Namespace || allows(sym))) { // Drop scopes which are before selectionRange.start. In // `int i, j, k;`, the scope of i will be ended by j. while (!scopes.empty() && @@ -230,8 +241,10 @@ void MessageHandler::textDocument_documentSymbol(JsonReader &reader, continue; if (std::optional info = getSymbolInfo(db, sym, false)) { - if ((sym.kind == Kind::Type && ignore(db->getType(sym).anyDef())) || - (sym.kind == Kind::Var && ignore(db->getVar(sym).anyDef()))) + if ((sym.kind == Kind::Type && + ignore(db, wf, file, db->getType(sym).anyDef())) || + (sym.kind == Kind::Var && + ignore(db, wf, file, db->getVar(sym).anyDef()))) continue; if (auto loc = getLsLocation(db, wfiles, sym, file_id)) { info->location = *loc;