From d33bf501814b0b2c1c449276f162cbaf36e4257c Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 18 Feb 2018 19:39:18 -0800 Subject: [PATCH] Order SymbolRef by size first, Role::Definition second --- src/messages/cquery_base.cc | 13 ++----------- src/messages/cquery_derived.cc | 13 ++----------- src/query_utils.cc | 13 +++++++------ 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/messages/cquery_base.cc b/src/messages/cquery_base.cc index a34b360c..226999cc 100644 --- a/src/messages/cquery_base.cc +++ b/src/messages/cquery_base.cc @@ -23,17 +23,8 @@ struct CqueryBaseHandler : BaseMessageHandler { Out_LocationList out; out.id = request->id; - std::vector syms = - FindSymbolsAtLocation(working_file, file, request->params.position); - // A template definition may be a use of its primary template. - // We want to get the definition instead of the use. - // Order by |Definition| DESC, range size ASC. - std::stable_sort(syms.begin(), syms.end(), - [](const SymbolRef& a, const SymbolRef& b) { - return (a.role & Role::Definition) > - (b.role & Role::Definition); - }); - for (SymbolRef sym : syms) { + for (SymbolRef sym : + FindSymbolsAtLocation(working_file, file, request->params.position)) { if (sym.kind == SymbolKind::Type) { if (const auto* def = db->GetType(sym).AnyDef()) out.result = GetLsLocations(db, working_files, diff --git a/src/messages/cquery_derived.cc b/src/messages/cquery_derived.cc index 6c6283cd..be4e3945 100644 --- a/src/messages/cquery_derived.cc +++ b/src/messages/cquery_derived.cc @@ -23,17 +23,8 @@ struct CqueryDerivedHandler : BaseMessageHandler { Out_LocationList out; out.id = request->id; - std::vector syms = - FindSymbolsAtLocation(working_file, file, request->params.position); - // A template definition may be a use of its primary template. - // We want to get the definition instead of the use. - // Order by |Definition| DESC, range size ASC. - std::stable_sort(syms.begin(), syms.end(), - [](const SymbolRef& a, const SymbolRef& b) { - return (a.role & Role::Definition) > - (b.role & Role::Definition); - }); - for (const SymbolRef& sym : syms) { + for (SymbolRef sym : + FindSymbolsAtLocation(working_file, file, request->params.position)) { if (sym.kind == SymbolKind::Type) { QueryType& type = db->GetType(sym); out.result = diff --git a/src/query_utils.cc b/src/query_utils.cc index faf95cbb..0e86215d 100644 --- a/src/query_utils.cc +++ b/src/query_utils.cc @@ -534,13 +534,14 @@ std::vector FindSymbolsAtLocation(WorkingFile* working_file, // better on constructors. std::sort(symbols.begin(), symbols.end(), [](const SymbolRef& a, const SymbolRef& b) { - int a_size = ComputeRangeSize(a.range); - int b_size = ComputeRangeSize(b.range); - - if (a_size != b_size) - return a_size < b_size; + int t = ComputeRangeSize(a.range) - ComputeRangeSize(b.range); + if (t) + return t < 0; + t = (a.role & Role::Definition) - (b.role & Role::Definition); + if (t) + return t > 0; // operator> orders Var/Func before Type. - int t = static_cast(a.kind) - static_cast(b.kind); + t = static_cast(a.kind) - static_cast(b.kind); if (t) return t > 0; return a.id < b.id;