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.id = request->id;
std::vector<SymbolRef> 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,

View File

@ -23,17 +23,8 @@ struct CqueryDerivedHandler : BaseMessageHandler<Ipc_CqueryDerived> {
Out_LocationList out;
out.id = request->id;
std::vector<SymbolRef> 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 =

View File

@ -534,13 +534,14 @@ std::vector<SymbolRef> 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<int>(a.kind) - static_cast<int>(b.kind);
t = static_cast<int>(a.kind) - static_cast<int>(b.kind);
if (t)
return t > 0;
return a.id < b.id;