From 69f749ac9f400e6ddb9201acac9df4e34a229ea0 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 6 Sep 2018 00:22:40 -0700 Subject: [PATCH] Improve hover and documentSymbol --- src/messages/textDocument_hover.cc | 5 ++++ src/query.cc | 40 ++++++++++++++---------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/messages/textDocument_hover.cc b/src/messages/textDocument_hover.cc index 8455da4b..920f6ff3 100644 --- a/src/messages/textDocument_hover.cc +++ b/src/messages/textDocument_hover.cc @@ -24,6 +24,11 @@ GetHover(DB *db, LanguageId lang, SymbolRef sym, int file_id) { break; } } + if (!def && entity.def.size()) { + def = &entity.def[0]; + if (def->comments[0]) + comments = def->comments; + } if (def) { lsMarkedString m; m.language = LanguageIdentifier(lang); diff --git a/src/query.cc b/src/query.cc index cc9b9346..d5fd3287 100644 --- a/src/query.cc +++ b/src/query.cc @@ -217,21 +217,19 @@ void DB::ApplyIndexUpdate(IndexUpdate *u) { // References (Use &use) in this function are important to update file_id. auto Ref = [&](std::unordered_map &lid2fid, Usr usr, - SymbolKind kind, Use &use, int delta) { + SymbolKind kind, Use &use, int delta, int k = 1) { use.file_id = use.file_id == -1 ? u->file_id : lid2fid.find(use.file_id)->second; - int &v = - files[use.file_id] + if (k & 1) { + int &v = + files[use.file_id] .symbol2refcnt[SymbolRef{{use.range, usr, kind, use.role}}]; - v += delta; - assert(v >= 0); - }; - auto RefO = [&](std::unordered_map &lid2fid, Usr usr, - SymbolKind kind, Use &use, int delta) { - use.file_id = - use.file_id == -1 ? u->file_id : lid2fid.find(use.file_id)->second; - files[use.file_id] - .outline2refcnt[SymbolRef{{use.range, usr, kind, use.role}}] += delta; + v += delta; + assert(v >= 0); + } + if (k & 2) + files[use.file_id] + .outline2refcnt[SymbolRef{{use.range, usr, kind, use.role}}] += delta; }; auto UpdateUses = [&](Usr usr, SymbolKind kind, @@ -283,15 +281,15 @@ void DB::ApplyIndexUpdate(IndexUpdate *u) { if (def.spell) Ref(prev_lid2file_id, usr, SymbolKind::Func, *def.spell, -1); if (def.extent) - RefO(prev_lid2file_id, usr, SymbolKind::Func, *def.extent, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Func, *def.extent, -1, 2); } RemoveUsrs(SymbolKind::Func, u->file_id, u->funcs_removed); Update(lid2file_id, u->file_id, std::move(u->funcs_def_update)); for (auto &[usr, del_add]: u->funcs_declarations) { for (Use &use : del_add.first) - Ref(prev_lid2file_id, usr, SymbolKind::Func, use, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Func, use, -1, 3); for (Use &use : del_add.second) - Ref(lid2file_id, usr, SymbolKind::Func, use, 1); + Ref(lid2file_id, usr, SymbolKind::Func, use, 1, 3); } REMOVE_ADD(func, declarations); REMOVE_ADD(func, derived); @@ -307,15 +305,15 @@ void DB::ApplyIndexUpdate(IndexUpdate *u) { if (def.spell) Ref(prev_lid2file_id, usr, SymbolKind::Type, *def.spell, -1); if (def.extent) - RefO(prev_lid2file_id, usr, SymbolKind::Type, *def.extent, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Type, *def.extent, -1, 2); } RemoveUsrs(SymbolKind::Type, u->file_id, u->types_removed); Update(lid2file_id, u->file_id, std::move(u->types_def_update)); for (auto &[usr, del_add]: u->types_declarations) { for (Use &use : del_add.first) - Ref(prev_lid2file_id, usr, SymbolKind::Type, use, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Type, use, -1, 3); for (Use &use : del_add.second) - Ref(lid2file_id, usr, SymbolKind::Type, use, 1); + Ref(lid2file_id, usr, SymbolKind::Type, use, 1, 3); } REMOVE_ADD(type, declarations); REMOVE_ADD(type, derived); @@ -332,15 +330,15 @@ void DB::ApplyIndexUpdate(IndexUpdate *u) { if (def.spell) Ref(prev_lid2file_id, usr, SymbolKind::Var, *def.spell, -1); if (def.extent) - RefO(prev_lid2file_id, usr, SymbolKind::Var, *def.extent, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Var, *def.extent, -1, 2); } RemoveUsrs(SymbolKind::Var, u->file_id, u->vars_removed); Update(lid2file_id, u->file_id, std::move(u->vars_def_update)); for (auto &[usr, del_add]: u->vars_declarations) { for (Use &use : del_add.first) - Ref(prev_lid2file_id, usr, SymbolKind::Var, use, -1); + Ref(prev_lid2file_id, usr, SymbolKind::Var, use, -1, 3); for (Use &use : del_add.second) - Ref(lid2file_id, usr, SymbolKind::Var, use, 1); + Ref(lid2file_id, usr, SymbolKind::Var, use, 1, 3); } REMOVE_ADD(var, declarations); for (auto &[usr, p] : u->vars_uses)