textDocument/definitions: on a variable definition, get both declarations and variable_type definition.

This commit is contained in:
Fangrui Song 2017-12-19 22:20:44 -08:00 committed by Jacob Dufault
parent 519abd1090
commit 3410f9769c
3 changed files with 34 additions and 10 deletions

View File

@ -24,6 +24,29 @@ struct Out_TextDocumentDefinition
}; };
MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result); MAKE_REFLECT_STRUCT(Out_TextDocumentDefinition, jsonrpc, id, result);
std::vector<QueryLocation> GetGotoDefinitionTargets(
QueryDatabase* db,
const SymbolIdx& symbol) {
switch (symbol.kind) {
// Returns GetDeclarationsOfSymbolForGotoDefinition and
// variable type definition.
case SymbolKind::Var: {
std::vector<QueryLocation> ret =
GetDeclarationsOfSymbolForGotoDefinition(db, symbol);
QueryVar& var = db->vars[symbol.idx];
if (var.def && var.def->variable_type) {
std::vector<QueryLocation> 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 struct TextDocumentDefinitionHandler
: BaseMessageHandler<Ipc_TextDocumentDefinition> { : BaseMessageHandler<Ipc_TextDocumentDefinition> {
void Run(Ipc_TextDocumentDefinition* request) override { void Run(Ipc_TextDocumentDefinition* request) override {
@ -72,13 +95,13 @@ struct TextDocumentDefinitionHandler
def_loc->range.Contains(target_line, target_column))) { def_loc->range.Contains(target_line, target_column))) {
// Goto declaration. // Goto declaration.
std::vector<QueryLocation> declarations = std::vector<QueryLocation> targets =
GetDeclarationsOfSymbolForGotoDefinition(db, ref.idx); GetGotoDefinitionTargets(db, ref.idx);
for (auto declaration : declarations) { for (QueryLocation target : targets) {
optional<lsLocation> ls_declaration = optional<lsLocation> ls_target =
GetLsLocation(db, working_files, declaration); GetLsLocation(db, working_files, target);
if (ls_declaration) if (ls_target)
out.result.push_back(*ls_declaration); out.result.push_back(*ls_target);
} }
// We found some declarations. Break so we don't add the definition // We found some declarations. Break so we don't add the definition
// location. // location.
@ -110,4 +133,4 @@ struct TextDocumentDefinitionHandler
} }
}; };
REGISTER_MESSAGE_HANDLER(TextDocumentDefinitionHandler); REGISTER_MESSAGE_HANDLER(TextDocumentDefinitionHandler);
} // namespace } // namespace

View File

@ -556,4 +556,4 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
}); });
return symbols; return symbols;
} }

View File

@ -33,6 +33,7 @@ std::vector<QueryLocation> GetUsesOfSymbol(QueryDatabase* db,
std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition( std::vector<QueryLocation> GetDeclarationsOfSymbolForGotoDefinition(
QueryDatabase* db, QueryDatabase* db,
const SymbolIdx& symbol); const SymbolIdx& symbol);
bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root); bool HasCallersOnSelfOrBaseOrDerived(QueryDatabase* db, QueryFunc& root);
std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db, std::vector<QueryFuncRef> GetCallersForAllBaseFunctions(QueryDatabase* db,
QueryFunc& root); QueryFunc& root);
@ -65,4 +66,4 @@ lsWorkspaceEdit BuildWorkspaceEdit(QueryDatabase* db,
std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file, std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
QueryFile* file, QueryFile* file,
lsPosition position); lsPosition position);