Skip to content

Commit

Permalink
Order SymbolRef by size first, Role::Definition second
Browse files Browse the repository at this point in the history
  • Loading branch information
MaskRay committed Feb 19, 2018
1 parent 5164c4b commit d33bf50
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 28 deletions.
13 changes: 2 additions & 11 deletions src/messages/cquery_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 2 additions & 11 deletions src/messages/cquery_derived.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 7 additions & 6 deletions src/query_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d33bf50

Please sign in to comment.