Add lexical parent to Func

This commit is contained in:
Fangrui Song 2018-02-10 20:30:27 -08:00
parent 9a6cbafa04
commit e019968f51
7 changed files with 22 additions and 28 deletions

View File

@ -1594,8 +1594,8 @@ void OnIndexDeclaration(CXClientData client_data, const CXIdxDeclInfo* decl) {
if (decl->isDefinition && !is_template_specialization) { if (decl->isDefinition && !is_template_specialization) {
// assert(!func->def.spell); // assert(!func->def.spell);
// assert(!func->def.extent); // assert(!func->def.extent);
func->def.spell = decl_spelling; SetUse(db, &func->def.spell, decl_spelling, lex_parent, Role::Definition);
func->def.extent = decl_extent; SetUse(db, &func->def.extent, decl_extent, lex_parent, Role::None);
} else { } else {
IndexFunc::Declaration declaration; IndexFunc::Declaration declaration;
declaration.spelling = decl_spelling; declaration.spelling = decl_spelling;

View File

@ -260,8 +260,8 @@ struct FuncDefDefinitionData {
std::string detailed_name; std::string detailed_name;
std::string hover; std::string hover;
std::string comments; std::string comments;
Maybe<typename F::Range> spell; Maybe<Use> spell;
Maybe<typename F::Range> extent; Maybe<Use> extent;
// Method this method overrides. // Method this method overrides.
std::vector<typename F::FuncId> base; std::vector<typename F::FuncId> base;

View File

@ -175,7 +175,7 @@ struct TextDocumentCodeLensHandler
// extent since that is better for outline. This tries to convert the // extent since that is better for outline. This tries to convert the
// extent location to the spelling location. // extent location to the spelling location.
auto try_ensure_spelling = [&](SymbolRef sym) { auto try_ensure_spelling = [&](SymbolRef sym) {
Maybe<Reference> def = GetDefinitionSpellingOfSymbol(db, sym); Maybe<Use> def = GetDefinitionSpellingOfSymbol(db, sym);
if (!def || db->GetFileId(*def) != db->GetFileId(sym) || if (!def || db->GetFileId(*def) != db->GetFileId(sym) ||
def->range.start.line != sym.range.start.line) { def->range.start.line != sym.range.start.line) {
return sym; return sym;
@ -213,7 +213,7 @@ struct TextDocumentCodeLensHandler
// "Base" // "Base"
if (func.def->base.size() == 1) { if (func.def->base.size() == 1) {
Maybe<Reference> base_loc = Maybe<Use> base_loc =
GetDefinitionSpellingOfSymbol(db, func.def->base[0]); GetDefinitionSpellingOfSymbol(db, func.def->base[0]);
if (base_loc) { if (base_loc) {
optional<lsLocation> ls_base = optional<lsLocation> ls_base =

View File

@ -74,7 +74,7 @@ struct TextDocumentDefinitionHandler
// - start at spelling but end at extent for better mouse tooltip // - start at spelling but end at extent for better mouse tooltip
// - goto declaration while in definition of recursive type // - goto declaration while in definition of recursive type
Maybe<Reference> def_loc = GetDefinitionSpellingOfSymbol(db, sym); Maybe<Use> def_loc = GetDefinitionSpellingOfSymbol(db, sym);
// We use spelling start and extent end because this causes vscode to // We use spelling start and extent end because this causes vscode to
// highlight the entire definition when previewing / hoving with the // highlight the entire definition when previewing / hoving with the

View File

@ -67,12 +67,8 @@ optional<QueryFunc::Def> ToQuery(const IdMap& id_map,
result.hover = func.hover; result.hover = func.hover;
result.comments = func.comments; result.comments = func.comments;
result.file = id_map.primary_file; result.file = id_map.primary_file;
if (func.spell) result.spell = id_map.ToQuery(func.spell);
result.spell = result.extent = id_map.ToQuery(func.extent);
id_map.ToQuery(*func.spell, Role::Definition);
if (func.extent)
result.extent =
id_map.ToQuery(*func.extent, Role::None);
result.declaring_type = id_map.ToQuery(func.declaring_type); result.declaring_type = id_map.ToQuery(func.declaring_type);
result.base = id_map.ToQuery(func.base); result.base = id_map.ToQuery(func.base);
result.locals = id_map.ToQuery(func.locals); result.locals = id_map.ToQuery(func.locals);
@ -274,11 +270,9 @@ QueryFile::DefUpdate BuildFileDefUpdate(const IdMap& id_map, const IndexFile& in
for (const IndexFunc& func : indexed.funcs) { for (const IndexFunc& func : indexed.funcs) {
QueryFuncId id = id_map.ToQuery(func.id); QueryFuncId id = id_map.ToQuery(func.id);
if (func.def.spell.has_value()) if (func.def.spell.has_value())
add_all_symbols(*func.def.spell, id, SymbolKind::Func, add_all_symbols_use(*func.def.spell, id, SymbolKind::Func);
Role::Definition);
if (func.def.extent.has_value()) if (func.def.extent.has_value())
add_outline(*func.def.extent, id, add_outline_use(*func.def.extent, id, SymbolKind::Func);
SymbolKind::Func, Role::None);
for (const IndexFunc::Declaration& decl : func.declarations) { for (const IndexFunc::Declaration& decl : func.declarations) {
add_all_symbols(decl.spelling, id, SymbolKind::Func, add_all_symbols(decl.spelling, id, SymbolKind::Func,
Role::Declaration); Role::Declaration);
@ -1023,7 +1017,7 @@ TEST_SUITE("query") {
previous.Resolve(previous.ToTypeId(HashUsr("usr1"))) previous.Resolve(previous.ToTypeId(HashUsr("usr1")))
->def.spell = Use(Range(Position(1, 0)), Id<void>(), SymbolKind::File, Role::None); ->def.spell = Use(Range(Position(1, 0)), Id<void>(), SymbolKind::File, Role::None);
previous.Resolve(previous.ToFuncId(HashUsr("usr2"))) previous.Resolve(previous.ToFuncId(HashUsr("usr2")))
->def.spell = Range(Position(2, 0)); ->def.spell = Use(Range(Position(2, 0)), Id<void>(), SymbolKind::File, Role::None);
previous.Resolve(previous.ToVarId(HashUsr("usr3"))) previous.Resolve(previous.ToVarId(HashUsr("usr3")))
->def.spell = Use(Range(Position(3, 0)), Id<void>(), SymbolKind::File, Role::None); ->def.spell = Use(Range(Position(3, 0)), Id<void>(), SymbolKind::File, Role::None);

View File

@ -18,21 +18,21 @@ int ComputeRangeSize(const Range& range) {
} // namespace } // namespace
Maybe<Reference> GetDefinitionSpellingOfSymbol(QueryDatabase* db, Maybe<Use> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
QueryFuncId id) { QueryFuncId id) {
QueryFunc& func = db->funcs[id.id]; QueryFunc& func = db->funcs[id.id];
if (func.def) if (func.def)
return func.def->spell; return func.def->spell;
return nullopt; return nullopt;
} }
Maybe<Reference> GetDefinitionSpellingOfSymbol(QueryDatabase* db, Maybe<Use> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
SymbolRef sym) { SymbolRef sym) {
switch (sym.kind) { switch (sym.kind) {
case SymbolKind::Type: { case SymbolKind::Type: {
QueryType& type = db->GetType(sym); QueryType& type = db->GetType(sym);
if (type.def) if (type.def)
return *type.def->spell; return type.def->spell;
break; break;
} }
case SymbolKind::Func: { case SymbolKind::Func: {
@ -44,7 +44,7 @@ Maybe<Reference> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
case SymbolKind::Var: { case SymbolKind::Var: {
QueryVar& var = db->GetVar(sym); QueryVar& var = db->GetVar(sym);
if (var.def) if (var.def)
return *var.def->spell; return var.def->spell;
break; break;
} }
case SymbolKind::File: case SymbolKind::File:

View File

@ -7,10 +7,10 @@
#include <optional.h> #include <optional.h>
Maybe<Reference> GetDefinitionSpellingOfSymbol(QueryDatabase* db, Maybe<Use> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
QueryFuncId id); QueryFuncId id);
Maybe<Reference> GetDefinitionSpellingOfSymbol(QueryDatabase* db, Maybe<Use> GetDefinitionSpellingOfSymbol(QueryDatabase* db,
SymbolRef sym); SymbolRef sym);
Maybe<Use> GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym); Maybe<Use> GetDefinitionExtentOfSymbol(QueryDatabase* db, SymbolRef sym);
Maybe<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db, Maybe<QueryFileId> GetDeclarationFileForSymbol(QueryDatabase* db,
SymbolRef sym); SymbolRef sym);