diff --git a/src/messages/text_document_definition.cc b/src/messages/text_document_definition.cc index f4f617b3..39994de1 100644 --- a/src/messages/text_document_definition.cc +++ b/src/messages/text_document_definition.cc @@ -24,6 +24,29 @@ struct Out_TextDocumentDefinition }; MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result); +std::vector GetGotoDefinitionTargets( + QueryDatabase* db, + const SymbolIdx& symbol) { + switch (symbol.kind) { + // Returns GetDeclarationsOfSymbolForGotoDefinition and + // variable type definition. + case SymbolKind::Var: { + std::vector ret = + GetDeclarationsOfSymbolForGotoDefinition(db, symbol); + QueryVar& var = db->vars[symbol.idx]; + if (var.def && var.def->variable_type) { + std::vector types = + GetDeclarationsOfSymbolForGotoDefinition( + db, SymbolIdx(SymbolKind::Type, var.def->variable_type->id)); + ret.insert(ret.end(), types.begin(), types.end()); + } + return ret; + } + default: + return GetDeclarationsOfSymbolForGotoDefinition(db, symbol); + } +} + struct TextDocumentDefinitionHandler : BaseMessageHandler { void Run(Ipc_TextDocumentDefinition* request) override { @@ -72,13 +95,13 @@ struct TextDocumentDefinitionHandler def_loc->range.Contains(target_line, target_column))) { // Goto declaration. - std::vector declarations = - GetDeclarationsOfSymbolForGotoDefinition(db, ref.idx); - for (auto declaration : declarations) { - optional ls_declaration = - GetLsLocation(db, working_files, declaration); - if (ls_declaration) - out.result.push_back(*ls_declaration); + std::vector targets = + GetGotoDefinitionTargets(db, ref.idx); + for (QueryLocation target : targets) { + optional ls_target = + GetLsLocation(db, working_files, target); + if (ls_target) + out.result.push_back(*ls_target); } // We found some declarations. Break so we don't add the definition // location. @@ -110,4 +133,4 @@ struct TextDocumentDefinitionHandler } }; REGISTER_MESSAGE_HANDLER(TextDocumentDefinitionHandler); -} // namespace \ No newline at end of file +} // namespace diff --git a/src/query_utils.cc b/src/query_utils.cc index e2a29e72..ba74b22e 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -556,4 +556,4 @@ std::vector FindSymbolsAtLocation(WorkingFile* working_file, }); return symbols; -} \ No newline at end of file +} diff --git a/src/query_utils.h b/src/query_utils.h index 4bd6ff7a..daa3156c 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -33,6 +33,7 @@ std::vector GetUsesOfSymbol(QueryDatabase* db, std::vector GetDeclarationsOfSymbolForGotoDefinition( QueryDatabase* db, const SymbolIdx& symbol); + bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root); std::vector GetCallersForAllBaseFunctions(QueryDatabase* db, QueryFunc& root); @@ -65,4 +66,4 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db, std::vector FindSymbolsAtLocation(WorkingFile* working_file, QueryFile* file, - lsPosition position); \ No newline at end of file + lsPosition position);