Order SymbolRef by size first, Role::Definition second

This commit is contained in:
Fangrui Song 2018-02-18 19:39:18 -08:00
parent 5164c4b2f6
commit d33bf50181
3 changed files with 11 additions and 28 deletions

View File

@ -23,17 +23,8 @@ struct CqueryBaseHandler : BaseMessageHandler<Ipc_CqueryBase> {
Out_LocationList out; Out_LocationList out;
out.id = request->id; out.id = request->id;
std::vector<SymbolRef> syms = for (SymbolRef sym :
FindSymbolsAtLocation(working_file, file, request->params.position); 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) {
if (sym.kind == SymbolKind::Type) { if (sym.kind == SymbolKind::Type) {
if (const auto* def = db->GetType(sym).AnyDef()) if (const auto* def = db->GetType(sym).AnyDef())
out.result = GetLsLocations(db, working_files, out.result = GetLsLocations(db, working_files,

View File

@ -23,17 +23,8 @@ struct CqueryDerivedHandler : BaseMessageHandler<Ipc_CqueryDerived> {
Out_LocationList out; Out_LocationList out;
out.id = request->id; out.id = request->id;
std::vector<SymbolRef> syms = for (SymbolRef sym :
FindSymbolsAtLocation(working_file, file, request->params.position); 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) {
if (sym.kind == SymbolKind::Type) { if (sym.kind == SymbolKind::Type) {
QueryType& type = db->GetType(sym); QueryType& type = db->GetType(sym);
out.result = out.result =

View File

@ -534,13 +534,14 @@ std::vector<SymbolRef> FindSymbolsAtLocation(WorkingFile* working_file,
// better on constructors. // better on constructors.
std::sort(symbols.begin(), symbols.end(), std::sort(symbols.begin(), symbols.end(),
[](const SymbolRef& a, const SymbolRef& b) { [](const SymbolRef& a, const SymbolRef& b) {
int a_size = ComputeRangeSize(a.range); int t = ComputeRangeSize(a.range) - ComputeRangeSize(b.range);
int b_size = ComputeRangeSize(b.range); if (t)
return t < 0;
if (a_size != b_size) t = (a.role & Role::Definition) - (b.role & Role::Definition);
return a_size < b_size; if (t)
return t > 0;
// operator> orders Var/Func before Type. // operator> orders Var/Func before Type.
int t = static_cast<int>(a.kind) - static_cast<int>(b.kind); t = static_cast<int>(a.kind) - static_cast<int>(b.kind);
if (t) if (t)
return t > 0; return t > 0;
return a.id < b.id; return a.id < b.id;